bayfrontmedia / array-schema
一个简单的库,用于强制给定数组使用预定义的“模式”。
v2.0.0
2023-01-26 18:32 UTC
Requires
- php: ^8.0
- bayfrontmedia/php-array-helpers: ^2.0
- bayfrontmedia/php-http-request: ^3.0
- bayfrontmedia/php-http-response: ^2.0
README
一个简单的库,用于强制给定数组使用预定义的“模式”。
许可证
本项目是开源的,并且可在MIT许可证下获取。
作者
需求
- PHP
^8.0
安装
composer require bayfrontmedia/array-schema
用法
预期用法是使用自定义模式实现Bayfront\ArraySchema\SchemaInterface
。此类确保数组符合其期望的模式,或者抛出Bayfront\ArraySchema\InvalidSchemaException
异常。
SchemaInterface
只包含一个静态方法
create
描述
返回符合期望模式的数组。
参数
$array
(数组): 输入数组$config = []
(数组): 可选配置数组,可用于传递构建期望模式所需选项。
返回
- (数组)
抛出
Bayfront\ArraySchema\InvalidSchemaException
包含的模式
此库包含一些示例模式,有助于根据最新的JSON API规范构建JSON响应。
这些绝不仅仅是“万用解决方案”,也不会强制执行所有JSON:API规范。它们仅作为示例用法参考。
资源文档
$array
是要返回的单个资源。- 如果存在
$config['base_url']
,其值将从返回的任何链接中移除。
示例
use Bayfront\ArraySchema\Schemas\ResourceDocument;
$resource = [
'type' => 'item',
'id' => '1001',
'attributes' => [
'key' => 'value'
],
'links' => [
'self' => 'https://api.example.com/items/1001'
]
];
echo json_encode(ResourceDocument::create($resource, [
'base_url' => 'https://api.example.com'
]));
上述示例将返回
{
"data": {
"type": "item",
"id": "1001",
"attributes": {
"key": "value"
},
"links": {
"self": "/items/1001"
}
},
"links": {
"self": "CURRENT_URL"
},
"jsonapi": {
"version": "1.0"
}
}
资源集合文档
$array
是要返回的资源数组。- 如果存在
$config['base_url']
,其值将从返回的任何链接中移除。 - 如果存在
$config['meta_results']
,它将用于返回ResourceCollectionMetaResults
和ResourceCollectionPagination
模式。
示例
use Bayfront\ArraySchema\Schemas\ResourceCollectionDocument;
$resources = [
[
'type' => 'item',
'id' => '1001',
'attributes' => [
'key' => 'value'
],
'links' => [
'self' => 'https://api.example.com/items/1001'
]
],
[
'type' => 'item',
'id' => '1002',
'attributes' => [
'key' => 'value'
],
'links' => [
'self' => 'https://api.example.com/items/1002'
]
],
[
'type' => 'item',
'id' => '1003',
'attributes' => [
'key' => 'value'
],
'links' => [
'self' => 'https://api.example.com/items/1003'
]
]
];
echo json_encode(ResourceCollectionDocument::create($resources, [
'base_url' => 'https://api.example.com',
'meta_results' => [
'count' => 3,
'total' => 10,
'limit' => 3,
'offset' => 0
]
]));
上述示例将返回
{
"data": [
{
"type": "item",
"id": "1001",
"attributes": {
"key": "value"
},
"links": {
"self": "/items/1001"
}
},
{
"type": "item",
"id": "1002",
"attributes": {
"key": "value"
},
"links": {
"self": "/items/1002"
}
},
{
"type": "item",
"id": "1003",
"attributes": {
"key": "value"
},
"links": {
"self": "/items/1003"
}
}
],
"meta": {
"results": {
"count": 3,
"total": 10,
"limit": 3,
"offset": 0
}
},
"links": {
"self": "CURRENT_URL",
"first": "CURRENT_URL",
"last": "CURRENT_URL?limit=3&offset=9",
"prev": null,
"next": "CURRENT_URL?limit=3&offset=3"
},
"jsonapi": {
"version": "1.0"
}
}
错误文档
$array
是要返回的错误数组。- 如果存在
$config['base_url']
,其值将从返回的任何链接中移除。
示例
use Bayfront\ArraySchema\Schemas\ErrorDocument;
$errors = [
[
'status' => '401',
'title' => 'Unauthorized',
'detail' => 'Invalid or missing credentials',
'code' => '0',
'links' => [
'about' => 'link to documentation'
]
]
];
echo json_encode(ErrorDocument::create($errors));
上述示例将返回
{
"errors": [
{
"status": "401",
"title": "Unauthorized",
"detail": "Invalid or missing credentials",
"code": "0",
"links": {
"about": "link to documentation"
}
}
],
"jsonapi": {
"version": "1.0"
}
}