PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Support/helpers.php

https://gitlab.com/jjpa2018/dashboard
PHP | 379 lines | 349 code | 12 blank | 18 comment | 9 complexity | 1794d5aa653ddae2b125a2c631397c10 MD5 | raw file
  1. <?php
  2. use Illuminate\Contracts\Support\DeferringDisplayableValue;
  3. use Illuminate\Contracts\Support\Htmlable;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Support\Env;
  6. use Illuminate\Support\HigherOrderTapProxy;
  7. use Illuminate\Support\Optional;
  8. if (! function_exists('append_config')) {
  9. /**
  10. * Assign high numeric IDs to a config item to force appending.
  11. *
  12. * @param array $array
  13. * @return array
  14. */
  15. function append_config(array $array)
  16. {
  17. $start = 9999;
  18. foreach ($array as $key => $value) {
  19. if (is_numeric($key)) {
  20. $start++;
  21. $array[$start] = Arr::pull($array, $key);
  22. }
  23. }
  24. return $array;
  25. }
  26. }
  27. if (! function_exists('blank')) {
  28. /**
  29. * Determine if the given value is "blank".
  30. *
  31. * @param mixed $value
  32. * @return bool
  33. */
  34. function blank($value)
  35. {
  36. if (is_null($value)) {
  37. return true;
  38. }
  39. if (is_string($value)) {
  40. return trim($value) === '';
  41. }
  42. if (is_numeric($value) || is_bool($value)) {
  43. return false;
  44. }
  45. if ($value instanceof Countable) {
  46. return count($value) === 0;
  47. }
  48. return empty($value);
  49. }
  50. }
  51. if (! function_exists('class_basename')) {
  52. /**
  53. * Get the class "basename" of the given object / class.
  54. *
  55. * @param string|object $class
  56. * @return string
  57. */
  58. function class_basename($class)
  59. {
  60. $class = is_object($class) ? get_class($class) : $class;
  61. return basename(str_replace('\\', '/', $class));
  62. }
  63. }
  64. if (! function_exists('class_uses_recursive')) {
  65. /**
  66. * Returns all traits used by a class, its parent classes and trait of their traits.
  67. *
  68. * @param object|string $class
  69. * @return array
  70. */
  71. function class_uses_recursive($class)
  72. {
  73. if (is_object($class)) {
  74. $class = get_class($class);
  75. }
  76. $results = [];
  77. foreach (array_reverse(class_parents($class)) + [$class => $class] as $class) {
  78. $results += trait_uses_recursive($class);
  79. }
  80. return array_unique($results);
  81. }
  82. }
  83. if (! function_exists('e')) {
  84. /**
  85. * Encode HTML special characters in a string.
  86. *
  87. * @param \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|string|null $value
  88. * @param bool $doubleEncode
  89. * @return string
  90. */
  91. function e($value, $doubleEncode = true)
  92. {
  93. if ($value instanceof DeferringDisplayableValue) {
  94. $value = $value->resolveDisplayableValue();
  95. }
  96. if ($value instanceof Htmlable) {
  97. return $value->toHtml();
  98. }
  99. return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8', $doubleEncode);
  100. }
  101. }
  102. if (! function_exists('env')) {
  103. /**
  104. * Gets the value of an environment variable.
  105. *
  106. * @param string $key
  107. * @param mixed $default
  108. * @return mixed
  109. */
  110. function env($key, $default = null)
  111. {
  112. return Env::get($key, $default);
  113. }
  114. }
  115. if (! function_exists('filled')) {
  116. /**
  117. * Determine if a value is "filled".
  118. *
  119. * @param mixed $value
  120. * @return bool
  121. */
  122. function filled($value)
  123. {
  124. return ! blank($value);
  125. }
  126. }
  127. if (! function_exists('object_get')) {
  128. /**
  129. * Get an item from an object using "dot" notation.
  130. *
  131. * @param object $object
  132. * @param string|null $key
  133. * @param mixed $default
  134. * @return mixed
  135. */
  136. function object_get($object, $key, $default = null)
  137. {
  138. if (is_null($key) || trim($key) === '') {
  139. return $object;
  140. }
  141. foreach (explode('.', $key) as $segment) {
  142. if (! is_object($object) || ! isset($object->{$segment})) {
  143. return value($default);
  144. }
  145. $object = $object->{$segment};
  146. }
  147. return $object;
  148. }
  149. }
  150. if (! function_exists('optional')) {
  151. /**
  152. * Provide access to optional objects.
  153. *
  154. * @param mixed $value
  155. * @param callable|null $callback
  156. * @return mixed
  157. */
  158. function optional($value = null, callable $callback = null)
  159. {
  160. if (is_null($callback)) {
  161. return new Optional($value);
  162. } elseif (! is_null($value)) {
  163. return $callback($value);
  164. }
  165. }
  166. }
  167. if (! function_exists('preg_replace_array')) {
  168. /**
  169. * Replace a given pattern with each value in the array in sequentially.
  170. *
  171. * @param string $pattern
  172. * @param array $replacements
  173. * @param string $subject
  174. * @return string
  175. */
  176. function preg_replace_array($pattern, array $replacements, $subject)
  177. {
  178. return preg_replace_callback($pattern, function () use (&$replacements) {
  179. foreach ($replacements as $key => $value) {
  180. return array_shift($replacements);
  181. }
  182. }, $subject);
  183. }
  184. }
  185. if (! function_exists('retry')) {
  186. /**
  187. * Retry an operation a given number of times.
  188. *
  189. * @param int $times
  190. * @param callable $callback
  191. * @param int|\Closure $sleepMilliseconds
  192. * @param callable|null $when
  193. * @return mixed
  194. *
  195. * @throws \Exception
  196. */
  197. function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null)
  198. {
  199. $attempts = 0;
  200. beginning:
  201. $attempts++;
  202. $times--;
  203. try {
  204. return $callback($attempts);
  205. } catch (Exception $e) {
  206. if ($times < 1 || ($when && ! $when($e))) {
  207. throw $e;
  208. }
  209. if ($sleepMilliseconds) {
  210. usleep(value($sleepMilliseconds, $attempts) * 1000);
  211. }
  212. goto beginning;
  213. }
  214. }
  215. }
  216. if (! function_exists('tap')) {
  217. /**
  218. * Call the given Closure with the given value then return the value.
  219. *
  220. * @param mixed $value
  221. * @param callable|null $callback
  222. * @return mixed
  223. */
  224. function tap($value, $callback = null)
  225. {
  226. if (is_null($callback)) {
  227. return new HigherOrderTapProxy($value);
  228. }
  229. $callback($value);
  230. return $value;
  231. }
  232. }
  233. if (! function_exists('throw_if')) {
  234. /**
  235. * Throw the given exception if the given condition is true.
  236. *
  237. * @param mixed $condition
  238. * @param \Throwable|string $exception
  239. * @param mixed ...$parameters
  240. * @return mixed
  241. *
  242. * @throws \Throwable
  243. */
  244. function throw_if($condition, $exception = 'RuntimeException', ...$parameters)
  245. {
  246. if ($condition) {
  247. if (is_string($exception) && class_exists($exception)) {
  248. $exception = new $exception(...$parameters);
  249. }
  250. throw is_string($exception) ? new RuntimeException($exception) : $exception;
  251. }
  252. return $condition;
  253. }
  254. }
  255. if (! function_exists('throw_unless')) {
  256. /**
  257. * Throw the given exception unless the given condition is true.
  258. *
  259. * @param mixed $condition
  260. * @param \Throwable|string $exception
  261. * @param mixed ...$parameters
  262. * @return mixed
  263. *
  264. * @throws \Throwable
  265. */
  266. function throw_unless($condition, $exception = 'RuntimeException', ...$parameters)
  267. {
  268. throw_if(! $condition, $exception, ...$parameters);
  269. return $condition;
  270. }
  271. }
  272. if (! function_exists('trait_uses_recursive')) {
  273. /**
  274. * Returns all traits used by a trait and its traits.
  275. *
  276. * @param string $trait
  277. * @return array
  278. */
  279. function trait_uses_recursive($trait)
  280. {
  281. $traits = class_uses($trait) ?: [];
  282. foreach ($traits as $trait) {
  283. $traits += trait_uses_recursive($trait);
  284. }
  285. return $traits;
  286. }
  287. }
  288. if (! function_exists('transform')) {
  289. /**
  290. * Transform the given value if it is present.
  291. *
  292. * @param mixed $value
  293. * @param callable $callback
  294. * @param mixed $default
  295. * @return mixed|null
  296. */
  297. function transform($value, callable $callback, $default = null)
  298. {
  299. if (filled($value)) {
  300. return $callback($value);
  301. }
  302. if (is_callable($default)) {
  303. return $default($value);
  304. }
  305. return $default;
  306. }
  307. }
  308. if (! function_exists('windows_os')) {
  309. /**
  310. * Determine whether the current environment is Windows based.
  311. *
  312. * @return bool
  313. */
  314. function windows_os()
  315. {
  316. return PHP_OS_FAMILY === 'Windows';
  317. }
  318. }
  319. if (! function_exists('with')) {
  320. /**
  321. * Return the given value, optionally passed through the given callback.
  322. *
  323. * @param mixed $value
  324. * @param callable|null $callback
  325. * @return mixed
  326. */
  327. function with($value, callable $callback = null)
  328. {
  329. return is_null($callback) ? $value : $callback($value);
  330. }
  331. }