1
|
|
|
<?php |
2
|
|
|
namespace Javis\JsonApi\Schema; |
3
|
|
|
|
4
|
|
|
class Resources |
5
|
|
|
{ |
6
|
|
|
/* |
7
|
|
|
* @var bool |
8
|
|
|
*/ |
9
|
|
|
protected $isSinglePrimaryResource; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var \Javis\JsonApi\Schema\Resource[] |
13
|
|
|
*/ |
14
|
|
|
protected $resources = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $primaryKeys = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $includedKeys = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param array $data |
28
|
|
|
* @param array $included |
29
|
|
|
*/ |
30
|
|
|
public function __construct(array $data, array $included) |
31
|
|
|
{ |
32
|
|
|
if (empty($data)) { |
33
|
|
|
$this->isSinglePrimaryResource = null; |
34
|
|
|
} else { |
35
|
|
|
$this->isSinglePrimaryResource = $this->isAssociativeArray($data) === true; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ($this->isSinglePrimaryResource === true) { |
39
|
|
|
$this->addPrimaryResource(new Resource($data, $this)); |
40
|
|
|
} else { |
41
|
|
|
foreach ($data as $resource) { |
42
|
|
|
$this->addPrimaryResource(new Resource($resource, $this)); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
foreach ($included as $resource) { |
47
|
|
|
$this->addIncludedResource(new Resource($resource, $this)); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return array|null |
53
|
|
|
*/ |
54
|
|
|
public function primaryDataToArray() |
55
|
|
|
{ |
56
|
|
|
return $this->isSinglePrimaryResource ? $this->primaryResourceToArray() : $this->primaryCollectionToArray(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function includedToArray() |
63
|
|
|
{ |
64
|
|
|
ksort($this->includedKeys); |
65
|
|
|
|
66
|
|
|
$result = []; |
67
|
|
|
foreach ($this->includedKeys as $resource) { |
68
|
|
|
/** @var \Javis\JsonApi\Schema\Resource $resource */ |
69
|
|
|
$result[] = $resource; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $result; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return array|null |
77
|
|
|
*/ |
78
|
|
|
protected function primaryResourceToArray() |
79
|
|
|
{ |
80
|
|
|
if ($this->hasPrimaryResources() === false) { |
81
|
|
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
reset($this->primaryKeys); |
85
|
|
|
$key = key($this->primaryKeys); |
86
|
|
|
|
87
|
|
|
return $this->resources[$key]->toArray(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
protected function primaryCollectionToArray() |
94
|
|
|
{ |
95
|
|
|
$result = []; |
96
|
|
|
foreach ($this->primaryKeys as $resource) { |
97
|
|
|
/** @var \Javis\JsonApi\Schema\Resource $resource */ |
98
|
|
|
$result[] = $resource->toArray(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function isSinglePrimaryResource() |
108
|
|
|
{ |
109
|
|
|
return $this->isSinglePrimaryResource === true; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
public function isPrimaryResourceCollection() |
116
|
|
|
{ |
117
|
|
|
return $this->isSinglePrimaryResource === false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $type |
122
|
|
|
* @param string $id |
123
|
|
|
* @return \Javis\JsonApi\Schema\Resource|null |
124
|
|
|
*/ |
125
|
|
|
public function getResource($type, $id) |
126
|
|
|
{ |
127
|
|
|
return isset($this->resources[$type . "." . $id]) ? $this->resources[$type . "." . $id] : null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function hasPrimaryResources() |
134
|
|
|
{ |
135
|
|
|
return empty($this->primaryKeys) === false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return \Javis\JsonApi\Schema\Resource[] |
140
|
|
|
*/ |
141
|
|
|
public function getPrimaryResources() |
142
|
|
|
{ |
143
|
|
|
return array_values($this->primaryKeys); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return \Javis\JsonApi\Schema\Resource|null |
148
|
|
|
*/ |
149
|
|
|
public function getPrimaryResource() |
150
|
|
|
{ |
151
|
|
|
if ($this->hasPrimaryResources() === false) { |
152
|
|
|
return null; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
reset($this->primaryKeys); |
156
|
|
|
$key = key($this->primaryKeys); |
157
|
|
|
|
158
|
|
|
return $this->resources[$key]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $type |
163
|
|
|
* @param string $id |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
|
|
public function hasPrimaryResource($type, $id) |
167
|
|
|
{ |
168
|
|
|
return isset($this->primaryKeys[$type . "." . $id]); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
|
|
public function hasIncludedResources() |
175
|
|
|
{ |
176
|
|
|
return empty($this->includedKeys) === false; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $type |
181
|
|
|
* @param string $id |
182
|
|
|
* @return bool |
183
|
|
|
*/ |
184
|
|
|
public function hasIncludedResource($type, $id) |
185
|
|
|
{ |
186
|
|
|
return isset($this->includedKeys[$type . "." . $id]); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return \Javis\JsonApi\Schema\Resource[] |
191
|
|
|
*/ |
192
|
|
|
public function getIncludedResources() |
193
|
|
|
{ |
194
|
|
|
return array_values($this->includedKeys); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param \Javis\JsonApi\Schema\Resource $resource |
199
|
|
|
* @return $this |
200
|
|
|
*/ |
201
|
|
|
protected function addPrimaryResource(Resource $resource) |
202
|
|
|
{ |
203
|
|
|
$type = $resource->type(); |
204
|
|
|
$id = $resource->id(); |
205
|
|
|
if ($this->hasIncludedResource($type, $id) === true) { |
206
|
|
|
unset($this->includedKeys[$type . "." . $id]); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$this->addResource($this->primaryKeys, $resource); |
210
|
|
|
|
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param \Javis\JsonApi\Schema\Resource $resource |
216
|
|
|
* @return $this |
217
|
|
|
*/ |
218
|
|
|
protected function addIncludedResource(Resource $resource) |
219
|
|
|
{ |
220
|
|
|
if ($this->hasPrimaryResource($resource->type(), $resource->id()) === false) { |
221
|
|
|
$this->addResource($this->includedKeys, $resource); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
protected function addResource(&$keys, Resource $resource) |
228
|
|
|
{ |
229
|
|
|
$type = $resource->type(); |
230
|
|
|
$id = $resource->id(); |
231
|
|
|
|
232
|
|
|
$this->resources[$type . "." . $id] = $resource; |
233
|
|
|
$keys[$type . "." . $id] = &$this->resources[$type . "." . $id]; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param array $array |
238
|
|
|
* @return bool |
239
|
|
|
*/ |
240
|
|
|
private function isAssociativeArray(array $array) |
241
|
|
|
{ |
242
|
|
|
return (bool)count(array_filter(array_keys($array), 'is_string')); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|