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) |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
return $value instanceof Closure ? $value() : $value; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
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.