Debian Codegraph @

lib/3rdParty/composer/illuminate/collections/helpers.php

raw symbols SCIP
1 <?php
2
3 use Illuminate\Support\Arr;
4 use Illuminate\Support\Collection;
5
6 if (! function_exists('collect')) {
7 /**
8 * Create a collection from the given value.
9 *
10 * @template TKey of array-key
11 * @template TValue
12 *
13 * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue>|null $value
14 * @return \Illuminate\Support\Collection<TKey, TValue>
15 */
16 function collect($value = []): Collection
17 {
18 return new Collection($value);
19 }
20 }
21
22 if (! function_exists('data_fill')) {
23 /**
24 * Fill in data where it's missing.
25 *
26 * @param mixed $target
27 * @param string|array $key
28 * @param mixed $value
29 * @return mixed
30 */
31 function data_fill(&$target, $key, $value)
32 {
33 return data_set($target, $key, $value, false);
34 }
35 }
36
37 if (! function_exists('data_has')) {
38 /**
39 * Determine if a key / property exists on an array or object using "dot" notation.
40 *
41 * @param mixed $target
42 * @param string|array|int|null $key
43 * @return bool
44 */
45 function data_has($target, $key): bool
46 {
47 if (is_null($key) || $key === []) {
48 return false;
49 }
50
51 $key = is_array($key) ? $key : explode('.', $key);
52
53 foreach ($key as $segment) {
54 if (Arr::accessible($target) && Arr::exists($target, $segment)) {
55 $target = $target[$segment];
56 } elseif (is_object($target) && property_exists($target, $segment)) {
57 $target = $target->{$segment};
58 } else {
59 return false;
60 }
61 }
62
63 return true;
64 }
65 }
66
67 if (! function_exists('data_get')) {
68 /**
69 * Get an item from an array or object using "dot" notation.
70 *
71 * @param mixed $target
72 * @param string|array|int|null $key
73 * @param mixed $default
74 * @return mixed
75 */
76 function data_get($target, $key, $default = null)
77 {
78 if (is_null($key)) {
79 return $target;
80 }
81
82 $key = is_array($key) ? $key : explode('.', $key);
83
84 foreach ($key as $i => $segment) {
85 unset($key[$i]);
86
87 if (is_null($segment)) {
88 return $target;
89 }
90
91 if ($segment === '*') {
92 if ($target instanceof Collection) {
93 $target = $target->all();
94 } elseif (! is_iterable($target)) {
95 return value($default);
96 }
97
98 $result = [];
99
100 foreach ($target as $item) {
101 $result[] = data_get($item, $key);
102 }
103
104 return in_array('*', $key) ? Arr::collapse($result) : $result;
105 }
106
107 $segment = match ($segment) {
108 '\*' => '*',
109 '\{first}' => '{first}',
110 '{first}' => array_key_first(Arr::from($target)),
111 '\{last}' => '{last}',
112 '{last}' => array_key_last(Arr::from($target)),
113 default => $segment,
114 };
115
116 if (Arr::accessible($target) && Arr::exists($target, $segment)) {
117 $target = $target[$segment];
118 } elseif (is_object($target) && isset($target->{$segment})) {
119 $target = $target->{$segment};
120 } else {
121 return value($default);
122 }
123 }
124
125 return $target;
126 }
127 }
128
129 if (! function_exists('data_set')) {
130 /**
131 * Set an item on an array or object using dot notation.
132 *
133 * @param mixed $target
134 * @param string|array $key
135 * @param mixed $value
136 * @param bool $overwrite
137 * @return mixed
138 */
139 function data_set(&$target, $key, $value, $overwrite = true)
140 {
141 $segments = is_array($key) ? $key : explode('.', $key);
142
143 if (($segment = array_shift($segments)) === '*') {
144 if (! Arr::accessible($target)) {
145 $target = [];
146 }
147
148 if ($segments) {
149 foreach ($target as &$inner) {
150 data_set($inner, $segments, $value, $overwrite);
151 }
152 } elseif ($overwrite) {
153 foreach ($target as &$inner) {
154 $inner = $value;
155 }
156 }
157 } elseif (Arr::accessible($target)) {
158 if ($segments) {
159 if (! Arr::exists($target, $segment)) {
160 $target[$segment] = [];
161 }
162
163 data_set($target[$segment], $segments, $value, $overwrite);
164 } elseif ($overwrite || ! Arr::exists($target, $segment)) {
165 $target[$segment] = $value;
166 }
167 } elseif (is_object($target)) {
168 if ($segments) {
169 if (! isset($target->{$segment})) {
170 $target->{$segment} = [];
171 }
172
173 data_set($target->{$segment}, $segments, $value, $overwrite);
174 } elseif ($overwrite || ! isset($target->{$segment})) {
175 $target->{$segment} = $value;
176 }
177 } else {
178 $target = [];
179
180 if ($segments) {
181 data_set($target[$segment], $segments, $value, $overwrite);
182 } elseif ($overwrite) {
183 $target[$segment] = $value;
184 }
185 }
186
187 return $target;
188 }
189 }
190
191 if (! function_exists('data_forget')) {
192 /**
193 * Remove / unset an item from an array or object using "dot" notation.
194 *
195 * @param mixed $target
196 * @param string|array|int|null $key
197 * @return mixed
198 */
199 function data_forget(&$target, $key)
200 {
201 $segments = is_array($key) ? $key : explode('.', $key);
202
203 if (($segment = array_shift($segments)) === '*' && Arr::accessible($target)) {
204 if ($segments) {
205 foreach ($target as &$inner) {
206 data_forget($inner, $segments);
207 }
208 }
209 } elseif (Arr::accessible($target)) {
210 if ($segments && Arr::exists($target, $segment)) {
211 data_forget($target[$segment], $segments);
212 } else {
213 Arr::forget($target, $segment);
214 }
215 } elseif (is_object($target)) {
216 if ($segments && isset($target->{$segment})) {
217 data_forget($target->{$segment}, $segments);
218 } elseif (isset($target->{$segment})) {
219 unset($target->{$segment});
220 }
221 }
222
223 return $target;
224 }
225 }
226
227 if (! function_exists('head')) {
228 /**
229 * Get the first element of an array. Useful for method chaining.
230 *
231 * @param array $array
232 * @return mixed
233 */
234 function head($array)
235 {
236 return empty($array) ? false : array_first($array);
237 }
238 }
239
240 if (! function_exists('last')) {
241 /**
242 * Get the last element from an array.
243 *
244 * @param array $array
245 * @return mixed
246 */
247 function last($array)
248 {
249 return empty($array) ? false : array_last($array);
250 }
251 }
252
253 if (! function_exists('value')) {
254 /**
255 * Return the default value of the given value.
256 *
257 * @template TValue
258 * @template TArgs
259 *
260 * @param TValue|\Closure(TArgs): TValue $value
261 * @param TArgs ...$args
262 * @return TValue
263 */
264 function value($value, ...$args)
265 {
266 return $value instanceof Closure ? $value(...$args) : $value;
267 }
268 }
269
270 if (! function_exists('when')) {
271 /**
272 * Return a value if the given condition is true.
273 *
274 * @template TValue
275 * @template TArgs
276 * @template TDefault
277 *
278 * @param mixed $condition
279 * @param TValue|\Closure(TArgs): TValue $value
280 * @param TDefault|\Closure(): TDefault $default
281 * @return ($condition is true|positive-int|non-falsy-string|non-empty-array ? TValue : ($condition is callable ? TValue|TDefault : TDefault))
282 */
283 function when($condition, $value, $default = null)
284 {
285 $condition = $condition instanceof Closure ? $condition() : $condition;
286
287 if ($condition) {
288 return value($value, $condition);
289 }
290
291 return value($default, $condition);
292 }
293 }