ValueObjectCollection::key()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\ValueObject;
4
5
use Iterator;
6
use Countable;
7
use ArrayAccess;
8
9
abstract class ValueObjectCollection implements
10
    ArrayAccess,
11
    Iterator,
12
    Countable
13
{
14
    /** @var array */
15
    protected $collection;
16
17
    /** @var int */
18
    protected $position = 0;
19
20
    public function __construct(array $collection = [])
21
    {
22
        $this->collection = $collection;
23
    }
24
25
    public function current()
26
    {
27
        return $this->collection[$this->position];
28
    }
29
30
    public function offsetGet($offset)
31
    {
32
        return $this->collection[$offset] ?? null;
33
    }
34
35
    public function offsetSet($offset, $value)
36
    {
37
        if (is_null($offset)) {
38
            $this->collection[] = $value;
39
        } else {
40
            $this->collection[$offset] = $value;
41
        }
42
    }
43
44
    public function offsetExists($offset)
45
    {
46
        return array_key_exists($offset, $this->collection);
47
    }
48
49
    public function offsetUnset($offset)
50
    {
51
        unset($this->collection[$offset]);
52
    }
53
54
    public function next()
55
    {
56
        $this->position++;
57
    }
58
59
    public function key()
60
    {
61
        return $this->position;
62
    }
63
64
    public function valid()
65
    {
66
        return array_key_exists($this->position, $this->collection);
67
    }
68
69
    public function rewind()
70
    {
71
        $this->position = 0;
72
    }
73
74
    public function toArray(): array
75
    {
76
        return $this->collection;
77
    }
78
79
    public function count(): int
80
    {
81
        return count($this->collection);
82
    }
83
}
84