1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Cubiche\Core\Collections\ArrayCollection; |
11
|
|
|
|
12
|
|
|
use Cubiche\Core\Collections\CollectionInterface; |
13
|
|
|
use Cubiche\Core\Collections\Exception\InvalidKeyException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ArrayCollection Class. |
17
|
|
|
* |
18
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
19
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class ArrayCollection implements CollectionInterface, \ArrayAccess |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $elements = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* AbstractArrayCollection constructor. |
30
|
|
|
* |
31
|
|
|
* @param array $elements |
32
|
|
|
*/ |
33
|
|
|
public function __construct(array $elements = array()) |
34
|
|
|
{ |
35
|
|
|
$this->elements = $elements; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function clear() |
42
|
|
|
{ |
43
|
|
|
$this->elements = array(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function count() |
50
|
|
|
{ |
51
|
|
|
return \count($this->elements); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function isEmpty() |
58
|
|
|
{ |
59
|
|
|
return $this->count() === 0; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function getIterator() |
66
|
|
|
{ |
67
|
|
|
return new \ArrayIterator($this->elements); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function toArray() |
74
|
|
|
{ |
75
|
|
|
return $this->elements; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function slice($offset, $length = null) |
82
|
|
|
{ |
83
|
|
|
return new static(\array_slice($this->elements, $offset, $length, true)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
protected function hasKey($key) |
90
|
|
|
{ |
91
|
|
|
$this->validateKey($key); |
92
|
|
|
|
93
|
|
|
return isset($this->elements[$key]) || \array_key_exists($key, $this->elements); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function offsetExists($offset) |
100
|
|
|
{ |
101
|
|
|
return $this->hasKey($offset); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function offsetGet($offset) |
108
|
|
|
{ |
109
|
|
|
$this->validateKey($offset); |
110
|
|
|
|
111
|
|
|
return isset($this->elements[$offset]) ? $this->elements[$offset] : null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function offsetSet($offset, $value) |
118
|
|
|
{ |
119
|
|
|
$this->validateKey($offset); |
120
|
|
|
|
121
|
|
|
$this->elements[$offset] = $value; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
public function offsetUnset($offset) |
128
|
|
|
{ |
129
|
|
|
if ($this->hasKey($offset)) { |
130
|
|
|
unset($this->elements[$offset]); |
131
|
|
|
|
132
|
|
|
$this->elements = array_values($this->elements); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Validates that a key is valid. |
138
|
|
|
* |
139
|
|
|
* @param int|string $key |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
* |
143
|
|
|
* @throws InvalidKeyException If the key is invalid. |
144
|
|
|
*/ |
145
|
|
|
protected function validateKey($key) |
146
|
|
|
{ |
147
|
|
|
if (!is_string($key) && !is_int($key)) { |
148
|
|
|
throw InvalidKeyException::forKey($key); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Validates that the argument is traversable. |
156
|
|
|
* |
157
|
|
|
* @param array|\Traversable $elements |
158
|
|
|
* |
159
|
|
|
* @return bool |
160
|
|
|
* |
161
|
|
|
* @throws \InvalidArgumentException. |
162
|
|
|
*/ |
163
|
|
|
protected function validateTraversable($elements) |
164
|
|
|
{ |
165
|
|
|
if (!is_array($elements) && !$elements instanceof \Traversable) { |
166
|
|
|
throw new \InvalidArgumentException( |
167
|
|
|
sprintf( |
168
|
|
|
'Expected an array or traversable as argument. Got: %s', |
169
|
|
|
is_object($elements) ? get_class($elements) : gettype($elements) |
170
|
|
|
) |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return true; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|