json-api-php / json-api
JSON API specs (jsonapi.org) as a set of PHP classes
Fund package maintenance!
www.paypal.me/f3ath
Installs: 262 728
Dependents: 2
Suggesters: 0
Security: 0
Stars: 183
Watchers: 9
Forks: 18
Open Issues: 3
Requires
- php: >=8.1
- ext-json: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.9
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-10-27 06:13:03 UTC
README
The goal of this library is to ensure strict validity of JSON API documents being produced.
JSON:
{ "data": { "type": "articles", "id": "1", "attributes": { "title": "Rails is Omakase" }, "relationships": { "author": { "data": { "type": "people", "id": "9" }, "links": { "self": "/articles/1/relationships/author", "related": "/articles/1/author" } } } } }
PHP:
<?php use JsonApiPhp\JsonApi\Attribute; use JsonApiPhp\JsonApi\DataDocument; use JsonApiPhp\JsonApi\Link\RelatedLink; use JsonApiPhp\JsonApi\Link\SelfLink; use JsonApiPhp\JsonApi\ResourceIdentifier; use JsonApiPhp\JsonApi\ResourceObject; use JsonApiPhp\JsonApi\ToOne; echo json_encode( new DataDocument( new ResourceObject( 'articles', '1', new Attribute('title', 'Rails is Omakase'), new ToOne( 'author', new ResourceIdentifier('author', '9'), new SelfLink('/articles/1/relationships/author'), new RelatedLink('/articles/1/author') ) ) ), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES );
Installation
composer require json-api-php/json-api
Documentation
First, take a look at the examples. All of them are runnable.
- Simple Document (the same as above)
- Extensive Compound Document
The library API and use-cases are expressed in a comprehensive suite of tests.
- Data Documents (containing primary data)
- Compound Documents
- Error Documents
- Meta Documents (containing neither data nor errors)
- Pagination
- Link Objects
- JSON API Object
- Meta Objects