composer package updates
[openemr.git] / vendor / illuminate / support / helpers.php
blobc52fd66fa742eb53e83110cede967edea5770a01
1 <?php
3 use Illuminate\Support\Arr;
4 use Illuminate\Support\Str;
5 use Illuminate\Support\Optional;
6 use Illuminate\Support\Collection;
7 use Illuminate\Support\Debug\Dumper;
8 use Illuminate\Contracts\Support\Htmlable;
9 use Illuminate\Support\HigherOrderTapProxy;
11 if (! function_exists('append_config')) {
12 /**
13 * Assign high numeric IDs to a config item to force appending.
15 * @param array $array
16 * @return array
18 function append_config(array $array)
20 $start = 9999;
22 foreach ($array as $key => $value) {
23 if (is_numeric($key)) {
24 $start++;
26 $array[$start] = Arr::pull($array, $key);
30 return $array;
34 if (! function_exists('array_add')) {
35 /**
36 * Add an element to an array using "dot" notation if it doesn't exist.
38 * @param array $array
39 * @param string $key
40 * @param mixed $value
41 * @return array
43 function array_add($array, $key, $value)
45 return Arr::add($array, $key, $value);
49 if (! function_exists('array_collapse')) {
50 /**
51 * Collapse an array of arrays into a single array.
53 * @param array $array
54 * @return array
56 function array_collapse($array)
58 return Arr::collapse($array);
62 if (! function_exists('array_divide')) {
63 /**
64 * Divide an array into two arrays. One with keys and the other with values.
66 * @param array $array
67 * @return array
69 function array_divide($array)
71 return Arr::divide($array);
75 if (! function_exists('array_dot')) {
76 /**
77 * Flatten a multi-dimensional associative array with dots.
79 * @param array $array
80 * @param string $prepend
81 * @return array
83 function array_dot($array, $prepend = '')
85 return Arr::dot($array, $prepend);
89 if (! function_exists('array_except')) {
90 /**
91 * Get all of the given array except for a specified array of keys.
93 * @param array $array
94 * @param array|string $keys
95 * @return array
97 function array_except($array, $keys)
99 return Arr::except($array, $keys);
103 if (! function_exists('array_first')) {
105 * Return the first element in an array passing a given truth test.
107 * @param array $array
108 * @param callable|null $callback
109 * @param mixed $default
110 * @return mixed
112 function array_first($array, callable $callback = null, $default = null)
114 return Arr::first($array, $callback, $default);
118 if (! function_exists('array_flatten')) {
120 * Flatten a multi-dimensional array into a single level.
122 * @param array $array
123 * @param int $depth
124 * @return array
126 function array_flatten($array, $depth = INF)
128 return Arr::flatten($array, $depth);
132 if (! function_exists('array_forget')) {
134 * Remove one or many array items from a given array using "dot" notation.
136 * @param array $array
137 * @param array|string $keys
138 * @return void
140 function array_forget(&$array, $keys)
142 return Arr::forget($array, $keys);
146 if (! function_exists('array_get')) {
148 * Get an item from an array using "dot" notation.
150 * @param \ArrayAccess|array $array
151 * @param string $key
152 * @param mixed $default
153 * @return mixed
155 function array_get($array, $key, $default = null)
157 return Arr::get($array, $key, $default);
161 if (! function_exists('array_has')) {
163 * Check if an item or items exist in an array using "dot" notation.
165 * @param \ArrayAccess|array $array
166 * @param string|array $keys
167 * @return bool
169 function array_has($array, $keys)
171 return Arr::has($array, $keys);
175 if (! function_exists('array_last')) {
177 * Return the last element in an array passing a given truth test.
179 * @param array $array
180 * @param callable|null $callback
181 * @param mixed $default
182 * @return mixed
184 function array_last($array, callable $callback = null, $default = null)
186 return Arr::last($array, $callback, $default);
190 if (! function_exists('array_only')) {
192 * Get a subset of the items from the given array.
194 * @param array $array
195 * @param array|string $keys
196 * @return array
198 function array_only($array, $keys)
200 return Arr::only($array, $keys);
204 if (! function_exists('array_pluck')) {
206 * Pluck an array of values from an array.
208 * @param array $array
209 * @param string|array $value
210 * @param string|array|null $key
211 * @return array
213 function array_pluck($array, $value, $key = null)
215 return Arr::pluck($array, $value, $key);
219 if (! function_exists('array_prepend')) {
221 * Push an item onto the beginning of an array.
223 * @param array $array
224 * @param mixed $value
225 * @param mixed $key
226 * @return array
228 function array_prepend($array, $value, $key = null)
230 return Arr::prepend($array, $value, $key);
234 if (! function_exists('array_pull')) {
236 * Get a value from the array, and remove it.
238 * @param array $array
239 * @param string $key
240 * @param mixed $default
241 * @return mixed
243 function array_pull(&$array, $key, $default = null)
245 return Arr::pull($array, $key, $default);
249 if (! function_exists('array_random')) {
251 * Get a random value from an array.
253 * @param array $array
254 * @param int|null $num
255 * @return mixed
257 function array_random($array, $num = null)
259 return Arr::random($array, $num);
263 if (! function_exists('array_set')) {
265 * Set an array item to a given value using "dot" notation.
267 * If no key is given to the method, the entire array will be replaced.
269 * @param array $array
270 * @param string $key
271 * @param mixed $value
272 * @return array
274 function array_set(&$array, $key, $value)
276 return Arr::set($array, $key, $value);
280 if (! function_exists('array_sort')) {
282 * Sort the array by the given callback or attribute name.
284 * @param array $array
285 * @param callable|string|null $callback
286 * @return array
288 function array_sort($array, $callback = null)
290 return Arr::sort($array, $callback);
294 if (! function_exists('array_sort_recursive')) {
296 * Recursively sort an array by keys and values.
298 * @param array $array
299 * @return array
301 function array_sort_recursive($array)
303 return Arr::sortRecursive($array);
307 if (! function_exists('array_where')) {
309 * Filter the array using the given callback.
311 * @param array $array
312 * @param callable $callback
313 * @return array
315 function array_where($array, callable $callback)
317 return Arr::where($array, $callback);
321 if (! function_exists('array_wrap')) {
323 * If the given value is not an array, wrap it in one.
325 * @param mixed $value
326 * @return array
328 function array_wrap($value)
330 return Arr::wrap($value);
334 if (! function_exists('blank')) {
336 * Determine if the given value is "blank".
338 * @param mixed $value
339 * @return bool
341 function blank($value)
343 if (is_null($value)) {
344 return true;
347 if (is_string($value)) {
348 return trim($value) === '';
351 if (is_numeric($value) || is_bool($value)) {
352 return false;
355 if ($value instanceof Countable) {
356 return count($value) === 0;
359 return empty($value);
363 if (! function_exists('camel_case')) {
365 * Convert a value to camel case.
367 * @param string $value
368 * @return string
370 function camel_case($value)
372 return Str::camel($value);
376 if (! function_exists('class_basename')) {
378 * Get the class "basename" of the given object / class.
380 * @param string|object $class
381 * @return string
383 function class_basename($class)
385 $class = is_object($class) ? get_class($class) : $class;
387 return basename(str_replace('\\', '/', $class));
391 if (! function_exists('class_uses_recursive')) {
393 * Returns all traits used by a class, its subclasses and trait of their traits.
395 * @param object|string $class
396 * @return array
398 function class_uses_recursive($class)
400 if (is_object($class)) {
401 $class = get_class($class);
404 $results = [];
406 foreach (array_merge([$class => $class], class_parents($class)) as $class) {
407 $results += trait_uses_recursive($class);
410 return array_unique($results);
414 if (! function_exists('collect')) {
416 * Create a collection from the given value.
418 * @param mixed $value
419 * @return \Illuminate\Support\Collection
421 function collect($value = null)
423 return new Collection($value);
427 if (! function_exists('data_fill')) {
429 * Fill in data where it's missing.
431 * @param mixed $target
432 * @param string|array $key
433 * @param mixed $value
434 * @return mixed
436 function data_fill(&$target, $key, $value)
438 return data_set($target, $key, $value, false);
442 if (! function_exists('data_get')) {
444 * Get an item from an array or object using "dot" notation.
446 * @param mixed $target
447 * @param string|array $key
448 * @param mixed $default
449 * @return mixed
451 function data_get($target, $key, $default = null)
453 if (is_null($key)) {
454 return $target;
457 $key = is_array($key) ? $key : explode('.', $key);
459 while (! is_null($segment = array_shift($key))) {
460 if ($segment === '*') {
461 if ($target instanceof Collection) {
462 $target = $target->all();
463 } elseif (! is_array($target)) {
464 return value($default);
467 $result = Arr::pluck($target, $key);
469 return in_array('*', $key) ? Arr::collapse($result) : $result;
472 if (Arr::accessible($target) && Arr::exists($target, $segment)) {
473 $target = $target[$segment];
474 } elseif (is_object($target) && isset($target->{$segment})) {
475 $target = $target->{$segment};
476 } else {
477 return value($default);
481 return $target;
485 if (! function_exists('data_set')) {
487 * Set an item on an array or object using dot notation.
489 * @param mixed $target
490 * @param string|array $key
491 * @param mixed $value
492 * @param bool $overwrite
493 * @return mixed
495 function data_set(&$target, $key, $value, $overwrite = true)
497 $segments = is_array($key) ? $key : explode('.', $key);
499 if (($segment = array_shift($segments)) === '*') {
500 if (! Arr::accessible($target)) {
501 $target = [];
504 if ($segments) {
505 foreach ($target as &$inner) {
506 data_set($inner, $segments, $value, $overwrite);
508 } elseif ($overwrite) {
509 foreach ($target as &$inner) {
510 $inner = $value;
513 } elseif (Arr::accessible($target)) {
514 if ($segments) {
515 if (! Arr::exists($target, $segment)) {
516 $target[$segment] = [];
519 data_set($target[$segment], $segments, $value, $overwrite);
520 } elseif ($overwrite || ! Arr::exists($target, $segment)) {
521 $target[$segment] = $value;
523 } elseif (is_object($target)) {
524 if ($segments) {
525 if (! isset($target->{$segment})) {
526 $target->{$segment} = [];
529 data_set($target->{$segment}, $segments, $value, $overwrite);
530 } elseif ($overwrite || ! isset($target->{$segment})) {
531 $target->{$segment} = $value;
533 } else {
534 $target = [];
536 if ($segments) {
537 data_set($target[$segment], $segments, $value, $overwrite);
538 } elseif ($overwrite) {
539 $target[$segment] = $value;
543 return $target;
547 if (! function_exists('dd')) {
549 * Dump the passed variables and end the script.
551 * @param mixed $args
552 * @return void
554 function dd(...$args)
556 http_response_code(500);
558 foreach ($args as $x) {
559 (new Dumper)->dump($x);
562 die(1);
566 if (! function_exists('e')) {
568 * Escape HTML special characters in a string.
570 * @param \Illuminate\Contracts\Support\Htmlable|string $value
571 * @param bool $doubleEncode
572 * @return string
574 function e($value, $doubleEncode = false)
576 if ($value instanceof Htmlable) {
577 return $value->toHtml();
580 return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', $doubleEncode);
584 if (! function_exists('ends_with')) {
586 * Determine if a given string ends with a given substring.
588 * @param string $haystack
589 * @param string|array $needles
590 * @return bool
592 function ends_with($haystack, $needles)
594 return Str::endsWith($haystack, $needles);
598 if (! function_exists('env')) {
600 * Gets the value of an environment variable.
602 * @param string $key
603 * @param mixed $default
604 * @return mixed
606 function env($key, $default = null)
608 $value = getenv($key);
610 if ($value === false) {
611 return value($default);
614 switch (strtolower($value)) {
615 case 'true':
616 case '(true)':
617 return true;
618 case 'false':
619 case '(false)':
620 return false;
621 case 'empty':
622 case '(empty)':
623 return '';
624 case 'null':
625 case '(null)':
626 return;
629 if (strlen($value) > 1 && Str::startsWith($value, '"') && Str::endsWith($value, '"')) {
630 return substr($value, 1, -1);
633 return $value;
637 if (! function_exists('filled')) {
639 * Determine if a value is "filled".
641 * @param mixed $value
642 * @return bool
644 function filled($value)
646 return ! blank($value);
650 if (! function_exists('head')) {
652 * Get the first element of an array. Useful for method chaining.
654 * @param array $array
655 * @return mixed
657 function head($array)
659 return reset($array);
663 if (! function_exists('kebab_case')) {
665 * Convert a string to kebab case.
667 * @param string $value
668 * @return string
670 function kebab_case($value)
672 return Str::kebab($value);
676 if (! function_exists('last')) {
678 * Get the last element from an array.
680 * @param array $array
681 * @return mixed
683 function last($array)
685 return end($array);
689 if (! function_exists('object_get')) {
691 * Get an item from an object using "dot" notation.
693 * @param object $object
694 * @param string $key
695 * @param mixed $default
696 * @return mixed
698 function object_get($object, $key, $default = null)
700 if (is_null($key) || trim($key) == '') {
701 return $object;
704 foreach (explode('.', $key) as $segment) {
705 if (! is_object($object) || ! isset($object->{$segment})) {
706 return value($default);
709 $object = $object->{$segment};
712 return $object;
716 if (! function_exists('optional')) {
718 * Provide access to optional objects.
720 * @param mixed $value
721 * @return mixed
723 function optional($value = null)
725 return new Optional($value);
729 if (! function_exists('preg_replace_array')) {
731 * Replace a given pattern with each value in the array in sequentially.
733 * @param string $pattern
734 * @param array $replacements
735 * @param string $subject
736 * @return string
738 function preg_replace_array($pattern, array $replacements, $subject)
740 return preg_replace_callback($pattern, function () use (&$replacements) {
741 foreach ($replacements as $key => $value) {
742 return array_shift($replacements);
744 }, $subject);
748 if (! function_exists('retry')) {
750 * Retry an operation a given number of times.
752 * @param int $times
753 * @param callable $callback
754 * @param int $sleep
755 * @return mixed
757 * @throws \Exception
759 function retry($times, callable $callback, $sleep = 0)
761 $times--;
763 beginning:
764 try {
765 return $callback();
766 } catch (Exception $e) {
767 if (! $times) {
768 throw $e;
771 $times--;
773 if ($sleep) {
774 usleep($sleep * 1000);
777 goto beginning;
782 if (! function_exists('snake_case')) {
784 * Convert a string to snake case.
786 * @param string $value
787 * @param string $delimiter
788 * @return string
790 function snake_case($value, $delimiter = '_')
792 return Str::snake($value, $delimiter);
796 if (! function_exists('starts_with')) {
798 * Determine if a given string starts with a given substring.
800 * @param string $haystack
801 * @param string|array $needles
802 * @return bool
804 function starts_with($haystack, $needles)
806 return Str::startsWith($haystack, $needles);
810 if (! function_exists('str_after')) {
812 * Return the remainder of a string after a given value.
814 * @param string $subject
815 * @param string $search
816 * @return string
818 function str_after($subject, $search)
820 return Str::after($subject, $search);
824 if (! function_exists('str_before')) {
826 * Get the portion of a string before a given value.
828 * @param string $subject
829 * @param string $search
830 * @return string
832 function str_before($subject, $search)
834 return Str::before($subject, $search);
838 if (! function_exists('str_contains')) {
840 * Determine if a given string contains a given substring.
842 * @param string $haystack
843 * @param string|array $needles
844 * @return bool
846 function str_contains($haystack, $needles)
848 return Str::contains($haystack, $needles);
852 if (! function_exists('str_finish')) {
854 * Cap a string with a single instance of a given value.
856 * @param string $value
857 * @param string $cap
858 * @return string
860 function str_finish($value, $cap)
862 return Str::finish($value, $cap);
866 if (! function_exists('str_is')) {
868 * Determine if a given string matches a given pattern.
870 * @param string|array $pattern
871 * @param string $value
872 * @return bool
874 function str_is($pattern, $value)
876 return Str::is($pattern, $value);
880 if (! function_exists('str_limit')) {
882 * Limit the number of characters in a string.
884 * @param string $value
885 * @param int $limit
886 * @param string $end
887 * @return string
889 function str_limit($value, $limit = 100, $end = '...')
891 return Str::limit($value, $limit, $end);
895 if (! function_exists('str_plural')) {
897 * Get the plural form of an English word.
899 * @param string $value
900 * @param int $count
901 * @return string
903 function str_plural($value, $count = 2)
905 return Str::plural($value, $count);
909 if (! function_exists('str_random')) {
911 * Generate a more truly "random" alpha-numeric string.
913 * @param int $length
914 * @return string
916 * @throws \RuntimeException
918 function str_random($length = 16)
920 return Str::random($length);
924 if (! function_exists('str_replace_array')) {
926 * Replace a given value in the string sequentially with an array.
928 * @param string $search
929 * @param array $replace
930 * @param string $subject
931 * @return string
933 function str_replace_array($search, array $replace, $subject)
935 return Str::replaceArray($search, $replace, $subject);
939 if (! function_exists('str_replace_first')) {
941 * Replace the first occurrence of a given value in the string.
943 * @param string $search
944 * @param string $replace
945 * @param string $subject
946 * @return string
948 function str_replace_first($search, $replace, $subject)
950 return Str::replaceFirst($search, $replace, $subject);
954 if (! function_exists('str_replace_last')) {
956 * Replace the last occurrence of a given value in the string.
958 * @param string $search
959 * @param string $replace
960 * @param string $subject
961 * @return string
963 function str_replace_last($search, $replace, $subject)
965 return Str::replaceLast($search, $replace, $subject);
969 if (! function_exists('str_singular')) {
971 * Get the singular form of an English word.
973 * @param string $value
974 * @return string
976 function str_singular($value)
978 return Str::singular($value);
982 if (! function_exists('str_slug')) {
984 * Generate a URL friendly "slug" from a given string.
986 * @param string $title
987 * @param string $separator
988 * @param string $language
989 * @return string
991 function str_slug($title, $separator = '-', $language = 'en')
993 return Str::slug($title, $separator, $language);
997 if (! function_exists('str_start')) {
999 * Begin a string with a single instance of a given value.
1001 * @param string $value
1002 * @param string $prefix
1003 * @return string
1005 function str_start($value, $prefix)
1007 return Str::start($value, $prefix);
1011 if (! function_exists('studly_case')) {
1013 * Convert a value to studly caps case.
1015 * @param string $value
1016 * @return string
1018 function studly_case($value)
1020 return Str::studly($value);
1024 if (! function_exists('tap')) {
1026 * Call the given Closure with the given value then return the value.
1028 * @param mixed $value
1029 * @param callable|null $callback
1030 * @return mixed
1032 function tap($value, $callback = null)
1034 if (is_null($callback)) {
1035 return new HigherOrderTapProxy($value);
1038 $callback($value);
1040 return $value;
1044 if (! function_exists('throw_if')) {
1046 * Throw the given exception if the given condition is true.
1048 * @param mixed $condition
1049 * @param \Throwable|string $exception
1050 * @param array ...$parameters
1051 * @return mixed
1052 * @throws \Throwable
1054 function throw_if($condition, $exception, ...$parameters)
1056 if ($condition) {
1057 throw (is_string($exception) ? new $exception(...$parameters) : $exception);
1060 return $condition;
1064 if (! function_exists('throw_unless')) {
1066 * Throw the given exception unless the given condition is true.
1068 * @param mixed $condition
1069 * @param \Throwable|string $exception
1070 * @param array ...$parameters
1071 * @return mixed
1072 * @throws \Throwable
1074 function throw_unless($condition, $exception, ...$parameters)
1076 if (! $condition) {
1077 throw (is_string($exception) ? new $exception(...$parameters) : $exception);
1080 return $condition;
1084 if (! function_exists('title_case')) {
1086 * Convert a value to title case.
1088 * @param string $value
1089 * @return string
1091 function title_case($value)
1093 return Str::title($value);
1097 if (! function_exists('trait_uses_recursive')) {
1099 * Returns all traits used by a trait and its traits.
1101 * @param string $trait
1102 * @return array
1104 function trait_uses_recursive($trait)
1106 $traits = class_uses($trait);
1108 foreach ($traits as $trait) {
1109 $traits += trait_uses_recursive($trait);
1112 return $traits;
1116 if (! function_exists('transform')) {
1118 * Transform the given value if it is present.
1120 * @param mixed $value
1121 * @param callable $callback
1122 * @param mixed $default
1123 * @return mixed|null
1125 function transform($value, callable $callback, $default = null)
1127 if (filled($value)) {
1128 return $callback($value);
1131 if (is_callable($default)) {
1132 return $default($value);
1135 return $default;
1139 if (! function_exists('value')) {
1141 * Return the default value of the given value.
1143 * @param mixed $value
1144 * @return mixed
1146 function value($value)
1148 return $value instanceof Closure ? $value() : $value;
1152 if (! function_exists('windows_os')) {
1154 * Determine whether the current environment is Windows based.
1156 * @return bool
1158 function windows_os()
1160 return strtolower(substr(PHP_OS, 0, 3)) === 'win';
1164 if (! function_exists('with')) {
1166 * Return the given value, optionally passed through the given callback.
1168 * @param mixed $value
1169 * @param callable|null $callback
1170 * @return mixed
1172 function with($value, callable $callback = null)
1174 return is_null($callback) ? $value : $callback($value);