1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jayS-de <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Commercetools\Core\Model\Common; |
7
|
|
|
|
8
|
|
|
use Commercetools\Core\Error\Message; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @package Commercetools\Core\Model\Common |
12
|
|
|
*/ |
13
|
|
|
class Collection extends AbstractJsonDeserializeObject implements \Iterator, \JsonSerializable, \Countable, \ArrayAccess |
14
|
|
|
{ |
15
|
|
|
use ContextTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $type; |
21
|
|
|
|
22
|
|
|
protected $pos = 0; |
23
|
|
|
|
24
|
|
|
protected $keys = []; |
25
|
|
|
|
26
|
|
|
protected $index = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param array $data |
30
|
|
|
* @param Context|callable $context |
31
|
|
|
*/ |
32
|
157 |
|
final public function __construct(array $data = [], $context = null) |
33
|
|
|
{ |
34
|
157 |
|
parent::__construct($data, $context); |
35
|
157 |
|
$this->indexData(); |
36
|
157 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
92 |
|
public function getType() |
42
|
|
|
{ |
43
|
92 |
|
return $this->type; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $type |
48
|
|
|
* @internal |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
18 |
|
public function setType($type) |
52
|
|
|
{ |
53
|
18 |
|
$this->type = $type; |
54
|
|
|
|
55
|
18 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
157 |
|
protected function indexData() |
59
|
|
|
{ |
60
|
157 |
|
$this->keys = array_keys($this->rawData); |
61
|
157 |
|
foreach ($this->rawData as $offset => $row) { |
62
|
100 |
|
$this->indexRow($offset, $row); |
63
|
157 |
|
} |
64
|
157 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param array $rawData |
68
|
|
|
* @internal |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
6 |
|
public function setRawData(array $rawData) |
72
|
|
|
{ |
73
|
6 |
|
parent::setRawData($rawData); |
74
|
6 |
|
$this->indexData(); |
75
|
|
|
|
76
|
6 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $offset |
81
|
|
|
* @param $row |
82
|
|
|
*/ |
83
|
75 |
|
protected function indexRow($offset, $row) |
84
|
|
|
{ |
85
|
75 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $indexName |
89
|
|
|
* @param int $offset |
90
|
|
|
* @param $key |
91
|
|
|
*/ |
92
|
51 |
|
protected function addToIndex($indexName, $offset, $key) |
93
|
|
|
{ |
94
|
51 |
|
$this->index[$indexName][$key] = $offset; |
95
|
51 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $indexName |
99
|
|
|
* @param $key |
100
|
|
|
* @return mixed|null |
101
|
|
|
*/ |
102
|
27 |
|
public function getBy($indexName, $key) |
103
|
|
|
{ |
104
|
27 |
|
if (isset($this->index[$indexName][$key])) { |
105
|
25 |
|
$key = $this->index[$indexName][$key]; |
106
|
|
|
|
107
|
25 |
|
return $this->getAt($key); |
108
|
|
|
} |
109
|
2 |
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
32 |
|
public function add($object) |
113
|
|
|
{ |
114
|
32 |
|
$this->setAt(null, $object); |
115
|
|
|
|
116
|
31 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $offset |
121
|
|
|
* @internal |
122
|
|
|
*/ |
123
|
66 |
|
protected function initialize($offset) |
124
|
|
|
{ |
125
|
66 |
|
$type = $this->getType(); |
126
|
66 |
|
if ($this->isDeserializableType($type)) { |
127
|
|
|
/** |
128
|
|
|
* @var JsonDeserializeInterface $type |
129
|
|
|
*/ |
130
|
65 |
|
$value = $type::fromArray($this->getRaw($offset), $this->getContextCallback()); |
131
|
65 |
|
if ($value instanceof ObjectTreeInterface) { |
132
|
65 |
|
$value->parentSet($this); |
133
|
65 |
|
$value->rootSet($this->rootGet()); |
134
|
65 |
|
} |
135
|
65 |
|
$this->typeData[$offset] = $value; |
136
|
65 |
|
} |
137
|
66 |
|
$this->initialized[$offset] = true; |
138
|
66 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
18 |
|
public function toArray() |
144
|
|
|
{ |
145
|
18 |
|
$values = []; |
146
|
18 |
|
foreach ($this->typeData as $key => $value) { |
147
|
18 |
|
if ($value instanceof JsonDeserializeInterface) { |
148
|
18 |
|
$values[$key] = $value->toArray(); |
149
|
18 |
|
} else { |
150
|
1 |
|
$values[$key] = $value; |
151
|
|
|
} |
152
|
18 |
|
} |
153
|
|
|
|
154
|
18 |
|
return $values; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param $offset |
159
|
|
|
* @return mixed |
160
|
|
|
*/ |
161
|
81 |
|
public function getAt($offset) |
162
|
|
|
{ |
163
|
81 |
|
return $this->getTyped($offset); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param $offset |
168
|
|
|
* @param mixed $default |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
65 |
|
protected function getRaw($offset, $default = []) |
172
|
|
|
{ |
173
|
65 |
|
return parent::getRaw($offset, $default); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param $offset |
178
|
|
|
* @param $object |
179
|
|
|
* @return $this |
180
|
|
|
*/ |
181
|
36 |
|
public function setAt($offset, $object) |
182
|
|
|
{ |
183
|
36 |
|
$type = $this->getType(); |
184
|
36 |
|
if (!$this->isValidType($type, $object)) { |
185
|
1 |
|
throw new \InvalidArgumentException(sprintf(Message::WRONG_TYPE, $offset, $type)); |
186
|
|
|
} |
187
|
|
|
|
188
|
35 |
|
if ($object instanceof ContextAwareInterface) { |
189
|
31 |
|
$object->setContext($this->getContextCallback()); |
190
|
31 |
|
} |
191
|
35 |
|
if ($object instanceof ObjectTreeInterface) { |
192
|
31 |
|
$object->parentSet($this); |
193
|
31 |
|
$object->rootSet($this->rootGet()); |
194
|
31 |
|
} |
195
|
35 |
|
if (is_null($offset)) { |
196
|
33 |
|
$this->typeData[] = $object; |
197
|
33 |
|
$offset = count($this->typeData) - 1; |
198
|
33 |
|
} else { |
199
|
2 |
|
$this->typeData[$offset] = $object; |
200
|
|
|
} |
201
|
35 |
|
$this->initialized[$offset] = true; |
202
|
35 |
|
if (!in_array($offset, $this->keys)) { |
203
|
35 |
|
$this->keys[] = $offset; |
204
|
35 |
|
} |
205
|
35 |
|
$this->indexRow($offset, $object); |
206
|
|
|
|
207
|
35 |
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* (PHP 5 >= 5.1.0)<br/> |
212
|
|
|
* Count elements of an object |
213
|
|
|
* @link http://php.net/manual/en/countable.count.php |
214
|
|
|
* @return int The custom count as an integer. |
215
|
|
|
* </p> |
216
|
|
|
* <p> |
217
|
|
|
* The return value is cast to an integer. |
218
|
|
|
*/ |
219
|
53 |
|
public function count() |
220
|
|
|
{ |
221
|
53 |
|
$rawKeys = array_keys($this->rawData); |
222
|
53 |
|
$typeKeys = array_keys($this->typeData); |
223
|
53 |
|
$keys = array_merge($rawKeys, $typeKeys); |
224
|
53 |
|
$uniqueKeys = array_unique($keys); |
225
|
53 |
|
return count($uniqueKeys); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
231
|
|
|
* Return the current element |
232
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
233
|
|
|
* @return mixed Can return any type. |
234
|
|
|
*/ |
235
|
17 |
|
public function current() |
236
|
|
|
{ |
237
|
17 |
|
if (isset($this->keys[$this->pos])) { |
238
|
17 |
|
return $this->getAt($this->keys[$this->pos]); |
239
|
|
|
} |
240
|
|
|
return null; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
245
|
|
|
* Move forward to next element |
246
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
247
|
|
|
* @return void Any returned value is ignored. |
248
|
|
|
*/ |
249
|
15 |
|
public function next() |
250
|
|
|
{ |
251
|
15 |
|
$this->pos++; |
252
|
15 |
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
256
|
|
|
* Return the key of the current element |
257
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
258
|
|
|
* @return mixed scalar on success, or null on failure. |
259
|
|
|
*/ |
260
|
4 |
|
public function key() |
261
|
|
|
{ |
262
|
4 |
|
if (isset($this->keys[$this->pos])) { |
263
|
4 |
|
return $this->keys[$this->pos]; |
264
|
|
|
} |
265
|
|
|
return null; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
270
|
|
|
* Checks if current position is valid |
271
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
272
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
273
|
|
|
* Returns true on success or false on failure. |
274
|
|
|
*/ |
275
|
16 |
|
public function valid() |
276
|
|
|
{ |
277
|
16 |
|
if (isset($this->keys[$this->pos])) { |
278
|
15 |
|
return $this->offsetExists($this->keys[$this->pos]); |
279
|
|
|
} |
280
|
16 |
|
return false; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
285
|
|
|
* Rewind the Iterator to the first element |
286
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
287
|
|
|
* @return void Any returned value is ignored. |
288
|
|
|
*/ |
289
|
16 |
|
public function rewind() |
290
|
|
|
{ |
291
|
16 |
|
$this->pos = 0; |
292
|
16 |
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
296
|
|
|
* Whether a offset exists |
297
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
298
|
|
|
* @param mixed $offset <p> |
299
|
|
|
* An offset to check for. |
300
|
|
|
* </p> |
301
|
|
|
* @return boolean true on success or false on failure. |
302
|
|
|
* </p> |
303
|
|
|
* <p> |
304
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
305
|
|
|
*/ |
306
|
19 |
|
public function offsetExists($offset) |
307
|
|
|
{ |
308
|
19 |
|
return isset($this->rawData[$offset]) || isset($this->typeData[$offset]); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
313
|
|
|
* Offset to retrieve |
314
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
315
|
|
|
* @param mixed $offset <p> |
316
|
|
|
* The offset to retrieve. |
317
|
|
|
* </p> |
318
|
|
|
* @return mixed Can return all value types. |
319
|
|
|
*/ |
320
|
5 |
|
public function offsetGet($offset) |
321
|
|
|
{ |
322
|
5 |
|
return $this->getAt($offset); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
327
|
|
|
* Offset to set |
328
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
329
|
|
|
* @param mixed $offset <p> |
330
|
|
|
* The offset to assign the value to. |
331
|
|
|
* </p> |
332
|
|
|
* @param mixed $value <p> |
333
|
|
|
* The value to set. |
334
|
|
|
* </p> |
335
|
|
|
* @return void |
336
|
|
|
*/ |
337
|
3 |
|
public function offsetSet($offset, $value) |
338
|
|
|
{ |
339
|
3 |
|
$this->setAt($offset, $value); |
340
|
3 |
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
344
|
|
|
* Offset to unset |
345
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
346
|
|
|
* @param mixed $offset <p> |
347
|
|
|
* The offset to unset. |
348
|
|
|
* </p> |
349
|
|
|
* @return void |
350
|
|
|
*/ |
351
|
1 |
|
public function offsetUnset($offset) |
352
|
|
|
{ |
353
|
1 |
|
unset($this->rawData[$offset]); |
354
|
1 |
|
unset($this->typeData[$offset]); |
355
|
1 |
|
} |
356
|
|
|
} |
357
|
|
|
|