Passed
Pull Request — main (#113)
by Andrey
15:18
created

Arrayable::of()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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
    public function __construct($value = [])
12
    {
13
        $this->value = $value;
14
    }
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
    public function of($value = []): self
24
    {
25
        return new self($value);
26
    }
27
28
    /**
29
     * Returns the final array.
30
     *
31
     * @return array
32
     */
33
    public function get(): array
34
    {
35
        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
    public function renameKeys(callable $callback): self
48
    {
49
        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
    public function renameKeysMap(array $map): self
60
    {
61
        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
    public function addUnique($values): self
72
    {
73
        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
    public function sortByKeys(array $sorter): self
102
    {
103
        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
    public function sort(callable $callback = null): self
114
    {
115
        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
    public function ksort(callable $callback = null): self
126
    {
127
        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
    public function merge(...$arrays): self
139
    {
140
        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
    public function toArray(): self
149
    {
150
        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
    public function except($keys): self
161
    {
162
        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
    public function only($keys): self
173
    {
174
        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
    public function filter(callable $callback, int $mode = 0): self
190
    {
191
        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
    public function flatten(bool $ignore_keys = true): self
202
    {
203
        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
    public function map(callable $callback, bool $recursive = false): self
215
    {
216
        return new self(Arr::map($this->value, $callback, $recursive));
217
    }
218
219
    /**
220
     * Return all the values of an array.
221
     *
222
     * @see  https://php.net/manual/en/function.array-values.php
223
     *
224
     * @return \Helldar\Support\Helpers\Ables\Arrayable
225
     */
226
    public function values(): self
227
    {
228
        return new self(Arr::values($this->value));
229
    }
230
}
231