1
|
|
|
<?php namespace BuildR\Collection\ArrayList; |
2
|
|
|
|
3
|
|
|
use BuildR\Collection\Collection\AbstractCollection; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* ArrayList implementation |
7
|
|
|
* |
8
|
|
|
* BuildR PHP Framework |
9
|
|
|
* |
10
|
|
|
* @author Zoltán Borsos <[email protected]> |
11
|
|
|
* @package Collection |
12
|
|
|
* @subpackage ArrayList |
13
|
|
|
* |
14
|
|
|
* @copyright Copyright 2015, Zoltán Borsos. |
15
|
|
|
* @license https://github.com/Zolli/BuildR/blob/master/LICENSE.md |
16
|
|
|
* @link https://github.com/Zolli/BuildR |
17
|
|
|
*/ |
18
|
|
|
class ArrayList extends AbstractCollection implements ListInterface { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ArrayList constructor. |
22
|
|
|
* |
23
|
|
|
* @param array|NULL $elements |
24
|
|
|
*/ |
25
|
24 |
|
public function __construct($elements = NULL) { |
26
|
24 |
|
if(is_array($elements)) { |
27
|
5 |
|
$this->addAll($elements); |
28
|
5 |
|
} |
29
|
24 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritDoc} |
33
|
|
|
*/ |
34
|
23 |
|
public function add($element) { |
35
|
23 |
|
$this->data[] = $element; |
36
|
23 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritDoc} |
40
|
|
|
*/ |
41
|
22 |
|
public function addAll($elements) { |
42
|
22 |
|
foreach($elements as $element) { |
43
|
22 |
|
$this->add($element); |
44
|
22 |
|
} |
45
|
22 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritDoc} |
49
|
|
|
*/ |
50
|
1 |
|
public function addTo($index, $element) { |
51
|
1 |
|
array_splice($this->data, $index, 0, $element); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritDoc} |
56
|
|
|
*/ |
57
|
13 |
|
public function get($index) { |
58
|
13 |
|
return (isset($this->data[$index])) ? $this->data[$index] : NULL; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
2 |
|
public function filter(callable $filter) { |
65
|
|
|
//@codeCoverageIgnoreStart |
66
|
|
|
if(defined('HHVM_VERSION')) { |
67
|
|
|
$returnedList = new static(); |
68
|
|
|
|
69
|
|
|
foreach($this->data as $index => $value) { |
70
|
|
|
if(call_user_func_array($filter, [$value, $index]) === TRUE) { |
71
|
|
|
$returnedList->add($value); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $returnedList; |
76
|
|
|
} |
77
|
|
|
//@codeCoverageIgnoreEnd |
78
|
|
|
|
79
|
2 |
|
$result = array_filter($this->data, $filter, ARRAY_FILTER_USE_BOTH); |
80
|
|
|
|
81
|
2 |
|
return new static($result); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
2 |
|
public function set($index, $element) { |
88
|
2 |
|
$returns = NULL; |
89
|
2 |
|
if(isset($this->data[$index])) { |
90
|
2 |
|
$returns = $this->data[$index]; |
91
|
2 |
|
} |
92
|
|
|
|
93
|
2 |
|
$this->data[$index] = $element; |
94
|
|
|
|
95
|
2 |
|
return $returns; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
9 |
|
public function contains($element) { |
102
|
9 |
|
return (array_search($element, $this->data, TRUE) === FALSE) ? FALSE : TRUE; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritDoc} |
107
|
|
|
*/ |
108
|
3 |
|
public function containsAt($index) { |
109
|
3 |
|
return isset($this->data[$index]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritDoc} |
114
|
|
|
*/ |
115
|
5 |
View Code Duplication |
public function containsAll($elements) { |
|
|
|
|
116
|
5 |
|
$elements = $this->collectionToArray($elements); |
117
|
|
|
|
118
|
5 |
|
$result = TRUE; |
119
|
|
|
|
120
|
5 |
|
foreach($elements as $item) { |
121
|
5 |
|
if($this->contains($item) === FALSE) { |
122
|
2 |
|
$result = FALSE; |
123
|
|
|
|
124
|
2 |
|
break; |
125
|
|
|
} |
126
|
5 |
|
} |
127
|
|
|
|
128
|
5 |
|
return $result; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritDoc} |
133
|
|
|
*/ |
134
|
2 |
View Code Duplication |
public function containsAny($elements) { |
|
|
|
|
135
|
2 |
|
$elements = $this->collectionToArray($elements); |
136
|
|
|
|
137
|
2 |
|
foreach($elements as $item) { |
138
|
2 |
|
if($this->contains($item) === TRUE) { |
139
|
2 |
|
return TRUE; |
140
|
|
|
} |
141
|
2 |
|
} |
142
|
|
|
|
143
|
2 |
|
return FALSE; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritDoc} |
148
|
|
|
*/ |
149
|
5 |
|
public function indexOf($element) { |
150
|
5 |
|
return array_search($element, $this->data, TRUE); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritDoc} |
155
|
|
|
*/ |
156
|
1 |
|
public function equals(ListInterface $collection) { |
157
|
1 |
|
if($collection->size() !== $this->size()) { |
158
|
1 |
|
return FALSE; |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
$elements = $collection->toArray(); |
162
|
|
|
|
163
|
1 |
|
foreach($elements as $key => $value) { |
164
|
1 |
|
if(isset($this->data[$key]) && $this->data[$key] === $value) { |
165
|
1 |
|
continue; |
166
|
|
|
} |
167
|
|
|
|
168
|
1 |
|
return FALSE; |
169
|
1 |
|
} |
170
|
|
|
|
171
|
1 |
|
return TRUE; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritDoc} |
176
|
|
|
*/ |
177
|
3 |
|
public function removeAt($index) { |
178
|
3 |
|
if(isset($this->data[$index])) { |
179
|
3 |
|
unset($this->data[$index]); |
180
|
3 |
|
} |
181
|
3 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritDoc} |
185
|
|
|
*/ |
186
|
2 |
|
public function removeAll($elements) { |
187
|
2 |
|
$elements = $this->collectionToArray($elements); |
188
|
|
|
|
189
|
2 |
|
foreach($elements as $item) { |
190
|
2 |
|
$index = $this->indexOf($item); |
191
|
2 |
|
$this->removeAt($index); |
192
|
2 |
|
} |
193
|
2 |
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritDoc} |
197
|
|
|
*/ |
198
|
1 |
|
public function subList($offset, $length) { |
199
|
1 |
|
$slice = array_slice($this->data, $offset, $length, FALSE); |
200
|
|
|
|
201
|
1 |
|
return new static($slice); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritDoc} |
206
|
|
|
*/ |
207
|
2 |
|
public function retainAll($elements) { |
208
|
2 |
|
$elements = $this->collectionToArray($elements); |
209
|
|
|
|
210
|
2 |
View Code Duplication |
foreach($this->data as $index => $item) { |
|
|
|
|
211
|
2 |
|
if(array_search($item, $elements, TRUE) !== FALSE) { |
212
|
2 |
|
continue; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
unset($this->data[$index]); |
216
|
2 |
|
} |
217
|
2 |
|
} |
218
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.