-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
compound_doc.php
82 lines (74 loc) · 2.64 KB
/
compound_doc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
declare(strict_types=1);
use JsonApiPhp\JsonApi\Attribute;
use JsonApiPhp\JsonApi\CompoundDocument;
use JsonApiPhp\JsonApi\Included;
use JsonApiPhp\JsonApi\Link\LastLink;
use JsonApiPhp\JsonApi\Link\NextLink;
use JsonApiPhp\JsonApi\Link\RelatedLink;
use JsonApiPhp\JsonApi\Link\SelfLink;
use JsonApiPhp\JsonApi\PaginatedCollection;
use JsonApiPhp\JsonApi\Pagination;
use JsonApiPhp\JsonApi\ResourceCollection;
use JsonApiPhp\JsonApi\ResourceIdentifier;
use JsonApiPhp\JsonApi\ResourceIdentifierCollection;
use JsonApiPhp\JsonApi\ResourceObject;
use JsonApiPhp\JsonApi\ToMany;
use JsonApiPhp\JsonApi\ToOne;
require_once __DIR__.'/../vendor/autoload.php';
$dan = new ResourceObject(
'people',
'9',
new Attribute('first-name', 'Dan'),
new Attribute('last-name', 'Gebhardt'),
new Attribute('twitter', 'dgeb'),
new SelfLink('http://example.com/people/9')
);
$comment05 = new ResourceObject(
'comments',
'5',
new Attribute('body', 'First!'),
new SelfLink('http://example.com/comments/5'),
new ToOne('author', new ResourceIdentifier('people', '2'))
);
$comment12 = new ResourceObject(
'comments',
'12',
new Attribute('body', 'I like XML better'),
new SelfLink('http://example.com/comments/12'),
new ToOne('author', $dan->identifier())
);
$document = new CompoundDocument(
new PaginatedCollection(
new Pagination(
new NextLink('http://example.com/articles?page[offset]=2'),
new LastLink('http://example.com/articles?page[offset]=10')
),
new ResourceCollection(
new ResourceObject(
'articles',
'1',
new Attribute('title', 'JSON API paints my bikeshed!'),
new SelfLink('http://example.com/articles/1'),
new ToOne(
'author',
$dan->identifier(),
new SelfLink('http://example.com/articles/1/relationships/author'),
new RelatedLink('http://example.com/articles/1/author')
),
new ToMany(
'comments',
new ResourceIdentifierCollection(
$comment05->identifier(),
$comment12->identifier()
),
new SelfLink('http://example.com/articles/1/relationships/comments'),
new RelatedLink('http://example.com/articles/1/comments')
)
)
)
),
new Included($dan, $comment05, $comment12),
new SelfLink('http://example.com/articles')
);
echo json_encode($document, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);