Passed
Push — main ( a7b6ef...298d53 )
by Andrey
02:02 queued 12s
created

Arrayable::tap()   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 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
     * 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
     * Exchanges all keys with their associated values in an array.
221
     *
222
     * @see  https://php.net/manual/en/function.array-flip.php
223
     *
224
     * @return $this
225
     */
226 6
    public function flip(): self
227
    {
228 6
        return new self(Arr::flip($this->value));
229
    }
230
231
    /**
232
     * Return all the keys or a subset of the keys of an array.
233
     *
234
     * @see https://php.net/manual/en/function.array-keys.php
235
     *
236
     * @return \Helldar\Support\Helpers\Ables\Arrayable
237
     */
238 6
    public function keys(): self
239
    {
240 6
        return new self(Arr::keys($this->value));
241
    }
242
243
    /**
244
     * Return all the values of an array.
245
     *
246
     * @see  https://php.net/manual/en/function.array-values.php
247
     *
248
     * @return \Helldar\Support\Helpers\Ables\Arrayable
249
     */
250 2
    public function values(): self
251
    {
252 2
        return new self(Arr::values($this->value));
253
    }
254
255
    /**
256
     * Push elements onto the end of array.
257
     *
258
     * @see  https://php.net/manual/en/function.array-push.php
259
     *
260
     * @param  mixed  ...$values
261
     *
262
     * @return \Helldar\Support\Helpers\Ables\Arrayable
263
     */
264 4
    public function push(...$values): self
265
    {
266 4
        return new self(Arr::push($this->value, ...$values));
267
    }
268
269
    /**
270
     * Assigns a value to an array key.
271
     *
272
     * @param  mixed  $key
273
     * @param  mixed  $value
274
     *
275
     * @return \Helldar\Support\Helpers\Ables\Arrayable
276
     */
277 4
    public function set($key, $value = null): self
278
    {
279 4
        return new self(Arr::set($this->value, $key, $value));
280
    }
281
282
    /**
283
     * Removes an array key.
284
     *
285
     * @param  mixed  $key
286
     *
287
     * @return \Helldar\Support\Helpers\Ables\Arrayable
288
     */
289 4
    public function remove($key): self
290
    {
291 4
        return new self(Arr::remove($this->value, $key));
292
    }
293
294
    /**
295
     * Call the given Closure with the given value then return the value.
296
     *
297
     * @param  callable  $callback
298
     *
299
     * @return $this
300
     */
301 2
    public function tap(callable $callback): self
302
    {
303 2
        return new self(Arr::tap($this->value, $callback));
304
    }
305
306
    /**
307
     * Outputs the contents of a variable without terminating the application.
308
     *
309
     * @return $this
310
     */
311
    public function dump(): self
312
    {
313
        dump($this->value);
314
315
        return $this;
316
    }
317
318
    /**
319
     * Outputs the contents of a variable, terminating the application.
320
     */
321
    public function dd(): void
322
    {
323
        dd($this->value);
324
    }
325
}
326