Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 use Carbon\CarbonInterval;
4 use Illuminate\Contracts\Support\DeferringDisplayableValue;
5 use Illuminate\Contracts\Support\Htmlable;
6 use Illuminate\Database\Eloquent\Model;
7 use Illuminate\Support\Arr;
8 use Illuminate\Support\Env;
9 use Illuminate\Support\Fluent;
10 use Illuminate\Support\HigherOrderTapProxy;
11 use Illuminate\Support\Once;
12 use Illuminate\Support\Onceable;
13 use Illuminate\Support\Optional;
14 use Illuminate\Support\Sleep;
15 use Illuminate\Support\Str;
16 use Illuminate\Support\Stringable as SupportStringable;
17
18 if (! function_exists('append_config')) {
19 /**
20 * Assign high numeric IDs to a config item to force appending.
21 *
22 * @param array $array
23 */
24 function append_config(array $array): array
25 {
26 $start = 9999;
27
28 foreach ($array as $key => $value) {
29 if (is_numeric($key)) {
30 $start++;
31
32 $array[$start] = Arr::pull($array, $key);
33 }
34 }
35
36 return $array;
37 }
38 }
39
40 if (! function_exists('blank')) {
41 /**
42 * Determine if the given value is "blank".
43 *
44 * @phpstan-assert-if-false !=null|'' $value
45 *
46 * @phpstan-assert-if-true !=numeric|bool $value
47 *
48 * @param mixed $value
49 */
50 function blank($value): bool
51 {
52 if (is_null($value)) {
53 return true;
54 }
55
56 if (is_string($value)) {
57 return trim($value) === '';
58 }
59
60 if (is_numeric($value) || is_bool($value)) {
61 return false;
62 }
63
64 if ($value instanceof Model) {
65 return false;
66 }
67
68 if ($value instanceof Countable) {
69 return count($value) === 0;
70 }
71
72 if ($value instanceof Stringable) {
73 return trim((string) $value) === '';
74 }
75
76 return empty($value);
77 }
78 }
79
80 if (! function_exists('class_basename')) {
81 /**
82 * Get the class "basename" of the given object / class.
83 *
84 * @param string|object $class
85 */
86 function class_basename($class): string
87 {
88 $class = is_object($class) ? get_class($class) : $class;
89
90 return basename(str_replace('\\', '/', $class));
91 }
92 }
93
94 if (! function_exists('class_uses_recursive')) {
95 /**
96 * Returns all traits used by a class, its parent classes and trait of their traits.
97 *
98 * @param object|string $class
99 * @return array<string, string>
100 */
101 function class_uses_recursive($class): array
102 {
103 if (is_object($class)) {
104 $class = get_class($class);
105 }
106
107 $results = [];
108
109 foreach (array_reverse(class_parents($class) ?: []) + [$class => $class] as $class) {
110 $results += trait_uses_recursive($class);
111 }
112
113 return array_unique($results);
114 }
115 }
116
117 if (! function_exists('e')) {
118 /**
119 * Encode HTML special characters in a string.
120 *
121 * @param \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|\BackedEnum|string|int|float|null $value
122 * @param bool $doubleEncode
123 */
124 function e($value, $doubleEncode = true): string
125 {
126 if ($value instanceof DeferringDisplayableValue) {
127 $value = $value->resolveDisplayableValue();
128 }
129
130 if ($value instanceof Htmlable) {
131 return $value->toHtml() ?? '';
132 }
133
134 if ($value instanceof BackedEnum) {
135 $value = $value->value;
136 }
137
138 return htmlspecialchars($value ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', $doubleEncode);
139 }
140 }
141
142 if (! function_exists('env')) {
143 /**
144 * Gets the value of an environment variable.
145 *
146 * @param string $key
147 * @param mixed $default
148 * @return mixed
149 */
150 function env($key, $default = null)
151 {
152 return Env::get($key, $default);
153 }
154 }
155
156 if (! function_exists('filled')) {
157 /**
158 * Determine if a value is "filled".
159 *
160 * @phpstan-assert-if-true !=null|'' $value
161 *
162 * @phpstan-assert-if-false !=numeric|bool $value
163 *
164 * @param mixed $value
165 */
166 function filled($value): bool
167 {
168 return ! blank($value);
169 }
170 }
171
172 if (! function_exists('fluent')) {
173 /**
174 * Create a Fluent object from the given value.
175 *
176 * @param iterable|object|null $value
177 */
178 function fluent($value = null): Fluent
179 {
180 return new Fluent($value ?? []);
181 }
182 }
183
184 if (! function_exists('literal')) {
185 /**
186 * Return a new literal or anonymous object using named arguments.
187 *
188 * @return mixed
189 */
190 function literal(...$arguments)
191 {
192 if (count($arguments) === 1 && array_is_list($arguments)) {
193 return $arguments[0];
194 }
195
196 return (object) $arguments;
197 }
198 }
199
200 if (! function_exists('object_get')) {
201 /**
202 * Get an item from an object using "dot" notation.
203 *
204 * @template TValue of object
205 *
206 * @param TValue $object
207 * @param string|null $key
208 * @param mixed $default
209 * @return ($key is empty ? TValue : mixed)
210 */
211 function object_get($object, $key, $default = null)
212 {
213 if (is_null($key) || trim($key) === '') {
214 return $object;
215 }
216
217 foreach (explode('.', $key) as $segment) {
218 if (! is_object($object) || ! isset($object->{$segment})) {
219 return value($default);
220 }
221
222 $object = $object->{$segment};
223 }
224
225 return $object;
226 }
227 }
228
229 if (! function_exists('laravel_cloud')) {
230 /**
231 * Determine if the application is running on Laravel Cloud.
232 */
233 function laravel_cloud(): bool
234 {
235 return ($_ENV['LARAVEL_CLOUD'] ?? false) === '1' ||
236 ($_SERVER['LARAVEL_CLOUD'] ?? false) === '1';
237 }
238 }
239
240 if (! function_exists('once')) {
241 /**
242 * Ensures a callable is only called once, and returns the result on subsequent calls.
243 *
244 * @template TReturnType
245 *
246 * @param callable(): TReturnType $callback
247 * @return TReturnType
248 */
249 function once(callable $callback)
250 {
251 $onceable = Onceable::tryFromTrace(
252 debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2),
253 $callback,
254 );
255
256 return $onceable ? Once::instance()->value($onceable) : call_user_func($callback);
257 }
258 }
259
260 if (! function_exists('optional')) {
261 /**
262 * Provide access to optional objects.
263 *
264 * @template TValue
265 * @template TReturn
266 *
267 * @param TValue $value
268 * @param (callable(TValue): TReturn)|null $callback
269 * @return ($callback is null ? \Illuminate\Support\Optional : ($value is null ? null : TReturn))
270 */
271 function optional($value = null, ?callable $callback = null)
272 {
273 if (is_null($callback)) {
274 return new Optional($value);
275 } elseif (! is_null($value)) {
276 return $callback($value);
277 }
278 }
279 }
280
281 if (! function_exists('preg_replace_array')) {
282 /**
283 * Replace a given pattern with each value in the array in sequentially.
284 *
285 * @param string $pattern
286 * @param array $replacements
287 * @param string $subject
288 */
289 function preg_replace_array($pattern, array $replacements, $subject): string
290 {
291 return preg_replace_callback($pattern, function () use (&$replacements) {
292 return array_shift($replacements);
293 }, $subject);
294 }
295 }
296
297 if (! function_exists('retry')) {
298 /**
299 * Retry an operation a given number of times.
300 *
301 * @template TValue
302 *
303 * @param int|array<int, int> $times
304 * @param callable(int): TValue $callback
305 * @param CarbonInterval|int|\Closure(int, \Throwable): CarbonInterval|int $sleepMilliseconds
306 * @param (callable(\Throwable): bool)|null $when
307 * @return TValue
308 *
309 * @throws \Throwable
310 */
311 function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null)
312 {
313 $attempts = 0;
314
315 $backoff = [];
316
317 if (is_array($times)) {
318 $backoff = $times;
319
320 $times = count($times) + 1;
321 }
322
323 beginning:
324 $attempts++;
325 $times--;
326
327 try {
328 return $callback($attempts);
329 } catch (Throwable $e) {
330 if ($times < 1 || ($when && ! $when($e))) {
331 throw $e;
332 }
333
334 $sleepMilliseconds = $backoff[$attempts - 1] ?? $sleepMilliseconds;
335
336 if ($sleepMilliseconds) {
337 $duration = value($sleepMilliseconds, $attempts, $e);
338
339 $duration instanceof CarbonInterval
340 ? Sleep::usleep($duration->totalMicroseconds)
341 : Sleep::usleep($duration * 1000);
342 }
343
344 goto beginning;
345 }
346 }
347 }
348
349 if (! function_exists('str')) {
350 /**
351 * Get a new stringable object from the given string.
352 *
353 * @param string|null $string
354 * @return ($string is null ? object : \Illuminate\Support\Stringable)
355 */
356 function str($string = null)
357 {
358 if (func_num_args() === 0) {
359 return new class
360 {
361 public function __call($method, $parameters)
362 {
363 return Str::$method(...$parameters);
364 }
365
366 public function __toString()
367 {
368 return '';
369 }
370 };
371 }
372
373 return new SupportStringable($string);
374 }
375 }
376
377 if (! function_exists('tap')) {
378 /**
379 * Call the given Closure with the given value then return the value.
380 *
381 * @template TValue
382 *
383 * @param TValue $value
384 * @param (callable(TValue): mixed)|null $callback
385 * @return ($callback is null ? \Illuminate\Support\HigherOrderTapProxy : TValue)
386 */
387 function tap($value, $callback = null)
388 {
389 if (is_null($callback)) {
390 return new HigherOrderTapProxy($value);
391 }
392
393 $callback($value);
394
395 return $value;
396 }
397 }
398
399 if (! function_exists('throw_if')) {
400 /**
401 * Throw the given exception if the given condition is true.
402 *
403 * @template TValue
404 * @template TParams of mixed
405 * @template TException of \Throwable
406 * @template TExceptionValue of TException|class-string<TException>|string
407 *
408 * @param TValue $condition
409 * @param Closure(TParams): TExceptionValue|TExceptionValue $exception
410 * @param TParams ...$parameters
411 * @return ($condition is true ? never : ($condition is non-empty-mixed ? never : TValue))
412 *
413 * @throws TException
414 */
415 function throw_if($condition, $exception = 'RuntimeException', ...$parameters)
416 {
417 if ($condition) {
418 if ($exception instanceof Closure) {
419 $exception = $exception(...$parameters);
420 }
421
422 if (is_string($exception) && class_exists($exception)) {
423 $exception = new $exception(...$parameters);
424 }
425
426 throw is_string($exception) ? new RuntimeException($exception) : $exception;
427 }
428
429 return $condition;
430 }
431 }
432
433 if (! function_exists('throw_unless')) {
434 /**
435 * Throw the given exception unless the given condition is true.
436 *
437 * @template TValue
438 * @template TParams of mixed
439 * @template TException of \Throwable
440 * @template TExceptionValue of TException|class-string<TException>|string
441 *
442 * @param TValue $condition
443 * @param Closure(TParams): TExceptionValue|TExceptionValue $exception
444 * @param TParams ...$parameters
445 * @return ($condition is false ? never : ($condition is non-empty-mixed ? TValue : never))
446 *
447 * @throws TException
448 */
449 function throw_unless($condition, $exception = 'RuntimeException', ...$parameters)
450 {
451 throw_if(! $condition, $exception, ...$parameters);
452
453 return $condition;
454 }
455 }
456
457 if (! function_exists('trait_uses_recursive')) {
458 /**
459 * Returns all traits used by a trait and its traits.
460 *
461 * @param object|string $trait
462 * @return array<string, string>
463 */
464 function trait_uses_recursive($trait): array
465 {
466 $traits = class_uses($trait) ?: [];
467
468 foreach ($traits as $trait) {
469 $traits += trait_uses_recursive($trait);
470 }
471
472 return $traits;
473 }
474 }
475
476 if (! function_exists('transform')) {
477 /**
478 * Transform the given value if it is present.
479 *
480 * @template TValue
481 * @template TReturn
482 * @template TDefault
483 *
484 * @param TValue $value
485 * @param callable(TValue): TReturn $callback
486 * @param TDefault|callable(TValue): TDefault $default
487 * @return ($value is empty ? TDefault : TReturn)
488 */
489 function transform($value, callable $callback, $default = null)
490 {
491 if (filled($value)) {
492 return $callback($value);
493 }
494
495 if (is_callable($default)) {
496 return $default($value);
497 }
498
499 return $default;
500 }
501 }
502
503 if (! function_exists('windows_os')) {
504 /**
505 * Determine whether the current environment is Windows based.
506 */
507 function windows_os(): bool
508 {
509 return PHP_OS_FAMILY === 'Windows';
510 }
511 }
512
513 if (! function_exists('with')) {
514 /**
515 * Return the given value, optionally passed through the given callback.
516 *
517 * @template TValue
518 * @template TReturn
519 *
520 * @param TValue $value
521 * @param (callable(TValue): (TReturn))|null $callback
522 * @return ($callback is null ? TValue : TReturn)
523 */
524 function with($value, ?callable $callback = null)
525 {
526 return is_null($callback) ? $value : $callback($value);
527 }
528 }