Passed
Push — main ( 298d53...8aefd5 )
by Andrey
12:51 queued 11:03
created

Arrayable::unique()   A

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
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 65
    public function __construct($value = [])
12
    {
13 65
        $this->value = $value;
14 65
    }
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 34
    public function of($value = []): self
24
    {
25 34
        return new self($value);
26
    }
27
28
    /**
29
     * Returns the final array.
30
     *
31
     * @return array
32
     */
33 65
    public function get(): array
34
    {
35 65
        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
     * Removes duplicate values from an array.
78
     *
79
     * Sorting type flags:
80
     *   SORT_REGULAR       - compare items normally
81
     *   SORT_NUMERIC       - compare items numerically
82
     *   SORT_STRING        - compare items as strings
83
     *   SORT_LOCALE_STRING - compare items as strings, based on the current locale
84
     *
85
     * @see https://php.net/manual/en/function.array-unique.php
86
     *
87
     * @param  int  $flags
88
     *
89
     * @return $this
90
     */
91 2
    public function unique(int $flags = SORT_STRING): self
92
    {
93 2
        return new self(Arr::unique($this->value, $flags));
94
    }
95
96
    /**
97
     * Sort an associative array in the order specified by an array of keys.
98
     *
99
     * Example:
100
     *
101
     *  $arr = ['q' => 1, 'r' => 2, 's' => 5, 'w' => 123];
102
     *
103
     *  Arr::sortByKeys($arr, ['q', 'w', 'e']);
104
     *
105
     * print_r($arr);
106
     *
107
     *   Array
108
     *   (
109
     *     [q] => 1
110
     *     [w] => 123
111
     *     [r] => 2
112
     *     [s] => 5
113
     *   )
114
     *
115
     * @see https://gist.github.com/Ellrion/a3145621f936aa9416f4c04987533d8d#file-helper-php
116
     *
117
     * @param  array  $sorter
118
     *
119
     * @return \Helldar\Support\Helpers\Ables\Arrayable
120
     */
121 2
    public function sortByKeys(array $sorter): self
122
    {
123 2
        return new self(Arr::sortByKeys($this->value, $sorter));
124
    }
125
126
    /**
127
     * Recursively sorting an array by values.
128
     *
129
     * @param  callable|null  $callback
130
     *
131
     * @return \Helldar\Support\Helpers\Ables\Arrayable
132
     */
133 6
    public function sort(callable $callback = null): self
134
    {
135 6
        return new self(Arr::sort($this->value, $callback));
136
    }
137
138
    /**
139
     * Recursively sorting an array by keys.
140
     *
141
     * @param  callable|null  $callback
142
     *
143
     * @return \Helldar\Support\Helpers\Ables\Arrayable
144
     */
145 6
    public function ksort(callable $callback = null): self
146
    {
147 6
        return new self(Arr::ksort($this->value, $callback));
148
    }
149
150
    /**
151
     * Merge one or more arrays recursively.
152
     * Don't forget that numeric keys NOT will be renumbered!
153
     *
154
     * @param  array  ...$arrays
155
     *
156
     * @return \Helldar\Support\Helpers\Ables\Arrayable
157
     */
158 4
    public function merge(...$arrays): self
159
    {
160 4
        return new self(Arr::merge($this->value, ...$arrays));
161
    }
162
163
    /**
164
     * Get the instance as an array.
165
     *
166
     * @return \Helldar\Support\Helpers\Ables\Arrayable
167
     */
168 4
    public function toArray(): self
169
    {
170 4
        return new self(Arr::toArray($this->value));
171
    }
172
173
    /**
174
     * Get all of the given array except for a specified array of keys.
175
     *
176
     * @param  array|callable|string  $keys
177
     *
178
     * @return \Helldar\Support\Helpers\Ables\Arrayable
179
     */
180 6
    public function except($keys): self
181
    {
182 6
        return new self(Arr::except($this->value, $keys));
183
    }
184
185
    /**
186
     * Get a subset of the items from the given array.
187
     *
188
     * @param  array|callable|string  $keys
189
     *
190
     * @return \Helldar\Support\Helpers\Ables\Arrayable
191
     */
192 4
    public function only($keys): self
193
    {
194 4
        return new self(Arr::only($this->value, $keys));
195
    }
196
197
    /**
198
     * Iterates over each value in the <b>array</b> passing them to the <b>callback</b> function.
199
     * If the <b>callback</b> function returns true, the current value from <b>array</b> is returned into
200
     * the result array. Array keys are preserved.
201
     *
202
     * @see https://php.net/manual/en/function.array-filter.php
203
     *
204
     * @param  callable  $callback
205
     * @param  int  $mode
206
     *
207
     * @return $this
208
     */
209 4
    public function filter(callable $callback, int $mode = 0): self
210
    {
211 4
        return new self(Arr::filter($this->value, $callback, $mode));
212
    }
213
214
    /**
215
     * Flatten a multi-dimensional array into a single level.
216
     *
217
     * @param  bool  $ignore_keys
218
     *
219
     * @return \Helldar\Support\Helpers\Ables\Arrayable
220
     */
221 6
    public function flatten(bool $ignore_keys = true): self
222
    {
223 6
        return new self(Arr::flatten($this->value, $ignore_keys));
224
    }
225
226
    /**
227
     * Applies the callback to the elements of the given arrays.
228
     *
229
     * @param  callable  $callback
230
     * @param  bool  $recursive
231
     *
232
     * @return \Helldar\Support\Helpers\Ables\Arrayable
233
     */
234 6
    public function map(callable $callback, bool $recursive = false): self
235
    {
236 6
        return new self(Arr::map($this->value, $callback, $recursive));
237
    }
238
239
    /**
240
     * Exchanges all keys with their associated values in an array.
241
     *
242
     * @see  https://php.net/manual/en/function.array-flip.php
243
     *
244
     * @return $this
245
     */
246 6
    public function flip(): self
247
    {
248 6
        return new self(Arr::flip($this->value));
249
    }
250
251
    /**
252
     * Return all the keys or a subset of the keys of an array.
253
     *
254
     * @see https://php.net/manual/en/function.array-keys.php
255
     *
256
     * @return \Helldar\Support\Helpers\Ables\Arrayable
257
     */
258 6
    public function keys(): self
259
    {
260 6
        return new self(Arr::keys($this->value));
261
    }
262
263
    /**
264
     * Return all the values of an array.
265
     *
266
     * @see  https://php.net/manual/en/function.array-values.php
267
     *
268
     * @return \Helldar\Support\Helpers\Ables\Arrayable
269
     */
270 2
    public function values(): self
271
    {
272 2
        return new self(Arr::values($this->value));
273
    }
274
275
    /**
276
     * Push elements onto the end of array.
277
     *
278
     * @see  https://php.net/manual/en/function.array-push.php
279
     *
280
     * @param  mixed  ...$values
281
     *
282
     * @return \Helldar\Support\Helpers\Ables\Arrayable
283
     */
284 4
    public function push(...$values): self
285
    {
286 4
        return new self(Arr::push($this->value, ...$values));
287
    }
288
289
    /**
290
     * Assigns a value to an array key.
291
     *
292
     * @param  mixed  $key
293
     * @param  mixed  $value
294
     *
295
     * @return \Helldar\Support\Helpers\Ables\Arrayable
296
     */
297 4
    public function set($key, $value = null): self
298
    {
299 4
        return new self(Arr::set($this->value, $key, $value));
300
    }
301
302
    /**
303
     * Removes an array key.
304
     *
305
     * @param  mixed  $key
306
     *
307
     * @return \Helldar\Support\Helpers\Ables\Arrayable
308
     */
309 4
    public function remove($key): self
310
    {
311 4
        return new self(Arr::remove($this->value, $key));
312
    }
313
314
    /**
315
     * Call the given Closure with the given value then return the value.
316
     *
317
     * @param  callable  $callback
318
     *
319
     * @return $this
320
     */
321 2
    public function tap(callable $callback): self
322
    {
323 2
        return new self(Arr::tap($this->value, $callback));
324
    }
325
326
    /**
327
     * Outputs the contents of a variable without terminating the application.
328
     *
329
     * @return $this
330
     */
331
    public function dump(): self
332
    {
333
        dump($this->value);
334
335
        return $this;
336
    }
337
338
    /**
339
     * Outputs the contents of a variable, terminating the application.
340
     */
341
    public function dd(): void
342
    {
343
        dd($this->value);
344
    }
345
}
346