|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helldar\Support\Helpers\Ables; |
|
4
|
|
|
|
|
5
|
|
|
use Helldar\Support\Facades\Helpers\Arr; |
|
6
|
|
|
|
|
7
|
|
|
class Arrayable |
|
8
|
|
|
{ |
|
9
|
|
|
protected $value; |
|
10
|
|
|
|
|
11
|
45 |
|
public function __construct($value = []) |
|
12
|
|
|
{ |
|
13
|
45 |
|
$this->value = $value; |
|
14
|
45 |
|
} |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Creates the current object in case of accessing the Arrayable through the facade. |
|
18
|
|
|
* |
|
19
|
|
|
* @param array|\ArrayAccess|string|null $value |
|
20
|
|
|
* |
|
21
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
22
|
|
|
*/ |
|
23
|
23 |
|
public function of($value = []): self |
|
24
|
|
|
{ |
|
25
|
23 |
|
return new self($value); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Returns the final array. |
|
30
|
|
|
* |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
45 |
|
public function get(): array |
|
34
|
|
|
{ |
|
35
|
45 |
|
return $this->value ?: []; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Renaming array keys. |
|
40
|
|
|
* As the second parameter, a callback function is passed, which determines the actions for processing the value. |
|
41
|
|
|
* The output of the function must be a string with a name. |
|
42
|
|
|
* |
|
43
|
|
|
* @param callable $callback |
|
44
|
|
|
* |
|
45
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
46
|
|
|
*/ |
|
47
|
4 |
|
public function renameKeys(callable $callback): self |
|
48
|
|
|
{ |
|
49
|
4 |
|
return new self(Arr::renameKeys($this->value, $callback)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Renaming array keys with map. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $map |
|
56
|
|
|
* |
|
57
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public function renameKeysMap(array $map): self |
|
60
|
|
|
{ |
|
61
|
2 |
|
return new self(Arr::renameKeysMap($this->value, $map)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Push one a unique element onto the end of array. |
|
66
|
|
|
* |
|
67
|
|
|
* @param mixed $values |
|
68
|
|
|
* |
|
69
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public function addUnique($values): self |
|
72
|
|
|
{ |
|
73
|
4 |
|
return new self(Arr::addUnique($this->value, $values)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Sort an associative array in the order specified by an array of keys. |
|
78
|
|
|
* |
|
79
|
|
|
* Example: |
|
80
|
|
|
* |
|
81
|
|
|
* $arr = ['q' => 1, 'r' => 2, 's' => 5, 'w' => 123]; |
|
82
|
|
|
* |
|
83
|
|
|
* Arr::sortByKeys($arr, ['q', 'w', 'e']); |
|
84
|
|
|
* |
|
85
|
|
|
* print_r($arr); |
|
86
|
|
|
* |
|
87
|
|
|
* Array |
|
88
|
|
|
* ( |
|
89
|
|
|
* [q] => 1 |
|
90
|
|
|
* [w] => 123 |
|
91
|
|
|
* [r] => 2 |
|
92
|
|
|
* [s] => 5 |
|
93
|
|
|
* ) |
|
94
|
|
|
* |
|
95
|
|
|
* @see https://gist.github.com/Ellrion/a3145621f936aa9416f4c04987533d8d#file-helper-php |
|
96
|
|
|
* |
|
97
|
|
|
* @param array $sorter |
|
98
|
|
|
* |
|
99
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public function sortByKeys(array $sorter): self |
|
102
|
|
|
{ |
|
103
|
2 |
|
return new self(Arr::sortByKeys($this->value, $sorter)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Recursively sorting an array by values. |
|
108
|
|
|
* |
|
109
|
|
|
* @param callable|null $callback |
|
110
|
|
|
* |
|
111
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
112
|
|
|
*/ |
|
113
|
6 |
|
public function sort(callable $callback = null): self |
|
114
|
|
|
{ |
|
115
|
6 |
|
return new self(Arr::sort($this->value, $callback)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Recursively sorting an array by keys. |
|
120
|
|
|
* |
|
121
|
|
|
* @param callable|null $callback |
|
122
|
|
|
* |
|
123
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
124
|
|
|
*/ |
|
125
|
6 |
|
public function ksort(callable $callback = null): self |
|
126
|
|
|
{ |
|
127
|
6 |
|
return new self(Arr::ksort($this->value, $callback)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Merge one or more arrays recursively. |
|
132
|
|
|
* Don't forget that numeric keys NOT will be renumbered! |
|
133
|
|
|
* |
|
134
|
|
|
* @param array ...$arrays |
|
135
|
|
|
* |
|
136
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
137
|
|
|
*/ |
|
138
|
4 |
|
public function merge(...$arrays): self |
|
139
|
|
|
{ |
|
140
|
4 |
|
return new self(Arr::merge($this->value, ...$arrays)); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Get the instance as an array. |
|
145
|
|
|
* |
|
146
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
147
|
|
|
*/ |
|
148
|
4 |
|
public function toArray(): self |
|
149
|
|
|
{ |
|
150
|
4 |
|
return new self(Arr::toArray($this->value)); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Get all of the given array except for a specified array of keys. |
|
155
|
|
|
* |
|
156
|
|
|
* @param array|callable|string $keys |
|
157
|
|
|
* |
|
158
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
159
|
|
|
*/ |
|
160
|
6 |
|
public function except($keys): self |
|
161
|
|
|
{ |
|
162
|
6 |
|
return new self(Arr::except($this->value, $keys)); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Get a subset of the items from the given array. |
|
167
|
|
|
* |
|
168
|
|
|
* @param array|callable|string $keys |
|
169
|
|
|
* |
|
170
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
171
|
|
|
*/ |
|
172
|
4 |
|
public function only($keys): self |
|
173
|
|
|
{ |
|
174
|
4 |
|
return new self(Arr::only($this->value, $keys)); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Iterates over each value in the <b>array</b> passing them to the <b>callback</b> function. |
|
179
|
|
|
* If the <b>callback</b> function returns true, the current value from <b>array</b> is returned into |
|
180
|
|
|
* the result array. Array keys are preserved. |
|
181
|
|
|
* |
|
182
|
|
|
* @see https://php.net/manual/en/function.array-filter.php |
|
183
|
|
|
* |
|
184
|
|
|
* @param callable $callback |
|
185
|
|
|
* @param int $mode |
|
186
|
|
|
* |
|
187
|
|
|
* @return $this |
|
188
|
|
|
*/ |
|
189
|
4 |
|
public function filter(callable $callback, int $mode = 0): self |
|
190
|
|
|
{ |
|
191
|
4 |
|
return new self(Arr::filter($this->value, $callback, $mode)); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Flatten a multi-dimensional array into a single level. |
|
196
|
|
|
* |
|
197
|
|
|
* @param bool $ignore_keys |
|
198
|
|
|
* |
|
199
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
200
|
|
|
*/ |
|
201
|
6 |
|
public function flatten(bool $ignore_keys = true): self |
|
202
|
|
|
{ |
|
203
|
6 |
|
return new self(Arr::flatten($this->value, $ignore_keys)); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Applies the callback to the elements of the given arrays. |
|
208
|
|
|
* |
|
209
|
|
|
* @param callable $callback |
|
210
|
|
|
* @param bool $recursive |
|
211
|
|
|
* |
|
212
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
213
|
|
|
*/ |
|
214
|
6 |
|
public function map(callable $callback, bool $recursive = false): self |
|
215
|
|
|
{ |
|
216
|
6 |
|
return new self(Arr::map($this->value, $callback, $recursive)); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Return all the values of an array. |
|
221
|
|
|
* |
|
222
|
|
|
* @see https://php.net/manual/en/function.array-values.php |
|
223
|
|
|
* |
|
224
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
225
|
|
|
*/ |
|
226
|
2 |
|
public function values(): self |
|
227
|
|
|
{ |
|
228
|
2 |
|
return new self(Arr::values($this->value)); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Push elements onto the end of array. |
|
233
|
|
|
* |
|
234
|
|
|
* @see https://php.net/manual/en/function.array-push.php |
|
235
|
|
|
* |
|
236
|
|
|
* @param mixed ...$values |
|
237
|
|
|
* |
|
238
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
239
|
|
|
*/ |
|
240
|
|
|
public function push(...$values): self |
|
241
|
|
|
{ |
|
242
|
|
|
return new self(Arr::push($this->value, ...$values)); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Assigns a value to an array key. |
|
247
|
|
|
* |
|
248
|
|
|
* @param mixed $key |
|
249
|
|
|
* @param mixed $value |
|
250
|
|
|
* |
|
251
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
252
|
|
|
*/ |
|
253
|
|
|
public function set($key, $value): self |
|
254
|
|
|
{ |
|
255
|
|
|
return new self(Arr::set($this->value, $key, $value)); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Removes an array key. |
|
260
|
|
|
* |
|
261
|
|
|
* @param mixed $key |
|
262
|
|
|
* |
|
263
|
|
|
* @return \Helldar\Support\Helpers\Ables\Arrayable |
|
264
|
|
|
*/ |
|
265
|
|
|
public function remove($key): self |
|
266
|
|
|
{ |
|
267
|
|
|
return new self(Arr::remove($this->value, $key)); |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|