1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mundanity\Collection; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Implements a basic collection from an array. |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
class Collection implements CollectionInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The data in the collection. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
protected $data = []; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor |
23
|
|
|
* |
24
|
|
|
* @param array $data |
25
|
|
|
* An array of data to populate the collection with. Note that existing |
26
|
|
|
* indexes are removed. If indexes are significant, use KeyedCollection |
27
|
|
|
* instead. |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
public function __construct(array $data = []) |
31
|
|
|
{ |
32
|
|
|
$this->data = array_values($data); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
* |
39
|
|
|
*/ |
40
|
|
|
public static function fromCollection(CollectionInterface $collection) |
41
|
|
|
{ |
42
|
|
|
return new static($collection->toArray()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
* |
49
|
|
|
*/ |
50
|
|
|
public function has($item) |
51
|
|
|
{ |
52
|
|
|
return in_array($item, $this->data); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
* |
59
|
|
|
*/ |
60
|
|
|
public function getWhere(callable $callable) |
61
|
|
|
{ |
62
|
|
|
foreach ($this->data as $item) { |
63
|
|
|
if ($callable($item) === true) { |
64
|
|
|
return is_object($item) ? clone $item : $item; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
* |
73
|
|
|
*/ |
74
|
|
|
public function getAtIndex($index) |
75
|
|
|
{ |
76
|
|
|
if (!is_numeric($index)) { |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return isset($this->data[$index]) ? $this->data[$index] : null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
* |
87
|
|
|
*/ |
88
|
|
|
public function isEmpty() |
89
|
|
|
{ |
90
|
|
|
return empty($this->data); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns the number of items within the collection. |
96
|
|
|
* |
97
|
|
|
* @return int |
98
|
|
|
* |
99
|
|
|
*/ |
100
|
|
|
public function count() |
101
|
|
|
{ |
102
|
|
|
return count($this->data); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns an iterator. |
108
|
|
|
* |
109
|
|
|
* @return \Traversable |
110
|
|
|
* |
111
|
|
|
*/ |
112
|
|
|
public function getIterator() |
113
|
|
|
{ |
114
|
|
|
return new \ArrayIterator($this->data); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
* |
121
|
|
|
*/ |
122
|
|
|
public function toArray() |
123
|
|
|
{ |
124
|
|
|
return $this->data; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
* |
131
|
|
|
*/ |
132
|
|
|
public function filter(callable $callable) |
133
|
|
|
{ |
134
|
|
|
$data = array_filter($this->data, $callable, ARRAY_FILTER_USE_BOTH); |
135
|
|
|
return new static($data); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
* |
142
|
|
|
*/ |
143
|
|
|
public function map(callable $callable) |
144
|
|
|
{ |
145
|
|
|
$data = array_map($callable, $this->data); |
146
|
|
|
return new static($data); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
* |
153
|
|
|
*/ |
154
|
|
|
public function reduce(callable $callable, $initial = null) |
155
|
|
|
{ |
156
|
|
|
return array_reduce($this->data, $callable, $initial); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
* |
163
|
|
|
*/ |
164
|
|
View Code Duplication |
public function diff(CollectionInterface ...$collection) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
$diffs = array_map(function($item) { |
167
|
|
|
return $item->toArray(); |
168
|
|
|
}, $collection); |
169
|
|
|
|
170
|
|
|
$func = ($this instanceof KeyedCollection) ? 'array_diff_assoc' : 'array_diff'; |
171
|
|
|
$data = $func($this->data, ...$diffs); |
172
|
|
|
|
173
|
|
|
return new static($data); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
* |
180
|
|
|
*/ |
181
|
|
View Code Duplication |
public function intersect(CollectionInterface ...$collection) |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
$intersections = array_map(function($item) { |
184
|
|
|
return $item->toArray(); |
185
|
|
|
}, $collection); |
186
|
|
|
|
187
|
|
|
$func = ($this instanceof KeyedCollection) ? 'array_intersect_assoc' : 'array_intersect'; |
188
|
|
|
$data = $func($this->data, ...$intersections); |
189
|
|
|
|
190
|
|
|
return new static($data); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritdoc} |
196
|
|
|
* |
197
|
|
|
*/ |
198
|
|
|
public function merge(CollectionInterface ...$collection) |
199
|
|
|
{ |
200
|
|
|
$merges = array_map(function($item) { |
201
|
|
|
return $item->toArray(); |
202
|
|
|
}, $collection); |
203
|
|
|
|
204
|
|
|
$data = array_merge($this->data, ...$merges); |
205
|
|
|
|
206
|
|
|
return new static($data); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
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.