data_set()   D
last analyzed

Complexity

Conditions 20
Paths 34

Size

Total Lines 49
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 31
nc 34
nop 4
dl 0
loc 49
rs 4.1666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php 
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file license.md.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2022 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
21
 */
22
23
use Syscodes\Components\Support\Arr;
24
use Syscodes\Components\Support\Collection;
25
use Syscodes\Components\Support\HigherOrderTakeProxy;
26
27
if ( ! function_exists('collect')) {
28
    /**
29
     * Create a collection from the given value.
30
     * 
31
     * @param  mixed  $value
32
     * 
33
     * @return \Syscodes\Components\Support\Collection
34
     */
35
    function collect($value = null)
36
    {
37
        return new Collection($value);
38
    }
39
}
40
41
if ( ! function_exists('data_get')) {
42
    /**
43
     * Get an item from an array or object using "dot" notation.
44
     * 
45
     * @param  mixed   $target
46
     * @param  string|array  $key
47
     * @param  mixed   $default
48
     * 
49
     * @return mixed
50
     */
51
    function data_get($target, $key, $default = null)
52
    {
53
        if (is_null($key)) return $target;
0 ignored issues
show
introduced by
The condition is_null($key) is always false.
Loading history...
54
        
55
        $key = is_array($key) ? $key : explode('.', $key);
56
        
57
        while (($segment = array_shift($key)) !== null) {
58
            if ($segment === '*') {
59
                if ($target instanceof Collection) {
60
                    $target = $target->all();
61
                } elseif (! is_array($target)) {
62
                    return value($default);
63
                }
64
                
65
                $result = Arr::pluck($target, $key);
66
                
67
                return in_array('*', $key) ? Arr::collapse($result) : $result;
68
            }
69
            
70
            if (Arr::accessible($target) && Arr::exists($target, $segment)) {
71
                $target = $target[$segment];
72
            } elseif (is_object($target) && isset($target->{$segment})) {
73
                $target = $target->{$segment};
74
            } else {
75
                return value($default);
76
            }
77
        }
78
        
79
        return $target;
80
    }
81
}
82
83
if( ! function_exists('data_set')) {
84
    /**
85
     * Set an item on an array or object using dot notation.
86
     * 
87
     * @param  mixed  $target
88
     * @param  string|array  $key
89
     * @param  mixed  $value
90
     * @param  bool  $overwrite
91
     * 
92
     * @return mixed
93
     */
94
    function data_set(&$target, $key, $value, $overwrite = true)
95
    {
96
        $segments = is_array($key) ? $key : explode('.', $key);
97
        
98
        if (($segment = array_shift($segments)) === '*') {
99
            if ( ! Arr::accessible($target)) {
100
                $target = [];
101
            }
102
            
103
            if ($segments) {
104
                foreach ($target as &$inner) {
105
                    data_set($inner, $segments, $value, $overwrite);
106
                }
107
            } elseif ($overwrite) {
108
                foreach ($target as &$inner) {
109
                    $inner = $value;
110
                }
111
            }
112
        } elseif (Arr::accessible($target)) {
113
            if ($segments) {
114
                if ( ! Arr::exists($target, $segment)) {
115
                    $target[$segment] = [];
116
                }
117
                
118
                data_set($target[$segment], $segments, $value, $overwrite);
119
            } elseif ($overwrite || ! Arr::exists($target, $segment)) {
120
                $target[$segment] = $value;
121
            }
122
        } elseif (is_object($target)) {
123
            if ($segments) {
124
                if ( ! isset($target->{$segment})) {
125
                    $target->{$segment} = [];
126
                }
127
                
128
                data_set($target->{$segment}, $segments, $value, $overwrite);
129
            } elseif ($overwrite || ! isset($target->{$segment})) {
130
                $target->{$segment} = $value;
131
            }
132
        } else {
133
            $target = [];
134
            
135
            if ($segments) {
136
                data_set($target[$segment], $segments, $value, $overwrite);
137
            } elseif ($overwrite) {
138
                $target[$segment] = $value;
139
            }
140
        }
141
        
142
        return $target;
143
    }
144
}
145
146
if ( ! function_exists('headItem')) {
147
    /**
148
     * Get the actual element of an array. Useful for method chaining.
149
     *
150
     * @param  array  $array
151
     *
152
     * @return mixed
153
     */
154
    function headItem($array, $bool = false)
155
    {
156
        return $bool ? reset($array) : current($array);
157
    }
158
}
159
160
if ( ! function_exists('lastItem')) {
161
    /**
162
     * Get the last element from an array.
163
     *
164
     * @param  array  $array
165
     *
166
     * @return mixed
167
     */
168
    function lastItem($array)
169
    {
170
        return end($array);
171
    }
172
}
173
174
if ( ! function_exists('take')) {
175
    /**
176
     * Call the given Closure if this activated then return the value.
177
     * 
178
     * @param  string  $value
179
     * @param  \Closure|null  $callback
180
     * 
181
     * @return mixed
182
     * 
183
     * @uses   \Syscodes\Components\Support\HigherOrderTakeProxy
184
     */
185
    function take($value, $callback = null)
186
    {
187
        if (is_null($callback)) {
188
            return new HigherOrderTakeProxy($value);
189
        }
190
191
        $callback($value);
192
193
        return $value;
194
    }
195
}
196
197
if ( ! function_exists('value')) {
198
    /**
199
     * Return the default value of the given value.
200
     *
201
     * @param  mixed  $value
202
     * 
203
     * @return mixed
204
     */
205
    function value($value)
206
    {
207
        return $value instanceof \Closure ? $value() : $value;
208
    }
209
}