Passed
Pull Request — main (#125)
by Andrey
11:22
created

Arrayable::unique()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
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 63
    public function __construct($value = [])
12
    {
13 63
        $this->value = $value;
14 63
    }
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 33
    public function of($value = []): self
24
    {
25 33
        return new self($value);
26
    }
27
28
    /**
29
     * Returns the final array.
30
     *
31
     * @return array
32
     */
33 63
    public function get(): array
34
    {
35 63
        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
    public function unique(int $flags = SORT_STRING): self
92
    {
93
        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 2
     *  $arr = ['q' => 1, 'r' => 2, 's' => 5, 'w' => 123];
102
     *
103 2
     *  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 6
     *   )
114
     *
115 6
     * @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
    public function sortByKeys(array $sorter): self
122
    {
123
        return new self(Arr::sortByKeys($this->value, $sorter));
124
    }
125 6
126
    /**
127 6
     * Recursively sorting an array by values.
128
     *
129
     * @param  callable|null  $callback
130
     *
131
     * @return \Helldar\Support\Helpers\Ables\Arrayable
132
     */
133
    public function sort(callable $callback = null): self
134
    {
135
        return new self(Arr::sort($this->value, $callback));
136
    }
137
138 4
    /**
139
     * Recursively sorting an array by keys.
140 4
     *
141
     * @param  callable|null  $callback
142
     *
143
     * @return \Helldar\Support\Helpers\Ables\Arrayable
144
     */
145
    public function ksort(callable $callback = null): self
146
    {
147
        return new self(Arr::ksort($this->value, $callback));
148 4
    }
149
150 4
    /**
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
    public function merge(...$arrays): self
159
    {
160 6
        return new self(Arr::merge($this->value, ...$arrays));
161
    }
162 6
163
    /**
164
     * Get the instance as an array.
165
     *
166
     * @return \Helldar\Support\Helpers\Ables\Arrayable
167
     */
168
    public function toArray(): self
169
    {
170
        return new self(Arr::toArray($this->value));
171
    }
172 4
173
    /**
174 4
     * 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
    public function except($keys): self
181
    {
182
        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 4
     *
190
     * @return \Helldar\Support\Helpers\Ables\Arrayable
191 4
     */
192
    public function only($keys): self
193
    {
194
        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 6
     *
202
     * @see https://php.net/manual/en/function.array-filter.php
203 6
     *
204
     * @param  callable  $callback
205
     * @param  int  $mode
206
     *
207
     * @return $this
208
     */
209
    public function filter(callable $callback, int $mode = 0): self
210
    {
211
        return new self(Arr::filter($this->value, $callback, $mode));
212
    }
213
214 6
    /**
215
     * Flatten a multi-dimensional array into a single level.
216 6
     *
217
     * @param  bool  $ignore_keys
218
     *
219
     * @return \Helldar\Support\Helpers\Ables\Arrayable
220
     */
221
    public function flatten(bool $ignore_keys = true): self
222
    {
223
        return new self(Arr::flatten($this->value, $ignore_keys));
224
    }
225
226 6
    /**
227
     * Applies the callback to the elements of the given arrays.
228 6
     *
229
     * @param  callable  $callback
230
     * @param  bool  $recursive
231
     *
232
     * @return \Helldar\Support\Helpers\Ables\Arrayable
233
     */
234
    public function map(callable $callback, bool $recursive = false): self
235
    {
236
        return new self(Arr::map($this->value, $callback, $recursive));
237
    }
238 6
239
    /**
240 6
     * 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
    public function flip(): self
247
    {
248
        return new self(Arr::flip($this->value));
249
    }
250 2
251
    /**
252 2
     * 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
    public function keys(): self
259
    {
260
        return new self(Arr::keys($this->value));
261
    }
262
263
    /**
264 4
     * Return all the values of an array.
265
     *
266 4
     * @see  https://php.net/manual/en/function.array-values.php
267
     *
268
     * @return \Helldar\Support\Helpers\Ables\Arrayable
269
     */
270
    public function values(): self
271
    {
272
        return new self(Arr::values($this->value));
273
    }
274
275
    /**
276
     * Push elements onto the end of array.
277 4
     *
278
     * @see  https://php.net/manual/en/function.array-push.php
279 4
     *
280
     * @param  mixed  ...$values
281
     *
282
     * @return \Helldar\Support\Helpers\Ables\Arrayable
283
     */
284
    public function push(...$values): self
285
    {
286
        return new self(Arr::push($this->value, ...$values));
287
    }
288
289 4
    /**
290
     * Assigns a value to an array key.
291 4
     *
292
     * @param  mixed  $key
293
     * @param  mixed  $value
294
     *
295
     * @return \Helldar\Support\Helpers\Ables\Arrayable
296
     */
297
    public function set($key, $value = null): self
298
    {
299
        return new self(Arr::set($this->value, $key, $value));
300
    }
301 2
302
    /**
303 2
     * Removes an array key.
304
     *
305
     * @param  mixed  $key
306
     *
307
     * @return \Helldar\Support\Helpers\Ables\Arrayable
308
     */
309
    public function remove($key): self
310
    {
311
        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
    public function tap(callable $callback): self
322
    {
323
        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