Passed
Pull Request — main (#22)
by Andrey
22:20 queued 08:45
created

Arr::isArrayable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 1
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Helldar\Support\Helpers;
4
5
use ArrayAccess;
6
use Helldar\Support\Facades\Helpers\Filesystem\File;
7
use Helldar\Support\Facades\Tools\Stub;
8
use Helldar\Support\Tools\Stub as StubTool;
9
10
class Arr
11
{
12
    /**
13
     * Renaming array keys.
14
     * As the second parameter, a callback function is passed, which determines the actions for processing the value.
15
     * The output of the function must be a string with a name.
16
     *
17
     * @param  array  $array
18
     * @param  callable  $callback
19
     *
20
     * @return array
21
     */
22
    public function renameKeys(array $array, callable $callback): array
23
    {
24
        $result = [];
25
26
        foreach ($array as $key => $value) {
27
            $new = $callback($key, $value);
28
29
            $result[$new] = $value;
30
        }
31
32
        return $result;
33
    }
34
35
    /**
36
     * Renaming array keys with map.
37
     *
38
     * @param  array  $array
39
     * @param  array  $map
40
     *
41
     * @return array
42
     */
43
    public function renameKeysMap(array $array, array $map): array
44
    {
45
        return $this->renameKeys($array, static function ($key) use ($map) {
46
            return $map[$key] ?? $key;
47
        });
48
    }
49
50
    /**
51
     * Get the size of the longest text element of the array.
52
     *
53
     * @param  array  $array
54
     *
55
     * @return int
56
     */
57
    public function longestStringLength(array $array): int
58
    {
59
        return ! empty($array)
60
            ? max(array_map('mb_strlen', $array))
61
            : 0;
62
    }
63
64
    /**
65
     * Push one a unique element onto the end of array.
66
     *
67
     * @param  array  $array
68
     * @param  array|mixed  $values
69
     *
70
     * @return array
71
     */
72
    public function addUnique(array $array, $values): array
73
    {
74
        if ($this->isArrayable($values)) {
75
            foreach ($values as $value) {
76
                $array = $this->addUnique($array, $value);
77
            }
78
        } else {
79
            array_push($array, $values);
80
        }
81
82
        return array_values(array_unique($array));
83
    }
84
85
    /**
86
     * Sort an associative array in the order specified by an array of keys.
87
     *
88
     * Example:
89
     *
90
     *  $arr = ['q' => 1, 'r' => 2, 's' => 5, 'w' => 123];
91
     *
92
     *  Arr::sortByKeys($arr, ['q', 'w', 'e']);
93
     *
94
     * print_r($arr);
95
     *
96
     *   Array
97
     *   (
98
     *     [q] => 1
99
     *     [w] => 123
100
     *     [r] => 2
101
     *     [s] => 5
102
     *   )
103
     *
104
     * @see https://gist.github.com/Ellrion/a3145621f936aa9416f4c04987533d8d#file-helper-php
105
     *
106
     * @param  array  $array
107
     * @param  array  $sorter
108
     *
109
     * @return array
110
     */
111
    public function sortByKeys(array $array, array $sorter): array
112
    {
113
        $sorter = array_intersect($sorter, array_keys($array));
114
        $array  = array_merge(array_flip($sorter), $array);
115
116
        return $array;
117
    }
118
119
    /**
120
     * Merge one or more arrays recursively.
121
     * Don't forget that numeric keys NOT will be renumbered!
122
     *
123
     * @param  array[]  ...$arrays
124
     *
125
     * @return array
126
     */
127
    public function merge(...$arrays): array
128
    {
129
        $result = [];
130
131
        foreach ($arrays as $array) {
132
            foreach ($array as $key => $value) {
133
                if (is_array($value)) {
134
                    $value = $this->merge($result[$key] ?? [], $value);
135
                }
136
137
                $result[$key] = $value;
138
            }
139
        }
140
141
        ksort($result);
142
143
        return $result;
144
    }
145
146
    public function wrap($value = null): array
147
    {
148
        if (is_array($value)) {
149
            return $value;
150
        }
151
152
        return ! empty($value) ? [$value] : [];
153
    }
154
155
    public function toArray($value = null): array
156
    {
157
        if (is_object($value)) {
158
            $value = method_exists($value, 'toArray') ? $value->toArray() : get_object_vars($value);
159
        }
160
161
        $array = $this->wrap($value);
162
163
        foreach ($array as &$item) {
164
            $item = $this->isArrayable($item) ? $this->toArray($item) : $item;
165
        }
166
167
        return $array;
168
    }
169
170
    /**
171
     * @param  array|\ArrayAccess  $array
172
     * @param  mixed  $key
173
     *
174
     * @return bool
175
     */
176
    public function exists($array, $key): bool
177
    {
178
        if ($array instanceof ArrayAccess) {
179
            return $array->offsetExists($key);
180
        }
181
182
        return isset($array[$key]);
183
    }
184
185
    /**
186
     * @param  array|ArrayAccess  $array
187
     * @param  mixed  $key
188
     * @param  mixed|null  $default
189
     *
190
     * @return mixed|null
191
     */
192
    public function get($array, $key, $default = null)
193
    {
194
        return $array[$key] ?? $default;
195
    }
196
197
    /**
198
     * @param  array|ArrayAccess  $array
199
     * @param  mixed  $key
200
     * @param  null  $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
201
     *
202
     * @return mixed|null
203
     */
204
    public function getKey($array, $key, $default = null)
205
    {
206
        return $this->exists($array, $key) ? $key : $default;
207
    }
208
209
    /**
210
     * @param  array|ArrayAccess  $array
211
     * @param $keys
212
     *
213
     * @return array
214
     */
215
    public function except($array, $keys): array
216
    {
217
        $keys = (array) $keys;
218
219
        return array_filter($array, static function ($key) use ($keys) {
0 ignored issues
show
Bug introduced by
It seems like $array can also be of type ArrayAccess; however, parameter $input of array_filter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

219
        return array_filter(/** @scrutinizer ignore-type */ $array, static function ($key) use ($keys) {
Loading history...
220
            return empty($keys) || ! in_array($key, $keys);
221
        }, ARRAY_FILTER_USE_KEY);
222
    }
223
224
    /**
225
     * Get a subset of the items from the given array.
226
     *
227
     * @param  array|ArrayAccess  $array
228
     * @param  array|string  $keys
229
     *
230
     * @return array
231
     */
232
    public function only($array, $keys): array
233
    {
234
        return array_intersect_key($array, array_flip((array) $keys));
0 ignored issues
show
Bug introduced by
It seems like $array can also be of type ArrayAccess; however, parameter $array1 of array_intersect_key() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

234
        return array_intersect_key(/** @scrutinizer ignore-type */ $array, array_flip((array) $keys));
Loading history...
235
    }
236
237
    /**
238
     * @param  array|ArrayAccess  $array
239
     * @param  callable  $callback
240
     *
241
     * @return array
242
     */
243
    public function map($array, callable $callback): array
244
    {
245
        foreach ($array as $key => &$value) {
246
            $value = $callback($value, $key);
247
        }
248
249
        return $array;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $array could return the type ArrayAccess which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
250
    }
251
252
    public function isArrayable($value = null): bool
253
    {
254
        return is_array($value) || is_object($value) || $value instanceof ArrayAccess;
255
    }
256
257
    /**
258
     * @param  array|ArrayAccess  $array
259
     * @param  string  $path
260
     * @param  bool  $is_json
261
     * @param  bool  $sort_keys
262
     */
263
    public function store($array, string $path, bool $is_json = false, bool $sort_keys = false): void
264
    {
265
        $is_json
266
            ? $this->storeAsJson($path, $array, $sort_keys)
267
            : $this->storeAsArray($path, $array, $sort_keys);
268
    }
269
270
    /**
271
     * @param  string  $path
272
     * @param  array|ArrayAccess  $array
273
     * @param  bool  $sort_keys
274
     */
275
    public function storeAsJson(string $path, $array, bool $sort_keys = false): void
276
    {
277
        $this->prepareToStore($path, StubTool::JSON, $array, static function (array $array) {
0 ignored issues
show
Bug introduced by
It seems like $array can also be of type ArrayAccess; however, parameter $array of Helldar\Support\Helpers\Arr::prepareToStore() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

277
        $this->prepareToStore($path, StubTool::JSON, /** @scrutinizer ignore-type */ $array, static function (array $array) {
Loading history...
278
            return json_encode($array);
279
        }, $sort_keys);
280
    }
281
282
    /**
283
     * @param  string  $path
284
     * @param  array|ArrayAccess  $array
285
     * @param  bool  $sort_keys
286
     */
287
    public function storeAsArray(string $path, $array, bool $sort_keys = false): void
288
    {
289
        $this->prepareToStore($path, StubTool::PHP_ARRAY, $array, static function (array $array) {
0 ignored issues
show
Bug introduced by
It seems like $array can also be of type ArrayAccess; however, parameter $array of Helldar\Support\Helpers\Arr::prepareToStore() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

289
        $this->prepareToStore($path, StubTool::PHP_ARRAY, /** @scrutinizer ignore-type */ $array, static function (array $array) {
Loading history...
290
            return var_export($array, true);
291
        }, $sort_keys);
292
    }
293
294
    protected function prepareToStore(string $path, string $stub, array $array, callable $replace, bool $sort_keys = false): void
295
    {
296
        if ($sort_keys) {
297
            ksort($array);
298
        }
299
300
        $content = Stub::replace($stub, [
301
            '{{slot}}' => $replace($array),
302
        ]);
303
304
        File::store($path, $content);
305
    }
306
}
307