1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Nip\Collections\Collection; |
4
|
|
|
use Nip\Utility\Arr; |
5
|
|
|
|
6
|
|
|
if (! function_exists('collect')) { |
7
|
|
|
/** |
8
|
|
|
* Create a collection from the given value. |
9
|
|
|
* |
10
|
|
|
* @param mixed $value |
11
|
|
|
* @return Collection |
12
|
|
|
*/ |
13
|
|
|
function collect($value = null) |
14
|
|
|
{ |
15
|
|
|
return new Collection($value); |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
if (!function_exists('data_get')) { |
20
|
|
|
/** |
21
|
|
|
* Get an item from an array or object using "dot" notation. |
22
|
|
|
* |
23
|
|
|
* @param mixed $target |
24
|
|
|
* @param string|array|int|null $key |
25
|
|
|
* @param mixed $default |
26
|
|
|
* @return mixed |
27
|
|
|
*/ |
28
|
|
|
function data_get($target, $key, $default = null) |
29
|
|
|
{ |
30
|
|
|
if (is_null($key)) { |
31
|
|
|
return $target; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$key = is_array($key) ? $key : explode('.', $key); |
35
|
|
|
|
36
|
|
|
foreach ($key as $i => $segment) { |
37
|
|
|
unset($key[$i]); |
38
|
|
|
|
39
|
|
|
if (is_null($segment)) { |
40
|
|
|
return $target; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if ($segment === '*') { |
44
|
|
|
if ($target instanceof Collection) { |
45
|
|
|
$target = $target->all(); |
46
|
|
|
} elseif (!is_array($target)) { |
47
|
|
|
return value($default); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$result = []; |
51
|
|
|
|
52
|
|
|
foreach ($target as $item) { |
53
|
|
|
$result[] = data_get($item, $key); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return in_array('*', $key) ? Arr::collapse($result) : $result; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (Arr::accessible($target) && Arr::exists($target, $segment)) { |
60
|
|
|
$target = $target[$segment]; |
61
|
|
|
} elseif (is_object($target)) { |
62
|
|
|
if (isset($target->{$segment})) { |
63
|
|
|
$target = $target->{$segment}; |
64
|
|
|
} elseif (method_exists($target, $segment)) { |
65
|
|
|
$target = $target->$segment(); |
66
|
|
|
} |
67
|
|
|
} else { |
68
|
|
|
return value($default); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $target; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
if (!function_exists('data_set')) { |
78
|
|
|
/** |
79
|
|
|
* Set an item on an array or object using dot notation. |
80
|
|
|
* |
81
|
|
|
* @param mixed $target |
82
|
|
|
* @param string|array $key |
83
|
|
|
* @param mixed $value |
84
|
|
|
* @param bool $overwrite |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
function data_set(&$target, $key, $value, $overwrite = true) |
88
|
|
|
{ |
89
|
|
|
$segments = is_array($key) ? $key : explode('.', $key); |
90
|
|
|
|
91
|
|
|
if (($segment = array_shift($segments)) === '*') { |
92
|
|
|
if (!Arr::accessible($target)) { |
93
|
|
|
$target = []; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($segments) { |
97
|
|
|
foreach ($target as &$inner) { |
98
|
|
|
data_set($inner, $segments, $value, $overwrite); |
99
|
|
|
} |
100
|
|
|
} elseif ($overwrite) { |
101
|
|
|
foreach ($target as &$inner) { |
102
|
|
|
$inner = $value; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} elseif (Arr::accessible($target)) { |
106
|
|
|
if ($segments) { |
107
|
|
|
if (!Arr::exists($target, $segment)) { |
108
|
|
|
$target[$segment] = []; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
data_set($target[$segment], $segments, $value, $overwrite); |
112
|
|
|
} elseif ($overwrite || !Arr::exists($target, $segment)) { |
113
|
|
|
$target[$segment] = $value; |
114
|
|
|
} |
115
|
|
|
} elseif (is_object($target)) { |
116
|
|
|
if ($segments) { |
117
|
|
|
if (!isset($target->{$segment})) { |
118
|
|
|
$target->{$segment} = []; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
data_set($target->{$segment}, $segments, $value, $overwrite); |
122
|
|
|
} elseif ($overwrite || !isset($target->{$segment})) { |
123
|
|
|
$target->{$segment} = $value; |
124
|
|
|
} |
125
|
|
|
} else { |
126
|
|
|
$target = []; |
127
|
|
|
|
128
|
|
|
if ($segments) { |
129
|
|
|
data_set($target[$segment], $segments, $value, $overwrite); |
130
|
|
|
} elseif ($overwrite) { |
131
|
|
|
$target[$segment] = $value; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $target; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (!function_exists('head')) { |
140
|
|
|
/** |
141
|
|
|
* Get the first element of an array. Useful for method chaining. |
142
|
|
|
* |
143
|
|
|
* @param array $array |
144
|
|
|
* @return mixed |
145
|
|
|
*/ |
146
|
|
|
function head($array) |
147
|
|
|
{ |
148
|
|
|
return reset($array); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (!function_exists('last')) { |
153
|
|
|
/** |
154
|
|
|
* Get the last element from an array. |
155
|
|
|
* |
156
|
|
|
* @param array $array |
157
|
|
|
* @return mixed |
158
|
|
|
*/ |
159
|
|
|
function last($array) |
160
|
|
|
{ |
161
|
|
|
return end($array); |
162
|
|
|
} |
163
|
|
|
} |