Completed
Push — develop ( fe4302...d9652d )
by Jens
14:25 queued 03:33
created

Collection::next()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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
    const ID = 'id';
16
    use ContextTrait;
17
    const COLLECTION_TYPE = Collection::TYPE_LIST;
1 ignored issue
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

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