|
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; |
|
13
|
|
|
protected $persisted; |
|
14
|
|
|
protected $items = []; |
|
15
|
|
|
protected $removed = []; |
|
16
|
|
|
protected $added = []; |
|
17
|
|
|
|
|
18
|
3 |
|
public function __construct(Iterator $cursor) |
|
19
|
|
|
{ |
|
20
|
3 |
|
$this->items = iterator_to_array($cursor); |
|
21
|
3 |
|
$this->persisted = $this->items; |
|
22
|
3 |
|
$this->length = count($this->items); |
|
23
|
3 |
|
} |
|
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
|
|
|
public function first() |
|
69
|
|
|
{ |
|
70
|
|
|
$this->index = 0; |
|
71
|
|
|
|
|
72
|
|
|
return $this->at($this->index); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function last() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->index = $this->length - 1; |
|
78
|
|
|
|
|
79
|
|
|
return $this->current(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
public function at(int $offset) |
|
83
|
|
|
{ |
|
84
|
1 |
|
if ($offset < $this->length) { |
|
85
|
1 |
|
return $this->items[$offset]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
throw new OutOfBoundsException("Invalid offset: ${offset}"); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function current() |
|
92
|
|
|
{ |
|
93
|
1 |
|
return $this->at($this->index); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function next(): void |
|
97
|
|
|
{ |
|
98
|
1 |
|
$this->index++; |
|
99
|
1 |
|
} |
|
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
|
2 |
|
return $this->length; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function toArray(): array |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->items; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|