1- # Implementation of [ JSON API] ( http://jsonapi.org ) in PHP 7
2- This library is an attempt to express business rules of JSON API specification in a set of PHP 7 classes.
1+ # [ JSON API] ( http://jsonapi.org ) spec implemented in PHP 7. Immutable
32
4- A simple example to illustrate the general idea. This JSON representation from
5- [ the documentation] ( http://jsonapi.org/format/#document-resource-objects )
6- <!-- name=my_json -->
3+ ** This is v2 of the implementation. For v1 click [ here] ( /json-api-php/json-api/tree/v2 ) .**
4+
5+ The goal of this library is to ensure strict validity of JSON API documents being produced.
6+
7+ JSON:
78``` json
89{
910 "data" : {
@@ -27,32 +28,56 @@ A simple example to illustrate the general idea. This JSON representation from
2728 }
2829}
2930```
30- can be built with the following php code:
31- <!-- assert=output expect=my_json -->
31+ PHP:
3232``` php
3333<?php
34- use \JsonApiPhp\JsonApi\Document;
35- use \JsonApiPhp\JsonApi\Document\Resource\{
36- Linkage\SingleLinkage, Relationship, ResourceIdentifier, ResourceObject
37- };
34+ use JsonApiPhp\JsonApi\Attribute;
35+ use JsonApiPhp\JsonApi\DataDocument;
36+ use JsonApiPhp\JsonApi\Link\RelatedLink;
37+ use JsonApiPhp\JsonApi\Link\SelfLink;
38+ use JsonApiPhp\JsonApi\Link\Url;
39+ use JsonApiPhp\JsonApi\Relationship;
40+ use JsonApiPhp\JsonApi\ResourceIdentifier;
41+ use JsonApiPhp\JsonApi\ResourceObject;
42+ use JsonApiPhp\JsonApi\SingleLinkage;
3843
39- $author = Relationship::fromLinkage(new SingleLinkage(new ResourceIdentifier('people', '9')));
40- $author->setLink('self', '/articles/1/relationships/author');
41- $author->setLink('related', '/articles/1/author');
42- $articles = new ResourceObject('articles', '1');
43- $articles->setRelationship('author', $author);
44- $articles->setAttribute('title', 'Rails is Omakase');
45- echo json_encode(Document::fromResource($articles), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
44+ echo json_encode(
45+ new DataDocument(
46+ new ResourceObject(
47+ 'articles',
48+ '1',
49+ new Attribute('title', 'Rails is Omakase'),
50+ new Relationship(
51+ 'author',
52+ new SingleLinkage(new ResourceIdentifier('author', '9')),
53+ new SelfLink(new Url('/articles/1/relationships/author')),
54+ new RelatedLink(new Url('/articles/1/author'))
55+ )
56+ )
57+ ),
58+ JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
59+ );
4660```
61+ ## Installation
62+ ` composer require json-api-php/json-api `
4763
48- Please refer to [ the tests] ( ./test ) for the full API documentation:
49- * [ Documents] ( ./test/Document/DocumentTest.php ) . Creating documents with primary data, errors, and meta.
50- Adding links and API version to a document.
51- * [ Compound Documents] ( ./test/Document/CompoundDocumentTest.php ) . Resource linkage.
52- * [ Errors] ( ./test/Document/ErrorTest.php )
53- * [ Resources] ( ./test/Document/Resource/ResourceTest.php )
54- * [ Relationships] ( ./test/Document/Resource/Relationship/RelationshipTest.php )
55- * [ Linkage] ( ./test/Document/Resource/Relationship/LinkageTest.php )
64+ ## Documentation
5665
57- ## Installation
58- With [ composer] ( https://getcomposer.org/ ) : ` json-api-php/json-api ` .
66+ First, take a look at the examples. All of them are runnable.
67+ - [ Simple Document] ( ./examples/simple_doc.php ) (the same as above)
68+ - [ Extensive Compound Document] ( ./examples/compound_doc.php )
69+
70+ The library API and use-cases are expressed in comprehensive suite of tests.
71+ - Data Documents (containing primary data)
72+ - [ with a single Resource Object] ( ./test/DataDocument/SingleResourceObjectTest.php )
73+ - [ with a single Resource Identifier] ( ./test/DataDocument/SingleResourceIdentifierTest.php )
74+ - [ with null data] ( ./test/DataDocument/NullDataTest.php )
75+ - [ with multiple Resource Objects] ( ./test/DataDocument/ManyResourceObjectsTest.php )
76+ - [ with multiple Resource Identifiers] ( ./test/DataDocument/ManyResourceIdentifiersTest.php )
77+ - [ Compound Documents] ( ./test/CompoundDocumentTest.php )
78+ - [ Error Documents] ( ./test/ErrorDocumentTest.php )
79+ - [ Meta Documents (containing neither data nor errors)] ( ./test/MetaDocumentTest.php )
80+ - [ Pagination links] ( ./test/PaginationLinksTest.php )
81+ - [ Link Objects] ( ./test/LinkObjectTest.php )
82+ - [ JSON API Object] ( ./test/JsonApiTest.php )
83+ - [ Meta Objects] ( ./test/MetaTest.php )
0 commit comments