Debian Codegraph @

lib/3rdParty/composer/illuminate/support/Traits/InteractsWithData.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support\Traits;
4
5 use Carbon\CarbonInterval;
6 use Carbon\Unit;
7 use Illuminate\Support\Arr;
8 use Illuminate\Support\Collection;
9 use Illuminate\Support\Facades\Date;
10 use Illuminate\Support\Number;
11 use Illuminate\Support\Str;
12 use stdClass;
13
14 use function Illuminate\Support\enum_value;
15
16 trait InteractsWithData
17 {
18 /**
19 * Retrieve all data from the instance.
20 *
21 * @param mixed $keys
22 * @return array
23 */
24 abstract public function all($keys = null);
25
26 /**
27 * Retrieve data from the instance.
28 *
29 * @param string|null $key
30 * @param mixed $default
31 * @return mixed
32 */
33 abstract protected function data($key = null, $default = null);
34
35 /**
36 * Determine if the data contains a given key.
37 *
38 * @param string|array $key
39 * @return bool
40 */
41 public function exists($key)
42 {
43 return $this->has($key);
44 }
45
46 /**
47 * Determine if the data contains a given key.
48 *
49 * @param string|array $key
50 * @return bool
51 */
52 public function has($key)
53 {
54 $keys = is_array($key) ? $key : func_get_args();
55
56 $data = $this->all();
57
58 foreach ($keys as $value) {
59 if (! Arr::has($data, $value)) {
60 return false;
61 }
62 }
63
64 return true;
65 }
66
67 /**
68 * Determine if the instance contains any of the given keys.
69 *
70 * @param string|array $keys
71 * @return bool
72 */
73 public function hasAny($keys)
74 {
75 $keys = is_array($keys) ? $keys : func_get_args();
76
77 $data = $this->all();
78
79 return Arr::hasAny($data, $keys);
80 }
81
82 /**
83 * Apply the callback if the instance contains the given key.
84 *
85 * @param string $key
86 * @param callable $callback
87 * @param callable|null $default
88 * @return $this|mixed
89 */
90 public function whenHas($key, callable $callback, ?callable $default = null)
91 {
92 if ($this->has($key)) {
93 return $callback(data_get($this->all(), $key)) ?: $this;
94 }
95
96 if ($default) {
97 return $default();
98 }
99
100 return $this;
101 }
102
103 /**
104 * Determine if the instance contains a non-empty value for the given key.
105 *
106 * @param string|array $key
107 * @return bool
108 */
109 public function filled($key)
110 {
111 $keys = is_array($key) ? $key : func_get_args();
112
113 foreach ($keys as $value) {
114 if ($this->isEmptyString($value)) {
115 return false;
116 }
117 }
118
119 return true;
120 }
121
122 /**
123 * Determine if the instance contains an empty value for the given key.
124 *
125 * @param string|array $key
126 * @return bool
127 */
128 public function isNotFilled($key)
129 {
130 $keys = is_array($key) ? $key : func_get_args();
131
132 foreach ($keys as $value) {
133 if (! $this->isEmptyString($value)) {
134 return false;
135 }
136 }
137
138 return true;
139 }
140
141 /**
142 * Determine if the instance contains a non-empty value for any of the given keys.
143 *
144 * @param string|array $keys
145 * @return bool
146 */
147 public function anyFilled($keys)
148 {
149 $keys = is_array($keys) ? $keys : func_get_args();
150
151 foreach ($keys as $key) {
152 if ($this->filled($key)) {
153 return true;
154 }
155 }
156
157 return false;
158 }
159
160 /**
161 * Apply the callback if the instance contains a non-empty value for the given key.
162 *
163 * @param string $key
164 * @param callable $callback
165 * @param callable|null $default
166 * @return $this|mixed
167 */
168 public function whenFilled($key, callable $callback, ?callable $default = null)
169 {
170 if ($this->filled($key)) {
171 return $callback(data_get($this->all(), $key)) ?: $this;
172 }
173
174 if ($default) {
175 return $default();
176 }
177
178 return $this;
179 }
180
181 /**
182 * Determine if the instance is missing a given key.
183 *
184 * @param string|array $key
185 * @return bool
186 */
187 public function missing($key)
188 {
189 $keys = is_array($key) ? $key : func_get_args();
190
191 return ! $this->has($keys);
192 }
193
194 /**
195 * Apply the callback if the instance is missing the given key.
196 *
197 * @param string $key
198 * @param callable $callback
199 * @param callable|null $default
200 * @return $this|mixed
201 */
202 public function whenMissing($key, callable $callback, ?callable $default = null)
203 {
204 if ($this->missing($key)) {
205 return $callback(data_get($this->all(), $key)) ?: $this;
206 }
207
208 if ($default) {
209 return $default();
210 }
211
212 return $this;
213 }
214
215 /**
216 * Determine if the given key is an empty string for "filled".
217 *
218 * @param string $key
219 * @return bool
220 */
221 protected function isEmptyString($key)
222 {
223 $value = $this->data($key);
224
225 return ! is_bool($value) && ! is_array($value) && trim((string) $value) === '';
226 }
227
228 /**
229 * Retrieve data from the instance as a Stringable instance.
230 *
231 * @param string $key
232 * @param mixed $default
233 * @return \Illuminate\Support\Stringable
234 */
235 public function str($key, $default = null)
236 {
237 return $this->string($key, $default);
238 }
239
240 /**
241 * Retrieve data from the instance as a Stringable instance.
242 *
243 * @param string $key
244 * @param mixed $default
245 * @return \Illuminate\Support\Stringable
246 */
247 public function string($key, $default = null)
248 {
249 return Str::of($this->data($key, $default));
250 }
251
252 /**
253 * Retrieve data as a boolean value.
254 *
255 * Returns true when value is "1", "true", "on", and "yes". Otherwise, returns false.
256 *
257 * @param string|null $key
258 * @param bool $default
259 * @return bool
260 */
261 public function boolean($key = null, $default = false)
262 {
263 return filter_var($this->data($key, $default), FILTER_VALIDATE_BOOLEAN);
264 }
265
266 /**
267 * Retrieve data as an integer value.
268 *
269 * @param string $key
270 * @param int $default
271 * @return int
272 */
273 public function integer($key, $default = 0)
274 {
275 return (int) $this->data($key, $default);
276 }
277
278 /**
279 * Retrieve data as a float value.
280 *
281 * @param string $key
282 * @param float $default
283 * @return float
284 */
285 public function float($key, $default = 0.0)
286 {
287 return (float) $this->data($key, $default);
288 }
289
290 /**
291 * Retrieve data clamped between min and max values.
292 *
293 * @param string $key
294 * @param int|float $min
295 * @param int|float $max
296 * @param int|float $default
297 * @return float|int
298 */
299 public function clamp($key, $min, $max, $default = 0)
300 {
301 return Number::clamp($this->data($key, $default), $min, $max);
302 }
303
304 /**
305 * Retrieve data from the instance as a Carbon instance.
306 *
307 * @param string $key
308 * @param string|null $format
309 * @param \UnitEnum|string|null $tz
310 * @return \Illuminate\Support\Carbon|null
311 *
312 * @throws \Carbon\Exceptions\InvalidFormatException
313 */
314 public function date($key, $format = null, $tz = null)
315 {
316 $tz = enum_value($tz);
317
318 if ($this->isNotFilled($key)) {
319 return null;
320 }
321
322 if (is_null($format)) {
323 return Date::parse($this->data($key), $tz);
324 }
325
326 return Date::createFromFormat($format, $this->data($key), $tz);
327 }
328
329 /**
330 * Retrieve data from the instance as a CarbonInterval instance.
331 *
332 * @param string $key
333 * @param \Carbon\Unit|string|null $unit
334 * @return \Carbon\CarbonInterval|null
335 */
336 public function interval($key, $unit = null)
337 {
338 if ($this->isNotFilled($key)) {
339 return null;
340 }
341
342 $value = $this->data($key);
343
344 if (is_null($unit)) {
345 return CarbonInterval::make($value);
346 }
347
348 $unit = $unit instanceof Unit ? $unit : Unit::fromName($unit);
349
350 return $unit->interval((float) $value);
351 }
352
353 /**
354 * Retrieve data from the instance as an enum.
355 *
356 * @template TEnum of \BackedEnum
357 * @template TDefault of TEnum|null
358 *
359 * @param string $key
360 * @param class-string<TEnum> $enumClass
361 * @param TDefault $default
362 * @return TEnum|TDefault
363 */
364 public function enum($key, $enumClass, $default = null)
365 {
366 if ($this->isNotFilled($key) || ! $this->isBackedEnum($enumClass)) {
367 return value($default);
368 }
369
370 return $enumClass::tryFrom($this->data($key)) ?: value($default);
371 }
372
373 /**
374 * Retrieve data from the instance as an array of enums.
375 *
376 * @template TEnum of \BackedEnum
377 *
378 * @param string $key
379 * @param class-string<TEnum> $enumClass
380 * @return TEnum[]
381 */
382 public function enums($key, $enumClass)
383 {
384 if ($this->isNotFilled($key) || ! $this->isBackedEnum($enumClass)) {
385 return [];
386 }
387
388 return $this->collect($key)
389 ->map(fn ($value) => $enumClass::tryFrom($value))
390 ->filter()
391 ->all();
392 }
393
394 /**
395 * Determine if the given enum class is backed.
396 *
397 * @param class-string $enumClass
398 * @return bool
399 */
400 protected function isBackedEnum($enumClass)
401 {
402 return is_a($enumClass, \BackedEnum::class, true);
403 }
404
405 /**
406 * Retrieve data from the instance as an array.
407 *
408 * @param array|string|null $key
409 * @return array
410 */
411 public function array($key = null)
412 {
413 return (array) (is_array($key) ? $this->only($key) : $this->data($key));
414 }
415
416 /**
417 * Retrieve data from the instance as a collection.
418 *
419 * @param array|string|null $key
420 * @return \Illuminate\Support\Collection
421 */
422 public function collect($key = null)
423 {
424 return new Collection(is_array($key) ? $this->only($key) : $this->data($key));
425 }
426
427 /**
428 * Get a subset containing the provided keys with values from the instance data.
429 *
430 * @param mixed $keys
431 * @return array
432 */
433 public function only($keys)
434 {
435 $results = [];
436
437 $data = $this->all();
438
439 $placeholder = new stdClass;
440
441 foreach (is_array($keys) ? $keys : func_get_args() as $key) {
442 $value = data_get($data, $key, $placeholder);
443
444 if ($value !== $placeholder) {
445 Arr::set($results, $key, $value);
446 }
447 }
448
449 return $results;
450 }
451
452 /**
453 * Get all of the data except for a specified array of items.
454 *
455 * @param mixed $keys
456 * @return array
457 */
458 public function except($keys)
459 {
460 $keys = is_array($keys) ? $keys : func_get_args();
461
462 $results = $this->all();
463
464 Arr::forget($results, $keys);
465
466 return $results;
467 }
468 }