|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File Collection.php |
|
4
|
|
|
* |
|
5
|
|
|
* @author Edward Pfremmer <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Epfremme\Collection; |
|
8
|
|
|
|
|
9
|
|
|
use \Closure; |
|
10
|
|
|
|
|
11
|
|
|
use Epfremme\Collection\Traits\ClearableTrait; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Collection |
|
15
|
|
|
* |
|
16
|
|
|
* @package Epfremme\Collection |
|
17
|
|
|
*/ |
|
18
|
|
|
class Collection extends BaseCollection |
|
19
|
|
|
{ |
|
20
|
|
|
use ClearableTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Push new element to collection |
|
24
|
|
|
* |
|
25
|
|
|
* @param mixed $element |
|
26
|
|
|
* @return int |
|
27
|
|
|
*/ |
|
28
|
2 |
|
public function push($element) |
|
29
|
|
|
{ |
|
30
|
2 |
|
return array_push($this->elements, $element); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Pop last element from collection |
|
35
|
|
|
* |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function pop() |
|
39
|
|
|
{ |
|
40
|
1 |
|
return array_pop($this->elements); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Shift first element from collection |
|
45
|
|
|
* |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function shift() |
|
49
|
|
|
{ |
|
50
|
1 |
|
return array_shift($this->elements); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Unshift element to collection |
|
55
|
|
|
* |
|
56
|
|
|
* @param mixed $element |
|
57
|
|
|
* @return int |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function unshift($element) |
|
60
|
|
|
{ |
|
61
|
1 |
|
return array_unshift($this->elements, $element); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Add element to collection |
|
66
|
|
|
* |
|
67
|
|
|
* @param $element |
|
68
|
|
|
* @return $this |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function add($element) |
|
71
|
|
|
{ |
|
72
|
1 |
|
$this->push($element); |
|
73
|
|
|
|
|
74
|
1 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Remove element from collection |
|
79
|
|
|
* |
|
80
|
|
|
* @param $element |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
2 |
|
public function remove($element) |
|
84
|
|
|
{ |
|
85
|
2 |
|
if (!$this->contains($element)) { |
|
86
|
1 |
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
$this->offsetUnset($this->indexOf($element)); |
|
90
|
|
|
|
|
91
|
1 |
|
return true; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Set new value to collection at position |
|
96
|
|
|
* |
|
97
|
|
|
* @param mixed $key |
|
98
|
|
|
* @param mixed $value |
|
99
|
|
|
* @return $this |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public function set($key, $value) |
|
102
|
|
|
{ |
|
103
|
2 |
|
$this->offsetSet($key, $value); |
|
104
|
|
|
|
|
105
|
2 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Return new collection sliced from current elements |
|
110
|
|
|
* |
|
111
|
|
|
* @param int $offset |
|
112
|
|
|
* @param int $length |
|
113
|
|
|
* @return static |
|
114
|
|
|
*/ |
|
115
|
2 |
|
public function slice($offset = 0, $length = 0) |
|
116
|
|
|
{ |
|
117
|
2 |
|
return new static(array_slice($this->elements, $offset, $length, true)); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Splice and return new collection from current elements |
|
122
|
|
|
* |
|
123
|
|
|
* @param int $offset |
|
124
|
|
|
* @param int $length |
|
125
|
|
|
* @param array|mixed $replacement |
|
126
|
|
|
* @return static |
|
127
|
|
|
*/ |
|
128
|
4 |
|
public function splice($offset = 0, $length = 0, $replacement = []) |
|
129
|
|
|
{ |
|
130
|
4 |
|
return new static(array_splice($this->elements, $offset, $length, $replacement)); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Map function on against collection elements |
|
135
|
|
|
* |
|
136
|
|
|
* @param Closure $fn |
|
137
|
|
|
* @return static |
|
138
|
|
|
*/ |
|
139
|
1 |
|
public function map(Closure $fn) |
|
140
|
|
|
{ |
|
141
|
1 |
|
return new static(array_map($fn, $this->getValues(), $this->getKeys())); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Reduce collection to a single value |
|
146
|
|
|
* |
|
147
|
|
|
* @param Closure $fn |
|
148
|
|
|
* @param null $initial |
|
149
|
|
|
* @return mixed |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public function reduce(Closure $fn, $initial = null) |
|
152
|
|
|
{ |
|
153
|
1 |
|
return array_reduce($this->elements, $fn, $initial); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Sort collection by user defined function |
|
158
|
|
|
* |
|
159
|
|
|
* @param Closure $fn |
|
160
|
|
|
* @return bool |
|
161
|
|
|
*/ |
|
162
|
1 |
|
public function sort(Closure $fn) |
|
163
|
|
|
{ |
|
164
|
1 |
|
return usort($this->elements, $fn); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Sort collection by use defined function and |
|
169
|
|
|
* maintain element keys/indicies |
|
170
|
|
|
* |
|
171
|
|
|
* @param Closure $fn |
|
172
|
|
|
* @return bool |
|
173
|
|
|
*/ |
|
174
|
1 |
|
public function asort(Closure $fn) |
|
175
|
|
|
{ |
|
176
|
1 |
|
return uasort($this->elements, $fn); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* CAll function on each element of collection |
|
181
|
|
|
* |
|
182
|
|
|
* @param Closure $fn |
|
183
|
|
|
* @return bool |
|
184
|
|
|
*/ |
|
185
|
2 |
|
public function each(Closure $fn) |
|
186
|
|
|
{ |
|
187
|
2 |
|
foreach ($this->elements as $key => $element) { |
|
188
|
2 |
|
if ($fn($element, $key) === false) { |
|
189
|
1 |
|
return false; |
|
190
|
|
|
} |
|
191
|
2 |
|
} |
|
192
|
|
|
|
|
193
|
1 |
|
return true; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|