arrayFlatten()   B
last analyzed

Complexity

Conditions 7
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 11
nc 1
nop 2
dl 0
loc 19
rs 8.8333
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Procedural wrappers for various functions.
5
 *
6
 * This file is part of PinkCrab Function Constructors.
7
 *
8
 * PinkCrab Function Constructors is free software: you can redistribute it and/or modify it under the terms of the
9
 * GNU General Public License as published by the Free Software Foundation, either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * PinkCrab Function Constructors is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
13
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 * See the GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with PinkCrab Function Constructors.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 *
19
 * @author Glynn Quelch <[email protected]>
20
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
21
 * @package PinkCrab\FunctionConstructors
22
 */
23
24
declare(strict_types=1);
25
26
if (! function_exists('stringContains')) {
27
    /**
28
     * Checks if a string contains a sub string
29
     *
30
     * @param string $haystack The string to search within.
31
     * @param string $needle The sub string to look for.
32
     * @return bool
33
     */
34
    function stringContains(string $haystack, string $needle): bool
35
    {
36
        return strpos($haystack, $needle) !== false;
37
    }
38
}
39
40
if (! function_exists('array_flatten')) {
41
    /**
42
     * Flattens an array to desired depth.
43
     * doesn't preserve keys
44
     *
45
     * @param mixed[] $array The array to flatten
46
     * @param int|null $n The depth to flatten the array, if null will flatten all arrays.
47
     * @return mixed[]
48
     */
49
    function arrayFlatten(array $array, ?int $n = null): array
50
    {
51
        return array_reduce(
52
            $array,
53
            function (array $carry, $element) use ($n): array {
54
                // Remove empty arrays.
55
                if (is_array($element) && empty($element)) {
56
                    return $carry;
57
                }
58
                // If the element is an array and we are still flattening, call again
59
                if (is_array($element) && (is_null($n) || $n > 0)) { // @phpstan-ignore-line
60
                    $carry = array_merge($carry, arrayFlatten($element, $n ? $n - 1 : null));
61
                } else {
62
                    // Else just add the element.
63
                    $carry[] = $element;
64
                }
65
                return $carry;
66
            },
67
            array()
68
        );
69
    }
70
}
71
72
if (! function_exists('toObject')) {
73
    /**
74
     * Simple mapper for turning arrays into stdClass objects.
75
     *
76
     * @param array<string|int, mixed> $array
77
     * @return stdClass
78
     */
79
    function toObject(array $array): object
80
    {
81
        $object = new stdClass();
82
        foreach ($array as $key => $value) {
83
            $key            = is_string($key) ? $key : sprintf('__%s', (string) $key);
84
            $object->{$key} = $value;
85
        }
86
        return $object;
87
    }
88
}
89
90
if (! function_exists('invokeCallable')) {
91
    /**
92
     * Used to invoke a callable.
93
     *
94
     * @param callable(mixed ...$args):void $fn
95
     * @param mixed ...$args
96
     * @return void
97
     */
98
    function invokeCallable(callable $fn, ...$args): void
99
    {
100
        $fn(...$args);
101
    }
102
}
103
104
if (! function_exists('isArrayAccess')) {
105
    /**
106
     * Checks if an array or an object which has array like access.
107
     *
108
     * @source https://stackoverflow.com/questions/12346479/how-to-check-for-arrayness-in-php
109
     * @param mixed $var
110
     * @return bool
111
     */
112
    function isArrayAccess($var)
113
    {
114
        return is_array($var) ||
115
           ($var instanceof \ArrayAccess &&
116
            $var instanceof \Traversable &&
117
            $var instanceof \Serializable &&
118
            $var instanceof \Countable);
119
    }
120
}
121