Passed
Push — 1.0.0 ( dcbbd2...044a70 )
by Alex
02:02
created

AbstractCollection::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace StraTDeS\VO\Collection;
4
5
abstract class AbstractCollection implements \Countable, \ArrayAccess, \Iterator
6
{
7
    protected array $items;
8
    protected int $position;
9
10 6
    protected function __construct()
11
    {
12 6
        $this->items = [];
13 6
        $this->position = 0;
14 6
    }
15
16 6
    public static function create()
17
    {
18 6
        return new static();
19
    }
20
21 2
    public function items(): array
22
    {
23 2
        return $this->items;
24
    }
25
26 2
    public function count()
27
    {
28 2
        return count($this->items);
29
    }
30
31
    public function offsetExists($offset)
32
    {
33
        return isset($this->items[$offset]);
34
    }
35
36 4
    public function offsetGet($offset)
37
    {
38 4
        return $this->items[$offset];
39
    }
40
41 2
    public function offsetSet($offset, $value)
42
    {
43 2
        $this->checkValueIsValidForCollection($value);
44 2
        $this->items[$offset] = $value;
45 2
    }
46
47
    public function offsetUnset($offset)
48
    {
49
        unset($this->items[$offset]);
50
    }
51
52 6
    private function checkValueIsValidForCollection($value): void
53
    {
54 6
        if (get_class($value) != $this->itemClass()) {
55 2
            throw new \InvalidArgumentException("Provided value is not a valid " . $this->itemClass());
56
        }
57 4
    }
58
59 2
    public function current()
60
    {
61 2
        return $this->items[$this->position];
62
    }
63
64 2
    public function next()
65
    {
66 2
        $this->position++;
67 2
    }
68
69 2
    public function key()
70
    {
71 2
        return $this->position;
72
    }
73
74 2
    public function valid()
75
    {
76 2
        return isset($this->items[$this->position]);
77
    }
78
79 2
    public function rewind()
80
    {
81 2
        $this->position = 0;
82 2
    }
83
84 4
    public function add($item) {
85 4
        $this->checkValueIsValidForCollection($item);
86
87 2
        $this->items[] = $item;
88
89 2
        return $this;
90
    }
91
92
    protected abstract function itemClass(): string;
93
}
94