|
| 1 | +[JSON API spec](http://jsonapi.org/format/) | [GitHub](https://github.com/json-api-php/json-api) |
| 2 | +# JSON API in PHP 7 |
| 3 | + |
| 4 | +This library is an attempt to express business rules of [JSON API v1.0](http://jsonapi.org/format/) |
| 5 | +specification in a set of PHP 7 classes. We adhere to the following concepts: |
| 6 | +- Test-first development |
| 7 | +- Tests as documentation |
| 8 | +- OOP (as much as PHP supports it), we respect SOLID principles |
| 9 | + |
| 10 | +## Example |
| 11 | + |
| 12 | +This JSON response... |
| 13 | +```json |
| 14 | +{ |
| 15 | + "data": [ |
| 16 | + { |
| 17 | + "type": "articles", |
| 18 | + "id": "1", |
| 19 | + "attributes": { |
| 20 | + "title": "JSON API paints my bikeshed!" |
| 21 | + }, |
| 22 | + "relationships": { |
| 23 | + "author": { |
| 24 | + "data": { |
| 25 | + "type": "people", |
| 26 | + "id": "9" |
| 27 | + }, |
| 28 | + "links": { |
| 29 | + "self": "http://example.com/articles/1/relationships/author", |
| 30 | + "related": "http://example.com/articles/1/author" |
| 31 | + } |
| 32 | + }, |
| 33 | + "comments": { |
| 34 | + "data": [ |
| 35 | + { |
| 36 | + "type": "comments", |
| 37 | + "id": "5" |
| 38 | + }, |
| 39 | + { |
| 40 | + "type": "comments", |
| 41 | + "id": "12" |
| 42 | + } |
| 43 | + ], |
| 44 | + "links": { |
| 45 | + "self": "http://example.com/articles/1/relationships/comments", |
| 46 | + "related": "http://example.com/articles/1/comments" |
| 47 | + } |
| 48 | + } |
| 49 | + }, |
| 50 | + "links": { |
| 51 | + "self": "http://example.com/articles/1" |
| 52 | + } |
| 53 | + } |
| 54 | + ], |
| 55 | + "links": { |
| 56 | + "self": "http://example.com/articles", |
| 57 | + "next": "http://example.com/articles?page[offset]=2", |
| 58 | + "last": "http://example.com/articles?page[offset]=10" |
| 59 | + }, |
| 60 | + "included": [ |
| 61 | + { |
| 62 | + "type": "people", |
| 63 | + "id": "9", |
| 64 | + "attributes": { |
| 65 | + "first-name": "Dan", |
| 66 | + "last-name": "Gebhardt", |
| 67 | + "twitter": "dgeb" |
| 68 | + }, |
| 69 | + "links": { |
| 70 | + "self": "http://example.com/people/9" |
| 71 | + } |
| 72 | + }, |
| 73 | + { |
| 74 | + "type": "comments", |
| 75 | + "id": "5", |
| 76 | + "attributes": { |
| 77 | + "body": "First!" |
| 78 | + }, |
| 79 | + "relationships": { |
| 80 | + "author": { |
| 81 | + "data": { |
| 82 | + "type": "people", |
| 83 | + "id": "2" |
| 84 | + } |
| 85 | + } |
| 86 | + }, |
| 87 | + "links": { |
| 88 | + "self": "http://example.com/comments/5" |
| 89 | + } |
| 90 | + }, |
| 91 | + { |
| 92 | + "type": "comments", |
| 93 | + "id": "12", |
| 94 | + "attributes": { |
| 95 | + "body": "I like XML better" |
| 96 | + }, |
| 97 | + "relationships": { |
| 98 | + "author": { |
| 99 | + "data": { |
| 100 | + "type": "people", |
| 101 | + "id": "9" |
| 102 | + } |
| 103 | + } |
| 104 | + }, |
| 105 | + "links": { |
| 106 | + "self": "http://example.com/comments/12" |
| 107 | + } |
| 108 | + } |
| 109 | + ] |
| 110 | +} |
| 111 | +``` |
| 112 | +...can be build with this PHP code |
| 113 | +```php |
| 114 | +<?php |
| 115 | +require_once __DIR__ . '/vendor/autoload.php'; |
| 116 | + |
| 117 | +use JsonApiPhp\JsonApi\Document; |
| 118 | +use JsonApiPhp\JsonApi\Document\Resource\Linkage\MultiLinkage; |
| 119 | +use JsonApiPhp\JsonApi\Document\Resource\Linkage\SingleLinkage; |
| 120 | +use JsonApiPhp\JsonApi\Document\Resource\Relationship; |
| 121 | +use JsonApiPhp\JsonApi\Document\Resource\ResourceIdentifier; |
| 122 | +use JsonApiPhp\JsonApi\Document\Resource\ResourceObject; |
| 123 | + |
| 124 | + |
| 125 | +$dan = new ResourceObject('people', '9'); |
| 126 | +$dan->setAttribute('first-name', 'Dan'); |
| 127 | +$dan->setAttribute('last-name', 'Gebhardt'); |
| 128 | +$dan->setAttribute('twitter', 'dgeb'); |
| 129 | +$dan->setLink('self', 'http://example.com/people/9'); |
| 130 | + |
| 131 | +$comment05 = new ResourceObject('comments', '5'); |
| 132 | +$comment05->setAttribute('body', 'First!'); |
| 133 | +$comment05->setLink('self', 'http://example.com/comments/5'); |
| 134 | +$comment05->setRelationship( |
| 135 | + 'author', |
| 136 | + Relationship::fromLinkage(new SingleLinkage(new ResourceIdentifier('people', '2'))) |
| 137 | +); |
| 138 | + |
| 139 | +$comment12 = new ResourceObject('comments', '12'); |
| 140 | +$comment12->setAttribute('body', 'I like XML better'); |
| 141 | +$comment12->setLink('self', 'http://example.com/comments/12'); |
| 142 | +$comment12->setRelationship( |
| 143 | + 'author', |
| 144 | + Relationship::fromLinkage(new SingleLinkage($dan->toIdentifier())) |
| 145 | +); |
| 146 | + |
| 147 | +$author = Relationship::fromLinkage(new SingleLinkage($dan->toIdentifier())); |
| 148 | +$author->setLink('self', 'http://example.com/articles/1/relationships/author'); |
| 149 | +$author->setLink('related', 'http://example.com/articles/1/author'); |
| 150 | + |
| 151 | +$comments = Relationship::fromLinkage(new MultiLinkage($comment05->toIdentifier(), $comment12->toIdentifier())); |
| 152 | +$comments->setLink('self', 'http://example.com/articles/1/relationships/comments'); |
| 153 | +$comments->setLink('related', 'http://example.com/articles/1/comments'); |
| 154 | + |
| 155 | +$article = new ResourceObject('articles', '1'); |
| 156 | +$article->setAttribute('title', 'JSON API paints my bikeshed!'); |
| 157 | +$article->setLink('self', 'http://example.com/articles/1'); |
| 158 | +$article->setRelationship('author', $author); |
| 159 | +$article->setRelationship('comments', $comments); |
| 160 | + |
| 161 | +$doc = Document::fromResources($article); |
| 162 | +$doc->setIncluded($dan, $comment05, $comment12); |
| 163 | +$doc->setLink('self', 'http://example.com/articles'); |
| 164 | +$doc->setLink('next', 'http://example.com/articles?page[offset]=2'); |
| 165 | +$doc->setLink('last', 'http://example.com/articles?page[offset]=10'); |
| 166 | + |
| 167 | +echo json_encode($doc, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
| 168 | +``` |
| 169 | +## API |
| 170 | +Please refer to [the tests](https://github.com/json-api-php/json-api/tree/master/test) for the full API documentation: |
| 171 | +* [Documents](https://github.com/json-api-php/json-api/tree/master/test/Document/DocumentTest.php). Creating documents with primary data, errors, and meta. |
| 172 | +Adding links and API version to a document. |
| 173 | + * [Compound Documents](https://github.com/json-api-php/json-api/tree/master/test/Document/CompoundDocumentTest.php). Resource linkage. |
| 174 | +* [Errors](https://github.com/json-api-php/json-api/tree/master/test/Document/ErrorTest.php) |
| 175 | +* [Resources](https://github.com/json-api-php/json-api/tree/master/test/Document/Resource/ResourceTest.php) |
| 176 | +* [Relationships](https://github.com/json-api-php/json-api/tree/master/test/Document/Resource/Relationship/RelationshipTest.php) |
| 177 | +* [Linkage](https://github.com/json-api-php/json-api/tree/master/test/Document/Resource/Relationship/LinkageTest.php) |
| 178 | +## Installation |
| 179 | +`composer require json-api-php/json-api` |
0 commit comments