1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use IlluminateAgnostic\Arr\Support\Arr; |
4
|
|
|
use IlluminateAgnostic\Arr\Support\Collection; |
5
|
|
|
use IlluminateAgnostic\Arr\Support\Debug\Dumper; |
6
|
|
|
|
7
|
|
|
if (!class_exists(Illuminate\Support\Collection::class)) { |
8
|
|
|
if (!function_exists('collect')) { |
9
|
|
|
/** |
10
|
|
|
* Create a collection from the given value. |
11
|
|
|
* |
12
|
|
|
* @param mixed $value |
13
|
|
|
* @return \IlluminateAgnostic\ArrAgnostic\Arr\Support\Collection |
14
|
|
|
*/ |
15
|
|
|
function collect($value = null) |
|
|
|
|
16
|
|
|
{ |
17
|
5 |
|
return new Collection($value); |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
if (!function_exists('data_get')) { |
22
|
|
|
/** |
23
|
|
|
* Get an item from an array or object using "dot" notation. |
24
|
|
|
* |
25
|
|
|
* @param mixed $target |
26
|
|
|
* @param string|array $key |
27
|
|
|
* @param mixed $default |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
|
|
function data_get($target, $key, $default = null) |
|
|
|
|
31
|
|
|
{ |
32
|
98 |
|
if (is_null($key)) { |
33
|
26 |
|
return $target; |
34
|
|
|
} |
35
|
|
|
|
36
|
86 |
|
$key = is_array($key) ? $key : explode('.', $key); |
37
|
|
|
|
38
|
86 |
|
while (!is_null($segment = array_shift($key))) { |
39
|
86 |
|
if ($segment === '*') { |
40
|
1 |
|
if ($target instanceof Collection) { |
41
|
|
|
$target = $target->all(); |
42
|
1 |
|
} elseif (!is_array($target)) { |
43
|
|
|
return value($default); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
$result = Arr::pluck($target, $key); |
47
|
|
|
|
48
|
1 |
|
return in_array('*', $key) ? Arr::collapse($result) : $result; |
49
|
|
|
} |
50
|
|
|
|
51
|
86 |
|
if (Arr::accessible($target) && Arr::exists($target, $segment)) { |
52
|
72 |
|
$target = $target[$segment]; |
53
|
29 |
|
} elseif (is_object($target) && isset($target->{$segment})) { |
54
|
25 |
|
$target = $target->{$segment}; |
55
|
|
|
} else { |
56
|
8 |
|
return value($default); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
86 |
|
return $target; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!function_exists('data_set')) { |
65
|
|
|
/** |
66
|
|
|
* Set an item on an array or object using dot notation. |
67
|
|
|
* |
68
|
|
|
* @param mixed $target |
69
|
|
|
* @param string|array $key |
70
|
|
|
* @param mixed $value |
71
|
|
|
* @param bool $overwrite |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
function data_set(&$target, $key, $value, $overwrite = true) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$segments = is_array($key) ? $key : explode('.', $key); |
77
|
|
|
|
78
|
|
|
if (($segment = array_shift($segments)) === '*') { |
79
|
|
|
if (!Arr::accessible($target)) { |
80
|
|
|
$target = []; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($segments) { |
|
|
|
|
84
|
|
|
foreach ($target as &$inner) { |
85
|
|
|
data_set($inner, $segments, $value, $overwrite); |
86
|
|
|
} |
87
|
|
|
} elseif ($overwrite) { |
88
|
|
|
foreach ($target as &$inner) { |
89
|
|
|
$inner = $value; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} elseif (Arr::accessible($target)) { |
93
|
|
|
if ($segments) { |
|
|
|
|
94
|
|
|
if (!Arr::exists($target, $segment)) { |
95
|
|
|
$target[$segment] = []; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
data_set($target[$segment], $segments, $value, $overwrite); |
99
|
|
|
} elseif ($overwrite || !Arr::exists($target, $segment)) { |
100
|
|
|
$target[$segment] = $value; |
101
|
|
|
} |
102
|
|
|
} elseif (is_object($target)) { |
103
|
|
|
if ($segments) { |
|
|
|
|
104
|
|
|
if (!isset($target->{$segment})) { |
105
|
|
|
$target->{$segment} = []; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
data_set($target->{$segment}, $segments, $value, $overwrite); |
109
|
|
|
} elseif ($overwrite || !isset($target->{$segment})) { |
110
|
|
|
$target->{$segment} = $value; |
111
|
|
|
} |
112
|
|
|
} else { |
113
|
|
|
$target = []; |
114
|
|
|
|
115
|
|
|
if ($segments) { |
|
|
|
|
116
|
|
|
data_set($target[$segment], $segments, $value, $overwrite); |
117
|
|
|
} elseif ($overwrite) { |
118
|
|
|
$target[$segment] = $value; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $target; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (!function_exists('dd')) { |
127
|
|
|
/** |
128
|
|
|
* Dump the passed variables and end the script. |
129
|
|
|
* |
130
|
|
|
* @param mixed $args |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
|
|
function dd(...$args) |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
http_response_code(500); |
136
|
|
|
|
137
|
|
|
foreach ($args as $x) { |
138
|
|
|
(new Dumper())->dump($x); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
die(1); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (!function_exists('value')) { |
146
|
|
|
/** |
147
|
|
|
* Return the default value of the given value. |
148
|
|
|
* |
149
|
|
|
* @param mixed $value |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
|
|
function value($value) |
|
|
|
|
153
|
|
|
{ |
154
|
31 |
|
return $value instanceof Closure ? $value() : $value; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.