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