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