|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Oqq\Minc\Common\Collection; |
|
4
|
|
|
|
|
5
|
|
|
use Oqq\Minc\Common\Collection\Exception\KeyIsNotDefinedException; |
|
6
|
|
|
use Oqq\Minc\Common\Exception\OutOfBoundsException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @author Eric Braun <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class AbstractCollection implements \ArrayAccess, \Iterator, CollectionInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var array */ |
|
14
|
|
|
protected $values; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param array $values |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct(array $values = []) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->values = $values; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @inheritdoc |
|
26
|
|
|
*/ |
|
27
|
|
|
public function containsKey($key) |
|
28
|
|
|
{ |
|
29
|
|
|
return isset($this->values[$key]) || array_key_exists($key, $this->values); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @inheritdoc |
|
34
|
|
|
*/ |
|
35
|
|
|
public function get($key) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->assertContainsKey($key); |
|
38
|
|
|
|
|
39
|
|
|
return $this->values[$key]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function set($key, $value) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->values[$key] = $value; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritdoc |
|
52
|
|
|
*/ |
|
53
|
|
|
public function removeKey($key) |
|
54
|
|
|
{ |
|
55
|
|
|
unset($this->values[$key]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritdoc |
|
60
|
|
|
*/ |
|
61
|
|
|
public function contains($value) |
|
62
|
|
|
{ |
|
63
|
|
|
return false !== array_search($value, $this->values, true); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @inheritdoc |
|
68
|
|
|
*/ |
|
69
|
|
|
public function remove($value) |
|
70
|
|
|
{ |
|
71
|
|
|
foreach (array_keys($this->values, $value, true) as $key) { |
|
72
|
|
|
unset($this->values[$key]); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @inheritdoc |
|
78
|
|
|
*/ |
|
79
|
|
|
public function add($value) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->values[] = $value; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @inheritdoc |
|
86
|
|
|
*/ |
|
87
|
|
|
public function offsetExists($offset) |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->containsKey($offset); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @inheritdoc |
|
94
|
|
|
*/ |
|
95
|
|
|
public function offsetGet($offset) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->get($offset); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @inheritdoc |
|
102
|
|
|
*/ |
|
103
|
|
|
public function offsetSet($offset, $value) |
|
104
|
|
|
{ |
|
105
|
|
|
if (null === $offset) { |
|
106
|
|
|
$this->add($value); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$this->set($offset, $value); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @inheritdoc |
|
114
|
|
|
*/ |
|
115
|
|
|
public function offsetUnset($offset) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->removeKey($offset); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @inheritDoc |
|
122
|
|
|
*/ |
|
123
|
|
|
public function current() |
|
124
|
|
|
{ |
|
125
|
|
|
return current($this->values); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @inheritDoc |
|
130
|
|
|
*/ |
|
131
|
|
|
public function next() |
|
132
|
|
|
{ |
|
133
|
|
|
next($this->values); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @inheritDoc |
|
138
|
|
|
*/ |
|
139
|
|
|
public function key() |
|
140
|
|
|
{ |
|
141
|
|
|
return key($this->values); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @inheritDoc |
|
146
|
|
|
*/ |
|
147
|
|
|
public function valid() |
|
148
|
|
|
{ |
|
149
|
|
|
return null !== $this->key(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @inheritDoc |
|
154
|
|
|
*/ |
|
155
|
|
|
public function rewind() |
|
156
|
|
|
{ |
|
157
|
|
|
reset($this->values); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param mixed $key |
|
162
|
|
|
* |
|
163
|
|
|
* @throws OutOfBoundsException |
|
164
|
|
|
*/ |
|
165
|
|
|
protected function assertContainsKey($key) |
|
166
|
|
|
{ |
|
167
|
|
|
if (!$this->containsKey($key)) { |
|
168
|
|
|
throw new KeyIsNotDefinedException($key); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|