Arrayable::flip()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/*
3
 * This file is part of the "andrey-helldar/support" project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author Andrey Helldar <[email protected]>
9
 *
10
 * @copyright 2021 Andrey Helldar
11
 *
12
 * @license MIT
13
 *
14
 * @see https://github.com/andrey-helldar/support
15
 */
16
17
namespace Helldar\Support\Helpers\Ables;
18
19
use Helldar\Support\Facades\Helpers\Arr;
20
21
class Arrayable
22
{
23
    protected $value;
24
25 249
    public function __construct($value = [])
26
    {
27 249
        $this->value = $value;
28 249
    }
29
30
    /**
31
     * Creates the current object in case of accessing the Arrayable through the facade.
32
     *
33
     * @param  array|\ArrayAccess|string|null  $value
34
     *
35
     * @return \Helldar\Support\Helpers\Ables\Arrayable
36
     */
37 217
    public function of($value = []): self
38
    {
39 217
        return new self($value);
40
    }
41
42
    /**
43
     * Returns the final array.
44
     *
45
     * @return array
46
     */
47 249
    public function get(): array
48
    {
49 249
        return $this->value ?: [];
50
    }
51
52
    /**
53
     * Renaming array keys.
54
     * As the second parameter, a callback function is passed, which determines the actions for processing the value.
55
     * The output of the function must be a string with a name.
56
     *
57
     * @param  callable  $callback
58
     *
59
     * @return \Helldar\Support\Helpers\Ables\Arrayable
60
     */
61 4
    public function renameKeys(callable $callback): self
62
    {
63 4
        return new self(Arr::renameKeys($this->value, $callback));
64
    }
65
66
    /**
67
     * Renaming array keys with map.
68
     *
69
     * @param  array  $map
70
     *
71
     * @return \Helldar\Support\Helpers\Ables\Arrayable
72
     */
73 2
    public function renameKeysMap(array $map): self
74
    {
75 2
        return new self(Arr::renameKeysMap($this->value, $map));
76
    }
77
78
    /**
79
     * Push one a unique element onto the end of array.
80
     *
81
     * @param  mixed  $values
82
     *
83
     * @return \Helldar\Support\Helpers\Ables\Arrayable
84
     */
85 4
    public function addUnique($values): self
86
    {
87 4
        return new self(Arr::addUnique($this->value, $values));
88
    }
89
90
    /**
91
     * Removes duplicate values from an array.
92
     *
93
     * Sorting type flags:
94
     *   SORT_REGULAR       - compare items normally
95
     *   SORT_NUMERIC       - compare items numerically
96
     *   SORT_STRING        - compare items as strings
97
     *   SORT_LOCALE_STRING - compare items as strings, based on the current locale
98
     *
99
     * @see https://php.net/manual/en/function.array-unique.php
100
     *
101
     * @param  int  $flags
102
     *
103
     * @return $this
104
     */
105 2
    public function unique(int $flags = SORT_STRING): self
106
    {
107 2
        return new self(Arr::unique($this->value, $flags));
108
    }
109
110
    /**
111
     * Sort an associative array in the order specified by an array of keys.
112
     *
113
     * Example:
114
     *
115
     *  $arr = ['q' => 1, 'r' => 2, 's' => 5, 'w' => 123];
116
     *
117
     *  Arr::sortByKeys($arr, ['q', 'w', 'e']);
118
     *
119
     * print_r($arr);
120
     *
121
     *   Array
122
     *   (
123
     *     [q] => 1
124
     *     [w] => 123
125
     *     [r] => 2
126
     *     [s] => 5
127
     *   )
128
     *
129
     * @see https://gist.github.com/Ellrion/a3145621f936aa9416f4c04987533d8d#file-helper-php
130
     *
131
     * @param  array  $sorter
132
     *
133
     * @return \Helldar\Support\Helpers\Ables\Arrayable
134
     */
135 2
    public function sortByKeys(array $sorter): self
136
    {
137 2
        return new self(Arr::sortByKeys($this->value, $sorter));
138
    }
139
140
    /**
141
     * Recursively sorting an array by values.
142
     *
143
     * @param  callable|null  $callback
144
     *
145
     * @return \Helldar\Support\Helpers\Ables\Arrayable
146
     */
147 6
    public function sort(callable $callback = null): self
148
    {
149 6
        return new self(Arr::sort($this->value, $callback));
150
    }
151
152
    /**
153
     * Recursively sorting an array by keys.
154
     *
155
     * @param  callable|null  $callback
156
     *
157
     * @return \Helldar\Support\Helpers\Ables\Arrayable
158
     */
159 6
    public function ksort(callable $callback = null): self
160
    {
161 6
        return new self(Arr::ksort($this->value, $callback));
162
    }
163
164
    /**
165
     * Merge one or more arrays recursively.
166
     * Don't forget that numeric keys NOT will be renumbered!
167
     *
168
     * @param  array  ...$arrays
169
     *
170
     * @return \Helldar\Support\Helpers\Ables\Arrayable
171
     */
172 154
    public function merge(...$arrays): self
173
    {
174 154
        return new self(Arr::merge($this->value, ...$arrays));
175
    }
176
177
    /**
178
     * Get the instance as an array.
179
     *
180
     * @return \Helldar\Support\Helpers\Ables\Arrayable
181
     */
182 4
    public function toArray(): self
183
    {
184 4
        return new self(Arr::toArray($this->value));
185
    }
186
187
    /**
188
     * Get all of the given array except for a specified array of keys.
189
     *
190
     * @param  array|callable|string  $keys
191
     *
192
     * @return \Helldar\Support\Helpers\Ables\Arrayable
193
     */
194 6
    public function except($keys): self
195
    {
196 6
        return new self(Arr::except($this->value, $keys));
197
    }
198
199
    /**
200
     * Get a subset of the items from the given array.
201
     *
202
     * @param  array|callable|string  $keys
203
     *
204
     * @return \Helldar\Support\Helpers\Ables\Arrayable
205
     */
206 4
    public function only($keys): self
207
    {
208 4
        return new self(Arr::only($this->value, $keys));
209
    }
210
211
    /**
212
     * Iterates over each value in the <b>array</b> passing them to the <b>callback</b> function.
213
     * If the <b>callback</b> function returns true, the current value from <b>array</b> is returned into
214
     * the result array. Array keys are preserved.
215
     *
216
     * @see https://php.net/manual/en/function.array-filter.php
217
     *
218
     * @param  callable|null  $callback
219
     * @param  int  $mode
220
     *
221
     * @return $this
222
     */
223 27
    public function filter(callable $callback = null, int $mode = 0): self
224
    {
225 27
        return new self(Arr::filter($this->value, $callback, $mode));
226
    }
227
228
    /**
229
     * Flatten a multi-dimensional array into a single level.
230
     *
231
     * @param  bool  $ignore_keys
232
     *
233
     * @return \Helldar\Support\Helpers\Ables\Arrayable
234
     */
235 6
    public function flatten(bool $ignore_keys = true): self
236
    {
237 6
        return new self(Arr::flatten($this->value, $ignore_keys));
238
    }
239
240
    /**
241
     * Applies the callback to the elements of the given arrays.
242
     *
243
     * @param  callable  $callback
244
     * @param  bool  $recursive
245
     *
246
     * @return \Helldar\Support\Helpers\Ables\Arrayable
247
     */
248 92
    public function map(callable $callback, bool $recursive = false): self
249
    {
250 92
        return new self(Arr::map($this->value, $callback, $recursive));
251
    }
252
253
    /**
254
     * Exchanges all keys with their associated values in an array.
255
     *
256
     * @see  https://php.net/manual/en/function.array-flip.php
257
     *
258
     * @return $this
259
     */
260 6
    public function flip(): self
261
    {
262 6
        return new self(Arr::flip($this->value));
263
    }
264
265
    /**
266
     * Return all the keys or a subset of the keys of an array.
267
     *
268
     * @see https://php.net/manual/en/function.array-keys.php
269
     *
270
     * @return \Helldar\Support\Helpers\Ables\Arrayable
271
     */
272 6
    public function keys(): self
273
    {
274 6
        return new self(Arr::keys($this->value));
275
    }
276
277
    /**
278
     * Return all the values of an array.
279
     *
280
     * @see  https://php.net/manual/en/function.array-values.php
281
     *
282
     * @return \Helldar\Support\Helpers\Ables\Arrayable
283
     */
284 2
    public function values(): self
285
    {
286 2
        return new self(Arr::values($this->value));
287
    }
288
289
    /**
290
     * Push elements onto the end of array.
291
     *
292
     * @see  https://php.net/manual/en/function.array-push.php
293
     *
294
     * @param  mixed  ...$values
295
     *
296
     * @return \Helldar\Support\Helpers\Ables\Arrayable
297
     */
298 4
    public function push(...$values): self
299
    {
300 4
        return new self(Arr::push($this->value, ...$values));
301
    }
302
303
    /**
304
     * Assigns a value to an array key.
305
     *
306
     * @param  mixed  $key
307
     * @param  mixed  $value
308
     *
309
     * @return \Helldar\Support\Helpers\Ables\Arrayable
310
     */
311 4
    public function set($key, $value = null): self
312
    {
313 4
        return new self(Arr::set($this->value, $key, $value));
314
    }
315
316
    /**
317
     * Removes an array key.
318
     *
319
     * @param  mixed  $key
320
     *
321
     * @return \Helldar\Support\Helpers\Ables\Arrayable
322
     */
323 4
    public function remove($key): self
324
    {
325 4
        return new self(Arr::remove($this->value, $key));
326
    }
327
328
    /**
329
     * Call the given Closure with the given value then return the value.
330
     *
331
     * @param  callable  $callback
332
     *
333
     * @return $this
334
     */
335 2
    public function tap(callable $callback): self
336
    {
337 2
        return new self(Arr::tap($this->value, $callback));
338
    }
339
340
    /**
341
     * Outputs the contents of a variable without terminating the application.
342
     *
343
     * @return $this
344
     */
345
    public function dump(): self
346
    {
347
        dump($this->value);
348
349
        return $this;
350
    }
351
352
    /**
353
     * Outputs the contents of a variable, terminating the application.
354
     */
355
    public function dd(): void
356
    {
357
        dd($this->value);
358
    }
359
}
360