|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace alsvanzelf\jsonapi\objects; |
|
4
|
|
|
|
|
5
|
|
|
use alsvanzelf\jsonapi\exceptions\Exception; |
|
6
|
|
|
use alsvanzelf\jsonapi\exceptions\DuplicateException; |
|
7
|
|
|
use alsvanzelf\jsonapi\helpers\AtMemberManager; |
|
8
|
|
|
use alsvanzelf\jsonapi\helpers\ExtensionMemberManager; |
|
9
|
|
|
use alsvanzelf\jsonapi\helpers\Validator; |
|
10
|
|
|
use alsvanzelf\jsonapi\interfaces\ObjectInterface; |
|
11
|
|
|
use alsvanzelf\jsonapi\interfaces\ResourceInterface; |
|
12
|
|
|
use alsvanzelf\jsonapi\objects\MetaObject; |
|
13
|
|
|
|
|
14
|
|
|
class ResourceIdentifierObject implements ObjectInterface, ResourceInterface { |
|
15
|
|
|
use AtMemberManager, ExtensionMemberManager; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
protected $type; |
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $id; |
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
protected $lid; |
|
23
|
|
|
/** @var MetaObject */ |
|
24
|
|
|
protected $meta; |
|
25
|
|
|
/** @var Validator */ |
|
26
|
|
|
protected $validator; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @note $type and $id are optional to pass during construction |
|
30
|
|
|
* however they are required for a valid ResourceIdentifierObject |
|
31
|
|
|
* so use ->setType() and ->setId() if not passing them during construction |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $type optional |
|
34
|
|
|
* @param string|int $id optional |
|
35
|
|
|
*/ |
|
36
|
93 |
|
public function __construct($type=null, $id=null) { |
|
37
|
93 |
|
$this->validator = new Validator(); |
|
38
|
|
|
|
|
39
|
93 |
|
if ($type !== null) { |
|
40
|
78 |
|
$this->setType($type); |
|
41
|
|
|
} |
|
42
|
93 |
|
if ($id !== null) { |
|
43
|
73 |
|
$this->setId($id); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// always mark as used, as these keys are reserved |
|
47
|
93 |
|
$this->validator->claimUsedFields($fieldNames=['type'], Validator::OBJECT_CONTAINER_TYPE); |
|
48
|
93 |
|
$this->validator->claimUsedFields($fieldNames=['id'], Validator::OBJECT_CONTAINER_ID); |
|
49
|
93 |
|
$this->validator->claimUsedFields($fieldNames=['lid'], Validator::OBJECT_CONTAINER_LID); |
|
50
|
93 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* human api |
|
54
|
|
|
*/ |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $key |
|
58
|
|
|
* @param mixed $value |
|
59
|
|
|
*/ |
|
60
|
6 |
|
public function addMeta($key, $value) { |
|
61
|
6 |
|
if ($this->meta === null) { |
|
62
|
6 |
|
$this->setMetaObject(new MetaObject()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
6 |
|
$this->meta->add($key, $value); |
|
66
|
6 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* spec api |
|
70
|
|
|
*/ |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $type |
|
74
|
|
|
*/ |
|
75
|
84 |
|
public function setType($type) { |
|
76
|
84 |
|
$this->type = $type; |
|
77
|
84 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string|int $id will be casted to a string |
|
81
|
|
|
* |
|
82
|
|
|
* @throws DuplicateException if localId is already set |
|
83
|
|
|
*/ |
|
84
|
77 |
|
public function setId($id) { |
|
85
|
77 |
|
if ($this->lid !== null) { |
|
86
|
1 |
|
throw new DuplicateException('id is not allowed when localId is already set'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
76 |
|
$this->id = (string) $id; |
|
90
|
76 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* set a local id to connect resources to each other when created on the client |
|
94
|
|
|
* |
|
95
|
|
|
* @note this should not be used to send back from the server to the client |
|
96
|
|
|
* |
|
97
|
|
|
* @param string|int $localId will be casted to a string |
|
98
|
|
|
* |
|
99
|
|
|
* @throws DuplicateException if normal id is already set |
|
100
|
|
|
*/ |
|
101
|
5 |
|
public function setLocalId($localId) { |
|
102
|
5 |
|
if ($this->id !== null) { |
|
103
|
1 |
|
throw new DuplicateException('localId is not allowed when id is already set'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
4 |
|
$this->lid = (string) $localId; |
|
107
|
4 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param MetaObject $metaObject |
|
111
|
|
|
*/ |
|
112
|
8 |
|
public function setMetaObject(MetaObject $metaObject) { |
|
113
|
8 |
|
$this->meta = $metaObject; |
|
114
|
8 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* internal api |
|
118
|
|
|
*/ |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @internal |
|
122
|
|
|
* |
|
123
|
|
|
* @param ResourceObject $resourceObject |
|
124
|
|
|
* @return ResourceIdentifierObject |
|
125
|
|
|
*/ |
|
126
|
36 |
|
public static function fromResourceObject(ResourceObject $resourceObject) { |
|
127
|
36 |
|
$resourceIdentifierObject = new self($resourceObject->type, $resourceObject->primaryId()); |
|
128
|
|
|
|
|
129
|
36 |
|
if ($resourceObject->meta !== null) { |
|
130
|
2 |
|
$resourceIdentifierObject->setMetaObject($resourceObject->meta); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
36 |
|
return $resourceIdentifierObject; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @internal |
|
138
|
|
|
* |
|
139
|
|
|
* @param ResourceInterface $resource |
|
140
|
|
|
* @return boolean |
|
141
|
|
|
* |
|
142
|
|
|
* @throws Exception if one or both are missing identification |
|
143
|
|
|
*/ |
|
144
|
23 |
|
public function equals(ResourceInterface $resource) { |
|
145
|
23 |
|
if ($this->hasIdentification() === false || $resource->getResource()->hasIdentification() === false) { |
|
146
|
1 |
|
throw new Exception('can not compare resources if identification is missing'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
22 |
|
return ($this->getIdentificationKey() === $resource->getResource()->getIdentificationKey()); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @internal |
|
154
|
|
|
* |
|
155
|
|
|
* @return boolean |
|
156
|
|
|
*/ |
|
157
|
45 |
|
public function hasIdentification() { |
|
158
|
45 |
|
return ($this->type !== null && $this->primaryId() !== null); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* get a key to uniquely define this resource |
|
163
|
|
|
* |
|
164
|
|
|
* @internal |
|
165
|
|
|
* |
|
166
|
|
|
* @return string |
|
167
|
|
|
* |
|
168
|
|
|
* @throws Exception if type or id is not set yet |
|
169
|
|
|
*/ |
|
170
|
42 |
|
public function getIdentificationKey() { |
|
171
|
42 |
|
if ($this->hasIdentification() === false) { |
|
172
|
2 |
|
throw new Exception('resource has no identification yet'); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
40 |
|
return $this->type.'|'.$this->primaryId(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* ObjectInterface |
|
180
|
|
|
*/ |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @inheritDoc |
|
184
|
|
|
*/ |
|
185
|
47 |
|
public function isEmpty() { |
|
186
|
47 |
|
if ($this->type !== null || $this->primaryId() !== null) { |
|
|
|
|
|
|
187
|
38 |
|
return false; |
|
188
|
|
|
} |
|
189
|
10 |
|
if ($this->meta !== null && $this->meta->isEmpty() === false) { |
|
190
|
1 |
|
return false; |
|
191
|
|
|
} |
|
192
|
9 |
|
if ($this->hasAtMembers()) { |
|
193
|
1 |
|
return false; |
|
194
|
|
|
} |
|
195
|
9 |
|
if ($this->hasExtensionMembers()) { |
|
196
|
1 |
|
return false; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
9 |
|
return true; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @inheritDoc |
|
204
|
|
|
*/ |
|
205
|
65 |
|
public function toArray() { |
|
206
|
65 |
|
$array = []; |
|
207
|
|
|
|
|
208
|
65 |
|
$array['type'] = $this->type; |
|
209
|
|
|
|
|
210
|
65 |
|
if ($this->id !== null) { |
|
211
|
57 |
|
$array['id'] = $this->id; |
|
212
|
|
|
} |
|
213
|
10 |
|
elseif ($this->lid !== null) { |
|
214
|
2 |
|
$array['lid'] = $this->lid; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
65 |
|
if ($this->hasAtMembers()) { |
|
218
|
1 |
|
$array = array_merge($array, $this->getAtMembers()); |
|
219
|
|
|
} |
|
220
|
65 |
|
if ($this->hasExtensionMembers()) { |
|
221
|
2 |
|
$array = array_merge($array, $this->getExtensionMembers()); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
65 |
|
if ($this->meta !== null && $this->meta->isEmpty() === false) { |
|
225
|
7 |
|
$array['meta'] = $this->meta->toArray(); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
65 |
|
return $array; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* ResourceInterface |
|
233
|
|
|
*/ |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @inheritDoc |
|
237
|
|
|
*/ |
|
238
|
10 |
|
public function getResource($identifierOnly=false) { |
|
239
|
10 |
|
return $this; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @internal |
|
244
|
|
|
*/ |
|
245
|
|
|
|
|
246
|
64 |
|
private function primaryId() { |
|
247
|
64 |
|
if ($this->lid !== null) { |
|
248
|
2 |
|
return $this->lid; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
62 |
|
return $this->id; |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|