1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace alsvanzelf\jsonapi; |
4
|
|
|
|
5
|
|
|
use alsvanzelf\jsonapi\DataDocument; |
6
|
|
|
use alsvanzelf\jsonapi\Document; |
7
|
|
|
use alsvanzelf\jsonapi\exceptions\InputException; |
8
|
|
|
use alsvanzelf\jsonapi\interfaces\PaginableInterface; |
9
|
|
|
use alsvanzelf\jsonapi\interfaces\RecursiveResourceContainerInterface; |
10
|
|
|
use alsvanzelf\jsonapi\interfaces\ResourceContainerInterface; |
11
|
|
|
use alsvanzelf\jsonapi\interfaces\ResourceInterface; |
12
|
|
|
use alsvanzelf\jsonapi\objects\ResourceObject; |
13
|
|
|
use alsvanzelf\jsonapi\objects\ResourceIdentifierObject; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* this document is a set of Resources |
17
|
|
|
* this document should be used if there could be multiple, also if only one or even none is returned |
18
|
|
|
*/ |
|
|
|
|
19
|
|
|
class CollectionDocument extends DataDocument implements PaginableInterface, ResourceContainerInterface { |
20
|
|
|
/** @var ResourceInterface[] */ |
21
|
|
|
protected $resources = []; |
22
|
|
|
/** @var array */ |
23
|
|
|
protected static $defaults = [ |
24
|
|
|
/** |
25
|
|
|
* add resources inside relationships to /included when adding resources to the collection |
26
|
|
|
*/ |
27
|
|
|
'includeContainedResources' => true, |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* human api |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* generate a CollectionDocument from one or multiple resources |
36
|
|
|
* |
37
|
|
|
* adds included resources if found inside the resource's relationships, use {@see ->addResource()} to change that behavior |
38
|
|
|
* |
39
|
|
|
* @param ResourceInterface ...$resources |
|
|
|
|
40
|
|
|
* @return CollectionDocument |
41
|
|
|
*/ |
42
|
|
|
public static function fromResources(ResourceInterface ...$resources) { |
43
|
|
|
$collectionDocument = new self(); |
44
|
|
|
|
45
|
|
|
foreach ($resources as $resource) { |
46
|
|
|
$collectionDocument->addResource($resource); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $collectionDocument; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $type |
|
|
|
|
54
|
|
|
* @param string|int $id |
|
|
|
|
55
|
|
|
* @param array $attributes optional, if given a ResourceObject is added, otherwise a ResourceIdentifierObject is added |
56
|
|
|
*/ |
|
|
|
|
57
|
|
|
public function add($type, $id, array $attributes=[]) { |
58
|
|
|
if ($attributes === []) { |
59
|
|
|
$this->addResource(new ResourceIdentifierObject($type, $id)); |
60
|
|
|
} |
61
|
|
|
else { |
62
|
|
|
$this->addResource(ResourceObject::fromArray($attributes, $type, $id)); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
|
|
|
|
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
|
|
|
|
69
|
|
|
public function setPaginationLinks($previousHref=null, $nextHref=null, $firstHref=null, $lastHref=null) { |
70
|
|
|
if ($previousHref !== null) { |
71
|
|
|
$this->addLink('prev', $previousHref); |
72
|
|
|
} |
73
|
|
|
if ($nextHref !== null) { |
74
|
|
|
$this->addLink('next', $nextHref); |
75
|
|
|
} |
76
|
|
|
if ($firstHref !== null) { |
77
|
|
|
$this->addLink('first', $firstHref); |
78
|
|
|
} |
79
|
|
|
if ($lastHref !== null) { |
80
|
|
|
$this->addLink('last', $lastHref); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* spec api |
86
|
|
|
*/ |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* add a resource to the collection |
90
|
|
|
* |
91
|
|
|
* adds included resources if found inside the resource's relationships, unless $options['includeContainedResources'] is set to false |
92
|
|
|
* |
93
|
|
|
* @param ResourceInterface $resource |
|
|
|
|
94
|
|
|
* @param array $options optional {@see CollectionDocument::$defaults} |
95
|
|
|
* |
96
|
|
|
* @throws InputException if the resource is empty |
97
|
|
|
*/ |
|
|
|
|
98
|
|
|
public function addResource(ResourceInterface $resource, array $options=[]) { |
99
|
|
|
if ($resource->getResource()->isEmpty()) { |
100
|
|
|
throw new InputException('does not make sense to add empty resources to a collection'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$options = array_merge(self::$defaults, $options); |
104
|
|
|
|
105
|
|
|
$this->validator->claimUsedResourceIdentifier($resource); |
106
|
|
|
|
107
|
|
|
$this->resources[] = $resource; |
108
|
|
|
|
109
|
|
|
if ($options['includeContainedResources'] && $resource instanceof RecursiveResourceContainerInterface) { |
110
|
|
|
$this->addIncludedResourceObject(...$resource->getNestedContainedResourceObjects()); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* DocumentInterface |
116
|
|
|
*/ |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritDoc |
120
|
|
|
*/ |
|
|
|
|
121
|
|
|
public function toArray() { |
122
|
|
|
$array = parent::toArray(); |
123
|
|
|
|
124
|
|
|
$array['data'] = []; |
125
|
|
|
foreach ($this->resources as $resource) { |
126
|
|
|
$array['data'][] = $resource->getResource()->toArray(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $array; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* ResourceContainerInterface |
134
|
|
|
*/ |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @inheritDoc |
138
|
|
|
*/ |
|
|
|
|
139
|
|
|
public function getContainedResources() { |
140
|
|
|
return $this->resources; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|