Skip to content

Commit 76a5404

Browse files
authored
Update index.md
1 parent 20677ad commit 76a5404

File tree

1 file changed

+44
-168
lines changed

1 file changed

+44
-168
lines changed

index.md

Lines changed: 44 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,184 +1,60 @@
1-
[JSON API spec](http://jsonapi.org/format/) :: [GitHub](https://github.com/json-api-php/json-api)
1+
# [JSON API](http://jsonapi.org) spec implemented in PHP 7. Immutable
22

3-
# JSON API in PHP 7
3+
The goal of this library is to ensure strict validity of JSON API documents being produced.
44

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

0 commit comments

Comments
 (0)