carlonicora / jsonapi
用于实现 {json:api} 的 PHP 库
3.0.12
2020-05-10 00:00 UTC
Requires
- php: >=8.0
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^9
- dev-master
- 3.0.12
- 3.0.11
- 3.0.10
- 3.0.9
- 3.0.8
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.4.18
- 2.4.17
- 2.4.16
- 2.4.15
- 2.4.14
- 2.4.13
- 2.4.12
- 2.4.11
- 2.4.10
- 2.4.9
- 2.4.8
- 2.4.7
- 2.4.6
- 2.4.5
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.0
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-dev
This package is auto-updated.
Last update: 2024-09-18 19:44:29 UTC
README
JsonApi 是一个 PHP 库,用于管理 {json:api} 文档。该库还提供直接从库中管理 HTTP 响应的可能性。
安装
Composer:
composer require carlonicora/jsonapi
Git:
git clone https://github.com/carlonicora/jsonapi.git
配置
JsonApi 库不需要任何配置。
Docker
库包含一个内置的 Docker 环境。这对于测试更改和运行 PHPUnit 测试很有用。
cd .docker docker-compose build docker-compose up -d docker exec -ti jsonapi composer update
使用方法
此库围绕 {json:api} 文档 中的可识别对象组织。
文档
主要对象是 document 对象,它为您提供访问 {json:api} 文档可以包含的主要元素的方法。
use \CarloNicora\JsonApi\document; $document = new Document();
您还可以从数组中导入一个 {json:api} 文档。
use \CarloNicora\JsonApi\Document; $array = [ 'data' => [ 'type' => 'journal', 'id' => 'andsjad897asd', 'attributes' => [ 'title' => 'About phlow - the community media movement' ], 'links' => [ 'self' => 'https://app.phlow.com/@carlo/journals/about-phlow-the-community-media-movement' ], 'relationships' => [ 'author' => [ 'links' => [ 'related' => 'https://app.phlow.com/@carlo' ], 'data' => [ 'type' => 'user', 'id' => 'adslau79ulaksdu', 'meta' => [ 'isPrimaryAuthor' => true ] ] ], 'images' => [ 'data' => [ [ 'type' => 'image', 'id' => '26037dd7-481b-4110-97f3-a879a08d1e20', 'meta' => [ 'isCover' => true ] ], [ 'type' => 'image', 'id' => '2563cc0c-3202-4554-be70-3c9850d5369e', 'meta' => [ 'isCover' => false ] ] ] ] ] ], 'included' => [ [ 'type' => 'user', 'id' => 'adslau79ulaksdu', 'attributes' => [ 'name' => 'Carlo Nicora', 'username' => 'carlo', 'url' => 'https://carlonicora.com' ], 'meta' => [ 'hasJournals' => true, 'hasPhotos' => true ], 'links' => [ 'self' => 'https://app.phlow.com/@carlo' ] ], [ 'type' => 'image', 'id' => '26037dd7-481b-4110-97f3-a879a08d1e20', 'attributes' => [ 'url' => 'https://acc-phlow.imgix.net/wZaN92gl7WlRmDWrKp/26037dd7-481b-4110-97f3-a879a08d1e20.jpg?w=750&ixlib=js-1.1.0&s=28c961bf9a05855320fe853155b1cd7f' ], 'links' => [ 'self' => 'https://acc-phlow.imgix.net/wZaN92gl7WlRmDWrKp/26037dd7-481b-4110-97f3-a879a08d1e20.jpg?w=750&ixlib=js-1.1.0&s=28c961bf9a05855320fe853155b1cd7f' ] ], [ 'type' => 'image', 'id' => '2563cc0c-3202-4554-be70-3c9850d5369e', 'attributes' => [ 'url' => 'https://acc-phlow.imgix.net/wZaN92gl7WlRmDWrKp/2563cc0c-3202-4554-be70-3c9850d5369e.jpg?w=750&ixlib=js-1.1.0&s=da188c73f2b571d1afd9b1625f482e05' ], 'links' => [ 'self' => 'https://acc-phlow.imgix.net/wZaN92gl7WlRmDWrKp/2563cc0c-3202-4554-be70-3c9850d5369e.jpg?w=750&ixlib=js-1.1.0&s=da188c73f2b571d1afd9b1625f482e05' ] ] ] ]; $document = new Document($array);
资源对象
resourceObject 是 {json:api} 文档的主要数据。一个 document 对象可以包含多个 resourceObject。
use \CarloNicora\JsonApi\Objects\ResourceObject; use \CarloNicora\JsonApi\Objects\Link; $resource = new ResourceObject('journal', 'iajhd80'); $resource->attributes->add('title', 'About phlow - the community media movement'); $resource->links->add(new Link('self', 'https://app.phlow.com/@carlo/journals/about-phlow-the-community-media-movement'));
与 document 类似,可以通过传递一个数组来填充 resourceObject。
use \CarloNicora\JsonApi\Objects\ResourceObject; $array = [ 'type' => 'journal', 'id' => 'andsjad897asd', 'attributes' => [ 'title' => 'About phlow - the community media movement' ], 'links' => [ 'self' => 'https://app.phlow.com/@carlo/journals/about-phlow-the-community-media-movement' ] ]; $resource = new ResourceObject(null, null, $array);
resourceObject 可以包含多个 relationship,如 {json:api} 文档中定义的那样。
use \CarloNicora\JsonApi\Objects\ResourceObject; $resource = new ResourceObject('journal', '1'); $userResource = new ResourceObject('user', '10'); $resource->relationship('author')->resourceLinkage->add($userResource);
版本控制
此项目使用 Semantic Versioning 进行标签。
作者
贡献
请随意贡献,fork 仓库并提交 PR。
许可证
此项目在 MIT 许可证 下授权 - 有关详细信息,请参阅 LICENSE.md 文件。