Completed
Push — master ( f8936b...a397e0 )
by Dawid
02:47
created

Collection::rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Igni\Storage\Mapping\Collection;
4
5
use Iterator;
6
use OutOfBoundsException;
7
8
class Collection implements \Igni\Storage\Mapping\Collection
9
{
10
    protected $length = 0;
11
    protected $current;
12
    protected $index = 0;
13
    protected $persisted;
14
    protected $items = [];
15
    protected $removed = [];
16
    protected $added = [];
17
18 6
    public function __construct(Iterator $cursor)
19
    {
20 6
        $this->items = iterator_to_array($cursor);
21 6
        $this->persisted = $this->items;
22 6
        $this->length = count($this->items);
23 6
    }
24
25 1
    public function add($item): bool
26
    {
27 1
        $this->items[] = $item;
28 1
        $this->length++;
29 1
        $this->index = 0;
30
31 1
        return true;
32
    }
33
34
    public function remove($item): bool
35
    {
36
        if (!$this->contains($item)) {
37
            return false;
38
        }
39
40
        $this->removed[] = $item;
41
        array_splice($array, 1, 1);
42
        $this->length = count($this->items);
43
        $this->index = 0;
44
45
        return true;
46
    }
47
48 1
    public function contains($item): bool
49
    {
50 1
        return in_array($item, $this->items);
51
    }
52
53
    public function clear(): void
54
    {
55
        $this->items = [];
56
        $this->length = 0;
57
    }
58
59
    public function reset(): void
60
    {
61
        $this->items = $this->persisted;
62
        $this->length = count($this->items);
63
        $this->index = 0;
64
        $this->added = [];
65
        $this->removed = [];
66
    }
67
68 1
    public function first()
69
    {
70 1
        $this->index = 0;
71
72 1
        return $this->at($this->index);
73
    }
74
75 1
    public function last()
76
    {
77 1
        $this->index = $this->length - 1;
78
79 1
        return $this->current();
80
    }
81
82 4
    public function at(int $offset)
83
    {
84 4
        if ($offset < $this->length) {
85 4
            return $this->items[$offset];
86
        }
87
88
        throw new OutOfBoundsException("Invalid offset: ${offset}");
89
    }
90
91
    public function current()
92
    {
93 4
        return $this->at($this->index);
94
    }
95
96
    public function next(): void
97
    {
98 2
        $this->index++;
99 2
    }
100
101
    public function previous(): void
102
    {
103
        if ($this->index > 0) {
104
            $this->index--;
105
        }
106
    }
107
108
    public function key(): int
109
    {
110
        return $this->index;
111
    }
112
113
    public function valid(): bool
114
    {
115 1
        if ($this->index < $this->length) {
116 1
            return true;
117
        }
118
119 1
        return false;
120
    }
121
122
    public function rewind(): void
123
    {
124 1
        $this->index = 0;
125 1
    }
126
127
    public function count(): int
128
    {
129 3
        return $this->length;
130
    }
131
132
    public function toArray(): array
133
    {
134
        return $this->items;
135
    }
136
}
137