|
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 MetaObject */ |
|
20
|
|
|
protected $meta; |
|
21
|
|
|
/** @var Validator */ |
|
22
|
|
|
protected $validator; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @note $type and $id are optional to pass during construction |
|
26
|
|
|
* however they are required for a valid ResourceIdentifierObject |
|
27
|
|
|
* so use ->setType() and ->setId() if not passing them during construction |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $type optional |
|
30
|
|
|
* @param string|int $id optional |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($type=null, $id=null) { |
|
33
|
|
|
$this->validator = new Validator(); |
|
34
|
|
|
|
|
35
|
|
|
if ($type !== null) { |
|
36
|
|
|
$this->setType($type); |
|
37
|
|
|
} |
|
38
|
|
|
if ($id !== null) { |
|
39
|
|
|
$this->setId($id); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// always mark as used, as these keys are reserved |
|
43
|
|
|
$this->validator->claimUsedFields($fieldNames=['type'], Validator::OBJECT_CONTAINER_TYPE); |
|
44
|
|
|
$this->validator->claimUsedFields($fieldNames=['id'], Validator::OBJECT_CONTAINER_ID); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* human api |
|
49
|
|
|
*/ |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $key |
|
|
|
|
|
|
53
|
|
|
* @param mixed $value |
|
|
|
|
|
|
54
|
|
|
*/ |
|
|
|
|
|
|
55
|
|
|
public function addMeta($key, $value) { |
|
56
|
|
|
if ($this->meta === null) { |
|
57
|
|
|
$this->setMetaObject(new MetaObject()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$this->meta->add($key, $value); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* spec api |
|
65
|
|
|
*/ |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $type |
|
|
|
|
|
|
69
|
|
|
*/ |
|
|
|
|
|
|
70
|
|
|
public function setType($type) { |
|
71
|
|
|
$this->type = $type; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string|int $id will be casted to a string |
|
76
|
|
|
*/ |
|
|
|
|
|
|
77
|
|
|
public function setId($id) { |
|
78
|
|
|
$this->id = (string) $id; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param MetaObject $metaObject |
|
|
|
|
|
|
83
|
|
|
*/ |
|
|
|
|
|
|
84
|
|
|
public function setMetaObject(MetaObject $metaObject) { |
|
85
|
|
|
$this->meta = $metaObject; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* internal api |
|
90
|
|
|
*/ |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @internal |
|
94
|
|
|
* |
|
95
|
|
|
* @param ResourceObject $resourceObject |
|
|
|
|
|
|
96
|
|
|
* @return ResourceIdentifierObject |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function fromResourceObject(ResourceObject $resourceObject) { |
|
99
|
|
|
$resourceIdentifierObject = new self($resourceObject->type, $resourceObject->id); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
if ($resourceObject->meta !== null) { |
|
|
|
|
|
|
102
|
|
|
$resourceIdentifierObject->setMetaObject($resourceObject->meta); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $resourceIdentifierObject; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @internal |
|
110
|
|
|
* |
|
111
|
|
|
* @param ResourceInterface $resource |
|
|
|
|
|
|
112
|
|
|
* @return boolean |
|
113
|
|
|
* |
|
114
|
|
|
* @throws Exception if one or both are missing identification |
|
115
|
|
|
*/ |
|
116
|
|
|
public function equals(ResourceInterface $resource) { |
|
117
|
|
|
if ($this->hasIdentification() === false || $resource->getResource()->hasIdentification() === false) { |
|
118
|
|
|
throw new Exception('can not compare resources if identification is missing'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return ($this->getIdentificationKey() === $resource->getResource()->getIdentificationKey()); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @internal |
|
126
|
|
|
* |
|
127
|
|
|
* @return boolean |
|
128
|
|
|
*/ |
|
129
|
|
|
public function hasIdentification() { |
|
130
|
|
|
return ($this->type !== null && $this->id !== null); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* get a key to uniquely define this resource |
|
135
|
|
|
* |
|
136
|
|
|
* @internal |
|
137
|
|
|
* |
|
138
|
|
|
* @return string |
|
139
|
|
|
* |
|
140
|
|
|
* @throws Exception if type or id is not set yet |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getIdentificationKey() { |
|
143
|
|
|
if ($this->hasIdentification() === false) { |
|
144
|
|
|
throw new Exception('resource has no identification yet'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $this->type.'|'.$this->id; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* ObjectInterface |
|
152
|
|
|
*/ |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @inheritDoc |
|
156
|
|
|
*/ |
|
|
|
|
|
|
157
|
|
View Code Duplication |
public function isEmpty() { |
|
|
|
|
|
|
158
|
|
|
if ($this->type !== null || $this->id !== null) { |
|
159
|
|
|
return false; |
|
160
|
|
|
} |
|
161
|
|
|
if ($this->meta !== null && $this->meta->isEmpty() === false) { |
|
162
|
|
|
return false; |
|
163
|
|
|
} |
|
164
|
|
|
if ($this->hasAtMembers()) { |
|
165
|
|
|
return false; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return true; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @inheritDoc |
|
173
|
|
|
*/ |
|
|
|
|
|
|
174
|
|
View Code Duplication |
public function toArray() { |
|
|
|
|
|
|
175
|
|
|
$array = $this->getAtMembers(); |
|
176
|
|
|
|
|
177
|
|
|
$array['type'] = $this->type; |
|
178
|
|
|
|
|
179
|
|
|
if ($this->id !== null) { |
|
180
|
|
|
$array['id'] = $this->id; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
if ($this->meta !== null && $this->meta->isEmpty() === false) { |
|
184
|
|
|
$array['meta'] = $this->meta->toArray(); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return $array; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* ResourceInterface |
|
192
|
|
|
*/ |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
|
|
|
|
|
195
|
|
|
* @inheritDoc |
|
196
|
|
|
*/ |
|
|
|
|
|
|
197
|
|
|
public function getResource($identifierOnly=false) { |
|
198
|
|
|
return $this; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|