Completed
Push — master ( a0c1d4...2d9929 )
by Antonio Carlos
01:33
created

helpers.php ➔ array_flatten()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
use IlluminateAgnostic\Arr\Support\Arr;
4
use IlluminateAgnostic\Arr\Support\Collection;
5
use IlluminateAgnostic\Arr\Support\Debug\Dumper;
6
use Illuminate\Support\Collection as IlluminateCollection;
7
8
if (!class_exists(Illuminate\Support\Collection::class)) {
9
    if (!function_exists('collect')) {
10
        /**
11
         * Create a collection from the given value.
12
         *
13
         * @param  mixed  $value
14
         * @return \IlluminateAgnostic\Arr\Support\Collection
15
         */
16
        function collect($value = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
17
        {
18
            return new Collection($value);
19
        }
20
    }
21
22
    if (!function_exists('data_get')) {
23
        /**
24
         * Get an item from an array or object using "dot" notation.
25
         *
26
         * @param  mixed   $target
27
         * @param  string|array  $key
28
         * @param  mixed   $default
29
         * @return mixed
30
         */
31
        function data_get($target, $key, $default = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
32
        {
33
            if (is_null($key)) {
34
                return $target;
35
            }
36
37
            $key = is_array($key) ? $key : explode('.', $key);
38
39
            while (!is_null($segment = array_shift($key))) {
40
                if ($segment === '*') {
41
                    if ($target instanceof Collection) {
42
                        $target = $target->all();
43
                    } elseif (!is_array($target)) {
44
                        return value($default);
45
                    }
46
47
                    $result = Arr::pluck($target, $key);
48
49
                    return in_array('*', $key) ? Arr::collapse($result) : $result;
50
                }
51
52
                if (Arr::accessible($target) && Arr::exists($target, $segment)) {
53
                    $target = $target[$segment];
54
                } elseif (is_object($target) && isset($target->{$segment})) {
55
                    $target = $target->{$segment};
56
                } else {
57
                    return value($default);
58
                }
59
            }
60
61
            return $target;
62
        }
63
    }
64
65
    if (!function_exists('data_set')) {
66
        /**
67
         * Set an item on an array or object using dot notation.
68
         *
69
         * @param  mixed  $target
70
         * @param  string|array  $key
71
         * @param  mixed  $value
72
         * @param  bool  $overwrite
73
         * @return mixed
74
         */
75
        function data_set(&$target, $key, $value, $overwrite = true)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
76
        {
77
            $segments = is_array($key) ? $key : explode('.', $key);
78
79
            if (($segment = array_shift($segments)) === '*') {
80
                if (!Arr::accessible($target)) {
81
                    $target = [];
82
                }
83
84
                if ($segments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $segments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
85
                    foreach ($target as &$inner) {
86
                        data_set($inner, $segments, $value, $overwrite);
87
                    }
88
                } elseif ($overwrite) {
89
                    foreach ($target as &$inner) {
90
                        $inner = $value;
91
                    }
92
                }
93
            } elseif (Arr::accessible($target)) {
94
                if ($segments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $segments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
95
                    if (!Arr::exists($target, $segment)) {
96
                        $target[$segment] = [];
97
                    }
98
99
                    data_set($target[$segment], $segments, $value, $overwrite);
100
                } elseif ($overwrite || !Arr::exists($target, $segment)) {
101
                    $target[$segment] = $value;
102
                }
103
            } elseif (is_object($target)) {
104
                if ($segments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $segments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
105
                    if (!isset($target->{$segment})) {
106
                        $target->{$segment} = [];
107
                    }
108
109
                    data_set($target->{$segment}, $segments, $value, $overwrite);
110
                } elseif ($overwrite || !isset($target->{$segment})) {
111
                    $target->{$segment} = $value;
112
                }
113
            } else {
114
                $target = [];
115
116
                if ($segments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $segments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
117
                    data_set($target[$segment], $segments, $value, $overwrite);
118
                } elseif ($overwrite) {
119
                    $target[$segment] = $value;
120
                }
121
            }
122
123
            return $target;
124
        }
125
    }
126
127
    if (!function_exists('dd')) {
128
        /**
129
         * Dump the passed variables and end the script.
130
         *
131
         * @param  mixed  $args
132
         * @return void
133
         */
134
        function dd(...$args)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
135
        {
136
            http_response_code(500);
137
138
            foreach ($args as $x) {
139
                (new Dumper())->dump($x);
140
            }
141
142
            die(1);
143
        }
144
    }
145
146
    if (!function_exists('value')) {
147
        /**
148
         * Return the default value of the given value.
149
         *
150
         * @param  mixed  $value
151
         * @return mixed
152
         */
153
        function value($value)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
154
        {
155
            return $value instanceof Closure ? $value() : $value;
156
        }
157
    }
158
}
159