|
1
|
|
|
<?php namespace BuildR\Collection; |
|
2
|
|
|
|
|
3
|
|
|
use BuildR\Collection\Exception\CollectionException; |
|
4
|
|
|
use BuildR\Collection\Interfaces\CollectionInterface; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Set type (Collection implementation) |
|
8
|
|
|
* Sets only store scalar types |
|
9
|
|
|
* |
|
10
|
|
|
* BuildR PHP Framework |
|
11
|
|
|
* |
|
12
|
|
|
* @author Zoltán Borsos <[email protected]> |
|
13
|
|
|
* @package Collection |
|
14
|
|
|
* |
|
15
|
|
|
* @copyright Copyright 2015, Zoltán Borsos. |
|
16
|
|
|
* @license https://github.com/Zolli/BuildR/blob/master/LICENSE.md |
|
17
|
|
|
* @link https://github.com/Zolli/BuildR |
|
18
|
|
|
*/ |
|
19
|
|
|
class Set implements CollectionInterface { |
|
20
|
|
|
|
|
21
|
|
|
protected $data = []; |
|
22
|
|
|
|
|
23
|
|
|
private $position = 0; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Set constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param null|array $values |
|
29
|
|
|
*/ |
|
30
|
24 |
|
public function __construct($values = NULL) { |
|
31
|
24 |
|
if(is_array($values)) { |
|
32
|
6 |
|
$this->addAll($values); |
|
33
|
6 |
|
} |
|
34
|
24 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
9 |
|
public function toArray() { |
|
40
|
9 |
|
return $this->data; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
24 |
|
public function add($element) { |
|
47
|
24 |
|
if(!is_scalar($element)) { |
|
48
|
4 |
|
throw CollectionException::nonScalarTypeGiven(gettype($element)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
20 |
|
$this->data[] = $element; |
|
52
|
20 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
18 |
|
public function addAll($elements) { |
|
58
|
18 |
|
$elements = $this->checkAndConvertInputCollection($elements); |
|
59
|
|
|
|
|
60
|
18 |
|
foreach($elements as $item) { |
|
61
|
18 |
|
$this->add($item); |
|
62
|
18 |
|
} |
|
63
|
18 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function clear() { |
|
69
|
1 |
|
$this->data = []; |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
9 |
|
public function contains($element) { |
|
76
|
9 |
|
return (array_search($element, $this->data, TRUE) === FALSE) ? FALSE : TRUE; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
8 |
View Code Duplication |
public function containsAll($elements) { |
|
83
|
8 |
|
$elements = $this->checkAndConvertInputCollection($elements); |
|
84
|
|
|
|
|
85
|
8 |
|
$result = TRUE; |
|
86
|
|
|
|
|
87
|
8 |
|
foreach($elements as $item) { |
|
88
|
8 |
|
if($this->contains($item) === FALSE) { |
|
89
|
4 |
|
$result = FALSE; |
|
90
|
|
|
|
|
91
|
4 |
|
break; |
|
92
|
|
|
} |
|
93
|
8 |
|
} |
|
94
|
|
|
|
|
95
|
8 |
|
return $result; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
5 |
|
public function containsAny($elements) { |
|
102
|
5 |
|
$elements = $this->checkAndConvertInputCollection($elements); |
|
103
|
|
|
|
|
104
|
5 |
|
foreach($elements as $item) { |
|
105
|
5 |
|
if($this->contains($item) === TRUE) { |
|
106
|
4 |
|
return TRUE; |
|
107
|
|
|
} |
|
108
|
5 |
|
} |
|
109
|
|
|
|
|
110
|
5 |
|
return FALSE; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* {@inheritdoc} |
|
115
|
|
|
*/ |
|
116
|
5 |
|
public function equals(CollectionInterface $collection) { |
|
117
|
|
|
//First check the size, if this not equals the tow collection |
|
118
|
|
|
//not be identical |
|
119
|
5 |
|
if($collection->size() !== $this->size()) { |
|
120
|
1 |
|
return FALSE; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
//Use strict comparison to check arrays are equals |
|
124
|
|
|
//(Values and orders) |
|
125
|
4 |
|
return $collection->toArray() === $this->data; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritdoc} |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function isEmpty() { |
|
132
|
1 |
|
return empty($this->data); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* {@inheritdoc} |
|
137
|
|
|
*/ |
|
138
|
2 |
|
public function remove($element) { |
|
139
|
2 |
|
if(($index = array_search($element, $this->data)) !== FALSE) { |
|
140
|
2 |
|
unset($this->data[$index]); |
|
141
|
|
|
|
|
142
|
2 |
|
return TRUE; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
2 |
|
return FALSE; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* {@inheritdoc} |
|
150
|
|
|
*/ |
|
151
|
1 |
View Code Duplication |
public function removeAll($elements) { |
|
152
|
1 |
|
$elements = $this->checkAndConvertInputCollection($elements); |
|
153
|
|
|
|
|
154
|
1 |
|
$result = FALSE; |
|
155
|
|
|
|
|
156
|
1 |
|
foreach($elements as $item) { |
|
157
|
1 |
|
if($this->remove($item) === TRUE) { |
|
158
|
1 |
|
$result = TRUE; |
|
159
|
1 |
|
} |
|
160
|
1 |
|
} |
|
161
|
|
|
|
|
162
|
1 |
|
return $result; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* {@inheritdoc} |
|
167
|
|
|
*/ |
|
168
|
1 |
|
public function retainAll($elements) { |
|
169
|
1 |
|
$elements = $this->checkAndConvertInputCollection($elements); |
|
170
|
|
|
|
|
171
|
1 |
|
$result = FALSE; |
|
172
|
|
|
|
|
173
|
1 |
|
foreach($this->data as $index => $data) { |
|
174
|
1 |
|
if(array_search($data, $elements) === FALSE) { |
|
175
|
1 |
|
unset($this->data[$index]); |
|
176
|
|
|
|
|
177
|
1 |
|
$result = TRUE; |
|
178
|
1 |
|
} |
|
179
|
1 |
|
} |
|
180
|
|
|
|
|
181
|
1 |
|
return $result; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* {@inheritdoc} |
|
186
|
|
|
*/ |
|
187
|
12 |
|
public function size() { |
|
188
|
12 |
|
return (is_array($this->data)) ? count($this->data) : 0; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* {@inheritdoc} |
|
193
|
|
|
* |
|
194
|
|
|
* @codeCoverageIgnore |
|
195
|
|
|
*/ |
|
196
|
|
|
public function current() { |
|
197
|
|
|
return $this->data[$this->position]; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* {@inheritdoc} |
|
202
|
|
|
* |
|
203
|
|
|
* @codeCoverageIgnore |
|
204
|
|
|
*/ |
|
205
|
|
|
public function next() { |
|
206
|
|
|
$this->position++; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* {@inheritdoc} |
|
211
|
|
|
* |
|
212
|
|
|
* @codeCoverageIgnore |
|
213
|
|
|
*/ |
|
214
|
|
|
public function key() { |
|
215
|
|
|
return $this->position; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* {@inheritdoc} |
|
220
|
|
|
* |
|
221
|
|
|
* @codeCoverageIgnore |
|
222
|
|
|
*/ |
|
223
|
|
|
public function valid() { |
|
224
|
|
|
return isset($this->data[$this->position]); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* {@inheritdoc} |
|
229
|
|
|
* |
|
230
|
|
|
* @codeCoverageIgnore |
|
231
|
|
|
*/ |
|
232
|
|
|
public function rewind() { |
|
233
|
|
|
$this->position--; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* {@inheritdoc} |
|
238
|
|
|
*/ |
|
239
|
7 |
|
public function count() { |
|
240
|
7 |
|
return $this->size(); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @param array|\BuildR\Collection\Interfaces\CollectionInterface $elements |
|
245
|
|
|
* |
|
246
|
|
|
* @return array |
|
247
|
|
|
*/ |
|
248
|
18 |
|
protected function checkAndConvertInputCollection($elements) { |
|
249
|
18 |
|
if($elements instanceof CollectionInterface) { |
|
250
|
1 |
|
$elements = $elements->toArray(); |
|
251
|
1 |
|
} |
|
252
|
|
|
|
|
253
|
18 |
|
return $elements; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
} |
|
257
|
|
|
|