|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPKitchen\Platform\Struct; |
|
4
|
|
|
|
|
5
|
|
|
use PHPKitchen\Platform\Contract; |
|
6
|
|
|
use PHPKitchen\Platform\Mixin\Properties; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Represents implementation of array based collection. |
|
10
|
|
|
* |
|
11
|
|
|
* @author Dmitry Kolodko <[email protected]> |
|
12
|
|
|
* @since 1.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
class Collection implements Contract\Struct\Collection, Contract\Data\ValueObject { |
|
15
|
|
|
use Mixin\StructureOfElementsMethods; |
|
16
|
|
|
use Mixin\CountableMethods; |
|
17
|
|
|
use Mixin\IteratorMethods; |
|
18
|
|
|
use Mixin\JsonSerializableMethods; |
|
19
|
|
|
use Properties; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct($elements = []) { |
|
22
|
|
|
$this->elements = $elements; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Named constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param array|iterable $elements elements of collection. |
|
29
|
|
|
* |
|
30
|
|
|
* @return Collection new collection of specified elements. |
|
31
|
|
|
* |
|
32
|
|
|
* @since 1.0 |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function of($elements): Contract\Struct\Collection { |
|
35
|
|
|
return new static($elements); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Add element to the end of the collection. |
|
40
|
|
|
* |
|
41
|
|
|
* @param mixed $element element to add. |
|
42
|
|
|
* |
|
43
|
|
|
* @since 1.0 |
|
44
|
|
|
*/ |
|
45
|
|
|
public function add($element): void { |
|
46
|
|
|
$this->elements[] = $element; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Push one or more elements onto the end of collection. |
|
51
|
|
|
* Example: |
|
52
|
|
|
* ```php |
|
53
|
|
|
* $collection->push(1, 2, 3, 4); |
|
54
|
|
|
* ``` |
|
55
|
|
|
* |
|
56
|
|
|
* @param array ...$elements elements. |
|
57
|
|
|
* |
|
58
|
|
|
* @return int the new number of elements in the array. |
|
59
|
|
|
* |
|
60
|
|
|
* @since 1.0 |
|
61
|
|
|
*/ |
|
|
|
|
|
|
62
|
|
|
public function push(...$elements): int { |
|
63
|
|
|
return array_push($this->elements, ...$elements); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Pad collection to the specified length with a value. |
|
68
|
|
|
* |
|
69
|
|
|
* @param int $size new size of the collection. If size is |
|
70
|
|
|
* positive then the collection is padded on the right, if it's negative then |
|
71
|
|
|
* on the left. If the absolute value of size is less than or equal to |
|
72
|
|
|
* the length of the collection then no padding takes place. |
|
73
|
|
|
* @param mixed $value Value to pad if input is less than `size`. |
|
74
|
|
|
* |
|
75
|
|
|
* @since 1.0 |
|
76
|
|
|
*/ |
|
77
|
|
|
public function pad(int $size, $value = null): void { |
|
78
|
|
|
$this->elements = array_pad($this->elements, $size, $value); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Split collection into chunks of specified size. |
|
83
|
|
|
* |
|
84
|
|
|
* @param int $size size of each chunk. |
|
85
|
|
|
* |
|
86
|
|
|
* @return Collection a multidimensional numerically indexed collection, starting with zero, |
|
87
|
|
|
* with each dimension containing collection of specified size. |
|
88
|
|
|
* |
|
89
|
|
|
* @since 1.0 |
|
90
|
|
|
*/ |
|
91
|
|
View Code Duplication |
public function chunkBy(int $size): Contract\Struct\Collection { |
|
|
|
|
|
|
92
|
|
|
$chunks = array_chunk($this->elements, $size, $keepKeys = false); |
|
93
|
|
|
$collections = static::of([]); |
|
94
|
|
|
foreach ($chunks as $chunk) { |
|
95
|
|
|
$collections->add(static::of($chunk)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $collections; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Split collection into chunks of specified size keeping original indexes. |
|
103
|
|
|
* |
|
104
|
|
|
* @param int $size size of each chunk. |
|
105
|
|
|
* |
|
106
|
|
|
* @return Collection a multidimensional numerically indexed collection, starting with zero, |
|
107
|
|
|
* with each dimension containing collection of specified size and keys equal to original keys |
|
108
|
|
|
* of elements. |
|
109
|
|
|
* |
|
110
|
|
|
* @since 1.0 |
|
111
|
|
|
*/ |
|
112
|
|
View Code Duplication |
public function chunkKeepingKeysBy(int $size): Contract\Struct\Collection { |
|
|
|
|
|
|
113
|
|
|
$chunks = array_chunk($this->elements, $size, $keepKeys = true); |
|
114
|
|
|
$collections = static::of([]); |
|
115
|
|
|
foreach ($chunks as $chunk) { |
|
116
|
|
|
$collections->add(static::of($chunk)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $collections; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Gets keys of the collection. |
|
124
|
|
|
* |
|
125
|
|
|
* @return Collection collection of the collection keys. |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getKeys(): Contract\Struct\Collection { |
|
128
|
|
|
return static::of(array_keys($this->elements)); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Gets values of the collection. |
|
133
|
|
|
* |
|
134
|
|
|
* @return Contract\Struct\Collection collection of the collection keys. |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getValues(): Contract\Struct\Collection { |
|
137
|
|
|
return static::of(array_values($this->elements)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
///endregion |
|
141
|
|
|
/// |
|
142
|
|
|
/// region ----------------- ARRAY ACCESS METHODS ----------------- |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Whether a offset exists |
|
146
|
|
|
* |
|
147
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
148
|
|
|
* |
|
149
|
|
|
* @param mixed $offset An offset to check for. |
|
150
|
|
|
* |
|
151
|
|
|
* @return boolean true on success or false on failure. |
|
152
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
|
153
|
|
|
* |
|
154
|
|
|
* @since 1.0 |
|
155
|
|
|
*/ |
|
156
|
|
|
public function offsetExists($offset) { |
|
157
|
|
|
return $this->hasKey($offset); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Offset to retrieve |
|
162
|
|
|
* |
|
163
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
164
|
|
|
* |
|
165
|
|
|
* @param mixed $offset the offset to retrieve. |
|
166
|
|
|
* |
|
167
|
|
|
* @return mixed Can return all value types. |
|
168
|
|
|
* |
|
169
|
|
|
* @since 1.0 |
|
170
|
|
|
*/ |
|
171
|
|
|
public function offsetGet($offset) { |
|
172
|
|
|
return $this->elements[$offset] ?? null; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Offset to set |
|
177
|
|
|
* |
|
178
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
179
|
|
|
* |
|
180
|
|
|
* @param mixed $offset The offset to assign the value to. |
|
181
|
|
|
* @param mixed $value the value to set. |
|
182
|
|
|
* |
|
183
|
|
|
* @return void |
|
184
|
|
|
* |
|
185
|
|
|
* @since 1.0 |
|
186
|
|
|
*/ |
|
187
|
|
|
public function offsetSet($offset, $value) { |
|
188
|
|
|
if ($offset === null) { |
|
189
|
|
|
$this->elements[] = $value; |
|
190
|
|
|
} else { |
|
191
|
|
|
$this->elements[$offset] = $value; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Offset to unset |
|
197
|
|
|
* |
|
198
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
199
|
|
|
* |
|
200
|
|
|
* @param mixed $offset the offset to unset. |
|
201
|
|
|
* |
|
202
|
|
|
* @return void |
|
203
|
|
|
* |
|
204
|
|
|
* @since 1.0 |
|
205
|
|
|
*/ |
|
206
|
|
|
public function offsetUnset($offset) { |
|
207
|
|
|
unset($this->elements[$offset]); |
|
208
|
|
|
} |
|
209
|
|
|
/// endregion |
|
210
|
|
|
} |