-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arr.php
262 lines (210 loc) · 6.03 KB
/
Arr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
class Arr
{
protected $array;
public function set(array $ar)
{
$this->array = $ar;
}
public function get(): array
{
return (array)$this;
}
public function changeKeyCase($case = CASE_UPPER): Arr
{
$this->array = array_change_key_case($this->array, $case);
return $this;
}
public function chunk(int $length, $preserve = false): Arr
{
$this->array = array_chunk($this->array, $length, $preserve);
return $this;
}
public function column($key, $index = null): Arr
{
$this->array = array_column($this->array, $key, $index);
return $this;
}
public function combine(array $values): Arr
{
$this->array = array_combine($this->array, $values);
return $this;
}
public function combineAsValues(array $keys): Arr
{
$this->array = array_combine($keys, $this->array);
return $this;
}
public function frequency(): Arr
{
$this->array = array_count_values($this->array);
return $this;
}
public function diffAssoc(array ...$arrays): Arr
{
$this->array = array_diff_assoc($this->array, $arrays);
return $this;
}
public function diffKeysAssoc(array ...$arrays): Arr
{
$this->array = array_diff_key($this->array, $arrays);
return $this;
}
public function diffAssocCallback(callable $callback, array ...$arrays): Arr
{
$this->array = array_diff_uassoc($this->array, $arrays, $callback);
return $this;
}
public function diffKeysAssocCallback(callable $callback, array ...$arrays): Arr
{
$this->array = array_diff_ukey($this->array, $arrays, $callback);
return $this;
}
public function difference(array ...$toCompare): Arr
{
$this->array = array_diff($this->array, $toCompare);
return $this;
}
public function addKeys(array $keys, $value): Arr
{
$this->array = array_fill_keys($keys, $value);
return $this;
}
public function fillValues($startIndex, $times, $value): Arr
{
$this->array = array_fill($startIndex, $times, $value);
return $this;
}
public function filter(callable $callback = null, int $mode = 0): Arr
{
$this->array = array_filter($this->array, $callback, $mode);
return $this;
}
public function flip(): Arr
{
$this->array = array_flip($this->array);
return $this;
}
public function intersectAssoc(array ...$arrays): Arr
{
$this->array = array_intersect_assoc($this->array, $arrays);
return $this;
}
public function intersectKey(array ...$arrays): Arr
{
$this->array = array_intersect_key($this->array, $arrays);
return $this;
}
public function intersectAssocCallback(callable $callback, array ...$arrays): Arr
{
$this->array = array_intersect_uassoc($this->array, $arrays, $callback);
return $this;
}
public function intersectKeyCallback(callable $callback, array ...$arrays)
{
$this->array = array_intersect_ukey($this->array, $arrays, $callback);
return $this;
}
public function intersect(array ...$arrays)
{
$this->array = array_intersect($this->array, $arrays);
return $this;
}
public function keyExists($key): bool
{
return array_key_exists($key, $this->array);
}
//TODO TEST IT
public function firstKey()
{
return key(current($this->array));
}
//TODO TEST IT
public function lastKey()
{
return key(end($this->array));
}
public function keys($searchValue = null, $strict = false): Arr
{
$this->array = array_keys($this->array, $searchValue, $strict);
return $this;
}
public function map(callable $callback): Arr
{
$this->array = array_map($callback, $this->array);
return $this;
}
public function mergeRecursive(array ...$arrays): Arr
{
$this->array = array_merge_recursive($this->array, $arrays);
return $this;
}
public function merge(array ...$arrays)
{
$this->array = array_merge($this->array, $arrays);
return $this;
}
public function pad(int $length, $value)
{
$this->array = array_pad($this->array, $length, $value);
return $this;
}
public function pop()
{
return array_pop($this->array);
}
public function product()
{
return array_product($this->array);
}
public function push(...$values)
{
array_push($this->array, $values);
return $this;
}
public function random($count = 1)
{
return array_rand($this->array, $count);
}
public function reduce(callable $callback, $initial = null)
{
return array_reduce($this->array, $callback, $initial);
}
public function replaceRecursive(array ...$replacements)
{
$this->array = array_replace_recursive($this->array, $replacements);
return $this;
}
public function replace(array ...$replacements)
{
$this->array = array_replace($this->array, $replacements);
return $this;
}
public function reverse($preserve_keys = false)
{
$this->array = array_reverse($this->array, $preserve_keys);
return $this;
}
public function search($needle, $strict = false)
{
return array_search($needle, $this->array, $strict);
}
public function shift()
{
return array_shift($this->array);
}
public function slice(int $offset, $length = null, $preserve_keys = false)
{
$this->array = array_slice($this->array, $offset, $length, $preserve_keys);
return $this;
}
public function splice($offset, $length = null, array $replacements = [])
{
$this->array = array_splice($this->array, $length, $replacements);
return $this;
}
public function sum()
{
return array_sum($this->array);
}
}