You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

helpers.php 31KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. <?php
  2. use PhpOption\Option;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Str;
  5. use Illuminate\Support\Optional;
  6. use Illuminate\Support\Collection;
  7. use Dotenv\Environment\DotenvFactory;
  8. use Illuminate\Contracts\Support\Htmlable;
  9. use Illuminate\Support\HigherOrderTapProxy;
  10. use Dotenv\Environment\Adapter\PutenvAdapter;
  11. use Dotenv\Environment\Adapter\EnvConstAdapter;
  12. use Dotenv\Environment\Adapter\ServerConstAdapter;
  13. if (! function_exists('append_config')) {
  14. /**
  15. * Assign high numeric IDs to a config item to force appending.
  16. *
  17. * @param array $array
  18. * @return array
  19. */
  20. function append_config(array $array)
  21. {
  22. $start = 9999;
  23. foreach ($array as $key => $value) {
  24. if (is_numeric($key)) {
  25. $start++;
  26. $array[$start] = Arr::pull($array, $key);
  27. }
  28. }
  29. return $array;
  30. }
  31. }
  32. if (! function_exists('array_add')) {
  33. /**
  34. * Add an element to an array using "dot" notation if it doesn't exist.
  35. *
  36. * @param array $array
  37. * @param string $key
  38. * @param mixed $value
  39. * @return array
  40. *
  41. * @deprecated Arr::add() should be used directly instead. Will be removed in Laravel 5.9.
  42. */
  43. function array_add($array, $key, $value)
  44. {
  45. return Arr::add($array, $key, $value);
  46. }
  47. }
  48. if (! function_exists('array_collapse')) {
  49. /**
  50. * Collapse an array of arrays into a single array.
  51. *
  52. * @param array $array
  53. * @return array
  54. *
  55. * @deprecated Arr::collapse() should be used directly instead. Will be removed in Laravel 5.9.
  56. */
  57. function array_collapse($array)
  58. {
  59. return Arr::collapse($array);
  60. }
  61. }
  62. if (! function_exists('array_divide')) {
  63. /**
  64. * Divide an array into two arrays. One with keys and the other with values.
  65. *
  66. * @param array $array
  67. * @return array
  68. *
  69. * @deprecated Arr::divide() should be used directly instead. Will be removed in Laravel 5.9.
  70. */
  71. function array_divide($array)
  72. {
  73. return Arr::divide($array);
  74. }
  75. }
  76. if (! function_exists('array_dot')) {
  77. /**
  78. * Flatten a multi-dimensional associative array with dots.
  79. *
  80. * @param array $array
  81. * @param string $prepend
  82. * @return array
  83. *
  84. * @deprecated Arr::dot() should be used directly instead. Will be removed in Laravel 5.9.
  85. */
  86. function array_dot($array, $prepend = '')
  87. {
  88. return Arr::dot($array, $prepend);
  89. }
  90. }
  91. if (! function_exists('array_except')) {
  92. /**
  93. * Get all of the given array except for a specified array of keys.
  94. *
  95. * @param array $array
  96. * @param array|string $keys
  97. * @return array
  98. *
  99. * @deprecated Arr::except() should be used directly instead. Will be removed in Laravel 5.9.
  100. */
  101. function array_except($array, $keys)
  102. {
  103. return Arr::except($array, $keys);
  104. }
  105. }
  106. if (! function_exists('array_first')) {
  107. /**
  108. * Return the first element in an array passing a given truth test.
  109. *
  110. * @param array $array
  111. * @param callable|null $callback
  112. * @param mixed $default
  113. * @return mixed
  114. *
  115. * @deprecated Arr::first() should be used directly instead. Will be removed in Laravel 5.9.
  116. */
  117. function array_first($array, callable $callback = null, $default = null)
  118. {
  119. return Arr::first($array, $callback, $default);
  120. }
  121. }
  122. if (! function_exists('array_flatten')) {
  123. /**
  124. * Flatten a multi-dimensional array into a single level.
  125. *
  126. * @param array $array
  127. * @param int $depth
  128. * @return array
  129. *
  130. * @deprecated Arr::flatten() should be used directly instead. Will be removed in Laravel 5.9.
  131. */
  132. function array_flatten($array, $depth = INF)
  133. {
  134. return Arr::flatten($array, $depth);
  135. }
  136. }
  137. if (! function_exists('array_forget')) {
  138. /**
  139. * Remove one or many array items from a given array using "dot" notation.
  140. *
  141. * @param array $array
  142. * @param array|string $keys
  143. * @return void
  144. *
  145. * @deprecated Arr::forget() should be used directly instead. Will be removed in Laravel 5.9.
  146. */
  147. function array_forget(&$array, $keys)
  148. {
  149. return Arr::forget($array, $keys);
  150. }
  151. }
  152. if (! function_exists('array_get')) {
  153. /**
  154. * Get an item from an array using "dot" notation.
  155. *
  156. * @param \ArrayAccess|array $array
  157. * @param string $key
  158. * @param mixed $default
  159. * @return mixed
  160. *
  161. * @deprecated Arr::get() should be used directly instead. Will be removed in Laravel 5.9.
  162. */
  163. function array_get($array, $key, $default = null)
  164. {
  165. return Arr::get($array, $key, $default);
  166. }
  167. }
  168. if (! function_exists('array_has')) {
  169. /**
  170. * Check if an item or items exist in an array using "dot" notation.
  171. *
  172. * @param \ArrayAccess|array $array
  173. * @param string|array $keys
  174. * @return bool
  175. *
  176. * @deprecated Arr::has() should be used directly instead. Will be removed in Laravel 5.9.
  177. */
  178. function array_has($array, $keys)
  179. {
  180. return Arr::has($array, $keys);
  181. }
  182. }
  183. if (! function_exists('array_last')) {
  184. /**
  185. * Return the last element in an array passing a given truth test.
  186. *
  187. * @param array $array
  188. * @param callable|null $callback
  189. * @param mixed $default
  190. * @return mixed
  191. *
  192. * @deprecated Arr::last() should be used directly instead. Will be removed in Laravel 5.9.
  193. */
  194. function array_last($array, callable $callback = null, $default = null)
  195. {
  196. return Arr::last($array, $callback, $default);
  197. }
  198. }
  199. if (! function_exists('array_only')) {
  200. /**
  201. * Get a subset of the items from the given array.
  202. *
  203. * @param array $array
  204. * @param array|string $keys
  205. * @return array
  206. *
  207. * @deprecated Arr::only() should be used directly instead. Will be removed in Laravel 5.9.
  208. */
  209. function array_only($array, $keys)
  210. {
  211. return Arr::only($array, $keys);
  212. }
  213. }
  214. if (! function_exists('array_pluck')) {
  215. /**
  216. * Pluck an array of values from an array.
  217. *
  218. * @param array $array
  219. * @param string|array $value
  220. * @param string|array|null $key
  221. * @return array
  222. *
  223. * @deprecated Arr::pluck() should be used directly instead. Will be removed in Laravel 5.9.
  224. */
  225. function array_pluck($array, $value, $key = null)
  226. {
  227. return Arr::pluck($array, $value, $key);
  228. }
  229. }
  230. if (! function_exists('array_prepend')) {
  231. /**
  232. * Push an item onto the beginning of an array.
  233. *
  234. * @param array $array
  235. * @param mixed $value
  236. * @param mixed $key
  237. * @return array
  238. *
  239. * @deprecated Arr::prepend() should be used directly instead. Will be removed in Laravel 5.9.
  240. */
  241. function array_prepend($array, $value, $key = null)
  242. {
  243. return Arr::prepend($array, $value, $key);
  244. }
  245. }
  246. if (! function_exists('array_pull')) {
  247. /**
  248. * Get a value from the array, and remove it.
  249. *
  250. * @param array $array
  251. * @param string $key
  252. * @param mixed $default
  253. * @return mixed
  254. *
  255. * @deprecated Arr::pull() should be used directly instead. Will be removed in Laravel 5.9.
  256. */
  257. function array_pull(&$array, $key, $default = null)
  258. {
  259. return Arr::pull($array, $key, $default);
  260. }
  261. }
  262. if (! function_exists('array_random')) {
  263. /**
  264. * Get a random value from an array.
  265. *
  266. * @param array $array
  267. * @param int|null $num
  268. * @return mixed
  269. *
  270. * @deprecated Arr::random() should be used directly instead. Will be removed in Laravel 5.9.
  271. */
  272. function array_random($array, $num = null)
  273. {
  274. return Arr::random($array, $num);
  275. }
  276. }
  277. if (! function_exists('array_set')) {
  278. /**
  279. * Set an array item to a given value using "dot" notation.
  280. *
  281. * If no key is given to the method, the entire array will be replaced.
  282. *
  283. * @param array $array
  284. * @param string $key
  285. * @param mixed $value
  286. * @return array
  287. *
  288. * @deprecated Arr::set() should be used directly instead. Will be removed in Laravel 5.9.
  289. */
  290. function array_set(&$array, $key, $value)
  291. {
  292. return Arr::set($array, $key, $value);
  293. }
  294. }
  295. if (! function_exists('array_sort')) {
  296. /**
  297. * Sort the array by the given callback or attribute name.
  298. *
  299. * @param array $array
  300. * @param callable|string|null $callback
  301. * @return array
  302. *
  303. * @deprecated Arr::sort() should be used directly instead. Will be removed in Laravel 5.9.
  304. */
  305. function array_sort($array, $callback = null)
  306. {
  307. return Arr::sort($array, $callback);
  308. }
  309. }
  310. if (! function_exists('array_sort_recursive')) {
  311. /**
  312. * Recursively sort an array by keys and values.
  313. *
  314. * @param array $array
  315. * @return array
  316. *
  317. * @deprecated Arr::sortRecursive() should be used directly instead. Will be removed in Laravel 5.9.
  318. */
  319. function array_sort_recursive($array)
  320. {
  321. return Arr::sortRecursive($array);
  322. }
  323. }
  324. if (! function_exists('array_where')) {
  325. /**
  326. * Filter the array using the given callback.
  327. *
  328. * @param array $array
  329. * @param callable $callback
  330. * @return array
  331. *
  332. * @deprecated Arr::where() should be used directly instead. Will be removed in Laravel 5.9.
  333. */
  334. function array_where($array, callable $callback)
  335. {
  336. return Arr::where($array, $callback);
  337. }
  338. }
  339. if (! function_exists('array_wrap')) {
  340. /**
  341. * If the given value is not an array, wrap it in one.
  342. *
  343. * @param mixed $value
  344. * @return array
  345. *
  346. * @deprecated Arr::wrap() should be used directly instead. Will be removed in Laravel 5.9.
  347. */
  348. function array_wrap($value)
  349. {
  350. return Arr::wrap($value);
  351. }
  352. }
  353. if (! function_exists('blank')) {
  354. /**
  355. * Determine if the given value is "blank".
  356. *
  357. * @param mixed $value
  358. * @return bool
  359. */
  360. function blank($value)
  361. {
  362. if (is_null($value)) {
  363. return true;
  364. }
  365. if (is_string($value)) {
  366. return trim($value) === '';
  367. }
  368. if (is_numeric($value) || is_bool($value)) {
  369. return false;
  370. }
  371. if ($value instanceof Countable) {
  372. return count($value) === 0;
  373. }
  374. return empty($value);
  375. }
  376. }
  377. if (! function_exists('camel_case')) {
  378. /**
  379. * Convert a value to camel case.
  380. *
  381. * @param string $value
  382. * @return string
  383. *
  384. * @deprecated Str::camel() should be used directly instead. Will be removed in Laravel 5.9.
  385. */
  386. function camel_case($value)
  387. {
  388. return Str::camel($value);
  389. }
  390. }
  391. if (! function_exists('class_basename')) {
  392. /**
  393. * Get the class "basename" of the given object / class.
  394. *
  395. * @param string|object $class
  396. * @return string
  397. */
  398. function class_basename($class)
  399. {
  400. $class = is_object($class) ? get_class($class) : $class;
  401. return basename(str_replace('\\', '/', $class));
  402. }
  403. }
  404. if (! function_exists('class_uses_recursive')) {
  405. /**
  406. * Returns all traits used by a class, its parent classes and trait of their traits.
  407. *
  408. * @param object|string $class
  409. * @return array
  410. */
  411. function class_uses_recursive($class)
  412. {
  413. if (is_object($class)) {
  414. $class = get_class($class);
  415. }
  416. $results = [];
  417. foreach (array_reverse(class_parents($class)) + [$class => $class] as $class) {
  418. $results += trait_uses_recursive($class);
  419. }
  420. return array_unique($results);
  421. }
  422. }
  423. if (! function_exists('collect')) {
  424. /**
  425. * Create a collection from the given value.
  426. *
  427. * @param mixed $value
  428. * @return \Illuminate\Support\Collection
  429. */
  430. function collect($value = null)
  431. {
  432. return new Collection($value);
  433. }
  434. }
  435. if (! function_exists('data_fill')) {
  436. /**
  437. * Fill in data where it's missing.
  438. *
  439. * @param mixed $target
  440. * @param string|array $key
  441. * @param mixed $value
  442. * @return mixed
  443. */
  444. function data_fill(&$target, $key, $value)
  445. {
  446. return data_set($target, $key, $value, false);
  447. }
  448. }
  449. if (! function_exists('data_get')) {
  450. /**
  451. * Get an item from an array or object using "dot" notation.
  452. *
  453. * @param mixed $target
  454. * @param string|array|int $key
  455. * @param mixed $default
  456. * @return mixed
  457. */
  458. function data_get($target, $key, $default = null)
  459. {
  460. if (is_null($key)) {
  461. return $target;
  462. }
  463. $key = is_array($key) ? $key : explode('.', $key);
  464. while (! is_null($segment = array_shift($key))) {
  465. if ($segment === '*') {
  466. if ($target instanceof Collection) {
  467. $target = $target->all();
  468. } elseif (! is_array($target)) {
  469. return value($default);
  470. }
  471. $result = [];
  472. foreach ($target as $item) {
  473. $result[] = data_get($item, $key);
  474. }
  475. return in_array('*', $key) ? Arr::collapse($result) : $result;
  476. }
  477. if (Arr::accessible($target) && Arr::exists($target, $segment)) {
  478. $target = $target[$segment];
  479. } elseif (is_object($target) && isset($target->{$segment})) {
  480. $target = $target->{$segment};
  481. } else {
  482. return value($default);
  483. }
  484. }
  485. return $target;
  486. }
  487. }
  488. if (! function_exists('data_set')) {
  489. /**
  490. * Set an item on an array or object using dot notation.
  491. *
  492. * @param mixed $target
  493. * @param string|array $key
  494. * @param mixed $value
  495. * @param bool $overwrite
  496. * @return mixed
  497. */
  498. function data_set(&$target, $key, $value, $overwrite = true)
  499. {
  500. $segments = is_array($key) ? $key : explode('.', $key);
  501. if (($segment = array_shift($segments)) === '*') {
  502. if (! Arr::accessible($target)) {
  503. $target = [];
  504. }
  505. if ($segments) {
  506. foreach ($target as &$inner) {
  507. data_set($inner, $segments, $value, $overwrite);
  508. }
  509. } elseif ($overwrite) {
  510. foreach ($target as &$inner) {
  511. $inner = $value;
  512. }
  513. }
  514. } elseif (Arr::accessible($target)) {
  515. if ($segments) {
  516. if (! Arr::exists($target, $segment)) {
  517. $target[$segment] = [];
  518. }
  519. data_set($target[$segment], $segments, $value, $overwrite);
  520. } elseif ($overwrite || ! Arr::exists($target, $segment)) {
  521. $target[$segment] = $value;
  522. }
  523. } elseif (is_object($target)) {
  524. if ($segments) {
  525. if (! isset($target->{$segment})) {
  526. $target->{$segment} = [];
  527. }
  528. data_set($target->{$segment}, $segments, $value, $overwrite);
  529. } elseif ($overwrite || ! isset($target->{$segment})) {
  530. $target->{$segment} = $value;
  531. }
  532. } else {
  533. $target = [];
  534. if ($segments) {
  535. data_set($target[$segment], $segments, $value, $overwrite);
  536. } elseif ($overwrite) {
  537. $target[$segment] = $value;
  538. }
  539. }
  540. return $target;
  541. }
  542. }
  543. if (! function_exists('e')) {
  544. /**
  545. * Encode HTML special characters in a string.
  546. *
  547. * @param \Illuminate\Contracts\Support\Htmlable|string $value
  548. * @param bool $doubleEncode
  549. * @return string
  550. */
  551. function e($value, $doubleEncode = true)
  552. {
  553. if ($value instanceof Htmlable) {
  554. return $value->toHtml();
  555. }
  556. return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', $doubleEncode);
  557. }
  558. }
  559. if (! function_exists('ends_with')) {
  560. /**
  561. * Determine if a given string ends with a given substring.
  562. *
  563. * @param string $haystack
  564. * @param string|array $needles
  565. * @return bool
  566. *
  567. * @deprecated Str::endsWith() should be used directly instead. Will be removed in Laravel 5.9.
  568. */
  569. function ends_with($haystack, $needles)
  570. {
  571. return Str::endsWith($haystack, $needles);
  572. }
  573. }
  574. if (! function_exists('env')) {
  575. /**
  576. * Gets the value of an environment variable.
  577. *
  578. * @param string $key
  579. * @param mixed $default
  580. * @return mixed
  581. */
  582. function env($key, $default = null)
  583. {
  584. static $variables;
  585. if ($variables === null) {
  586. $variables = (new DotenvFactory([new EnvConstAdapter, new PutenvAdapter, new ServerConstAdapter]))->createImmutable();
  587. }
  588. return Option::fromValue($variables->get($key))
  589. ->map(function ($value) {
  590. switch (strtolower($value)) {
  591. case 'true':
  592. case '(true)':
  593. return true;
  594. case 'false':
  595. case '(false)':
  596. return false;
  597. case 'empty':
  598. case '(empty)':
  599. return '';
  600. case 'null':
  601. case '(null)':
  602. return;
  603. }
  604. if (preg_match('/\A([\'"])(.*)\1\z/', $value, $matches)) {
  605. return $matches[2];
  606. }
  607. return $value;
  608. })
  609. ->getOrCall(function () use ($default) {
  610. return value($default);
  611. });
  612. }
  613. }
  614. if (! function_exists('filled')) {
  615. /**
  616. * Determine if a value is "filled".
  617. *
  618. * @param mixed $value
  619. * @return bool
  620. */
  621. function filled($value)
  622. {
  623. return ! blank($value);
  624. }
  625. }
  626. if (! function_exists('head')) {
  627. /**
  628. * Get the first element of an array. Useful for method chaining.
  629. *
  630. * @param array $array
  631. * @return mixed
  632. */
  633. function head($array)
  634. {
  635. return reset($array);
  636. }
  637. }
  638. if (! function_exists('kebab_case')) {
  639. /**
  640. * Convert a string to kebab case.
  641. *
  642. * @param string $value
  643. * @return string
  644. *
  645. * @deprecated Str::kebab() should be used directly instead. Will be removed in Laravel 5.9.
  646. */
  647. function kebab_case($value)
  648. {
  649. return Str::kebab($value);
  650. }
  651. }
  652. if (! function_exists('last')) {
  653. /**
  654. * Get the last element from an array.
  655. *
  656. * @param array $array
  657. * @return mixed
  658. */
  659. function last($array)
  660. {
  661. return end($array);
  662. }
  663. }
  664. if (! function_exists('object_get')) {
  665. /**
  666. * Get an item from an object using "dot" notation.
  667. *
  668. * @param object $object
  669. * @param string $key
  670. * @param mixed $default
  671. * @return mixed
  672. */
  673. function object_get($object, $key, $default = null)
  674. {
  675. if (is_null($key) || trim($key) == '') {
  676. return $object;
  677. }
  678. foreach (explode('.', $key) as $segment) {
  679. if (! is_object($object) || ! isset($object->{$segment})) {
  680. return value($default);
  681. }
  682. $object = $object->{$segment};
  683. }
  684. return $object;
  685. }
  686. }
  687. if (! function_exists('optional')) {
  688. /**
  689. * Provide access to optional objects.
  690. *
  691. * @param mixed $value
  692. * @param callable|null $callback
  693. * @return mixed
  694. */
  695. function optional($value = null, callable $callback = null)
  696. {
  697. if (is_null($callback)) {
  698. return new Optional($value);
  699. } elseif (! is_null($value)) {
  700. return $callback($value);
  701. }
  702. }
  703. }
  704. if (! function_exists('preg_replace_array')) {
  705. /**
  706. * Replace a given pattern with each value in the array in sequentially.
  707. *
  708. * @param string $pattern
  709. * @param array $replacements
  710. * @param string $subject
  711. * @return string
  712. */
  713. function preg_replace_array($pattern, array $replacements, $subject)
  714. {
  715. return preg_replace_callback($pattern, function () use (&$replacements) {
  716. foreach ($replacements as $key => $value) {
  717. return array_shift($replacements);
  718. }
  719. }, $subject);
  720. }
  721. }
  722. if (! function_exists('retry')) {
  723. /**
  724. * Retry an operation a given number of times.
  725. *
  726. * @param int $times
  727. * @param callable $callback
  728. * @param int $sleep
  729. * @return mixed
  730. *
  731. * @throws \Exception
  732. */
  733. function retry($times, callable $callback, $sleep = 0)
  734. {
  735. $attempts = 0;
  736. $times--;
  737. beginning:
  738. $attempts++;
  739. try {
  740. return $callback($attempts);
  741. } catch (Exception $e) {
  742. if (! $times) {
  743. throw $e;
  744. }
  745. $times--;
  746. if ($sleep) {
  747. usleep($sleep * 1000);
  748. }
  749. goto beginning;
  750. }
  751. }
  752. }
  753. if (! function_exists('snake_case')) {
  754. /**
  755. * Convert a string to snake case.
  756. *
  757. * @param string $value
  758. * @param string $delimiter
  759. * @return string
  760. *
  761. * @deprecated Str::snake() should be used directly instead. Will be removed in Laravel 5.9.
  762. */
  763. function snake_case($value, $delimiter = '_')
  764. {
  765. return Str::snake($value, $delimiter);
  766. }
  767. }
  768. if (! function_exists('starts_with')) {
  769. /**
  770. * Determine if a given string starts with a given substring.
  771. *
  772. * @param string $haystack
  773. * @param string|array $needles
  774. * @return bool
  775. *
  776. * @deprecated Str::startsWith() should be used directly instead. Will be removed in Laravel 5.9.
  777. */
  778. function starts_with($haystack, $needles)
  779. {
  780. return Str::startsWith($haystack, $needles);
  781. }
  782. }
  783. if (! function_exists('str_after')) {
  784. /**
  785. * Return the remainder of a string after a given value.
  786. *
  787. * @param string $subject
  788. * @param string $search
  789. * @return string
  790. *
  791. * @deprecated Str::after() should be used directly instead. Will be removed in Laravel 5.9.
  792. */
  793. function str_after($subject, $search)
  794. {
  795. return Str::after($subject, $search);
  796. }
  797. }
  798. if (! function_exists('str_before')) {
  799. /**
  800. * Get the portion of a string before a given value.
  801. *
  802. * @param string $subject
  803. * @param string $search
  804. * @return string
  805. *
  806. * @deprecated Str::before() should be used directly instead. Will be removed in Laravel 5.9.
  807. */
  808. function str_before($subject, $search)
  809. {
  810. return Str::before($subject, $search);
  811. }
  812. }
  813. if (! function_exists('str_contains')) {
  814. /**
  815. * Determine if a given string contains a given substring.
  816. *
  817. * @param string $haystack
  818. * @param string|array $needles
  819. * @return bool
  820. *
  821. * @deprecated Str::contains() should be used directly instead. Will be removed in Laravel 5.9.
  822. */
  823. function str_contains($haystack, $needles)
  824. {
  825. return Str::contains($haystack, $needles);
  826. }
  827. }
  828. if (! function_exists('str_finish')) {
  829. /**
  830. * Cap a string with a single instance of a given value.
  831. *
  832. * @param string $value
  833. * @param string $cap
  834. * @return string
  835. *
  836. * @deprecated Str::finish() should be used directly instead. Will be removed in Laravel 5.9.
  837. */
  838. function str_finish($value, $cap)
  839. {
  840. return Str::finish($value, $cap);
  841. }
  842. }
  843. if (! function_exists('str_is')) {
  844. /**
  845. * Determine if a given string matches a given pattern.
  846. *
  847. * @param string|array $pattern
  848. * @param string $value
  849. * @return bool
  850. *
  851. * @deprecated Str::is() should be used directly instead. Will be removed in Laravel 5.9.
  852. */
  853. function str_is($pattern, $value)
  854. {
  855. return Str::is($pattern, $value);
  856. }
  857. }
  858. if (! function_exists('str_limit')) {
  859. /**
  860. * Limit the number of characters in a string.
  861. *
  862. * @param string $value
  863. * @param int $limit
  864. * @param string $end
  865. * @return string
  866. *
  867. * @deprecated Str::limit() should be used directly instead. Will be removed in Laravel 5.9.
  868. */
  869. function str_limit($value, $limit = 100, $end = '...')
  870. {
  871. return Str::limit($value, $limit, $end);
  872. }
  873. }
  874. if (! function_exists('str_plural')) {
  875. /**
  876. * Get the plural form of an English word.
  877. *
  878. * @param string $value
  879. * @param int $count
  880. * @return string
  881. *
  882. * @deprecated Str::plural() should be used directly instead. Will be removed in Laravel 5.9.
  883. */
  884. function str_plural($value, $count = 2)
  885. {
  886. return Str::plural($value, $count);
  887. }
  888. }
  889. if (! function_exists('str_random')) {
  890. /**
  891. * Generate a more truly "random" alpha-numeric string.
  892. *
  893. * @param int $length
  894. * @return string
  895. *
  896. * @throws \RuntimeException
  897. *
  898. * @deprecated Str::random() should be used directly instead. Will be removed in Laravel 5.9.
  899. */
  900. function str_random($length = 16)
  901. {
  902. return Str::random($length);
  903. }
  904. }
  905. if (! function_exists('str_replace_array')) {
  906. /**
  907. * Replace a given value in the string sequentially with an array.
  908. *
  909. * @param string $search
  910. * @param array $replace
  911. * @param string $subject
  912. * @return string
  913. *
  914. * @deprecated Str::replaceArray() should be used directly instead. Will be removed in Laravel 5.9.
  915. */
  916. function str_replace_array($search, array $replace, $subject)
  917. {
  918. return Str::replaceArray($search, $replace, $subject);
  919. }
  920. }
  921. if (! function_exists('str_replace_first')) {
  922. /**
  923. * Replace the first occurrence of a given value in the string.
  924. *
  925. * @param string $search
  926. * @param string $replace
  927. * @param string $subject
  928. * @return string
  929. *
  930. * @deprecated Str::replaceFirst() should be used directly instead. Will be removed in Laravel 5.9.
  931. */
  932. function str_replace_first($search, $replace, $subject)
  933. {
  934. return Str::replaceFirst($search, $replace, $subject);
  935. }
  936. }
  937. if (! function_exists('str_replace_last')) {
  938. /**
  939. * Replace the last occurrence of a given value in the string.
  940. *
  941. * @param string $search
  942. * @param string $replace
  943. * @param string $subject
  944. * @return string
  945. *
  946. * @deprecated Str::replaceLast() should be used directly instead. Will be removed in Laravel 5.9.
  947. */
  948. function str_replace_last($search, $replace, $subject)
  949. {
  950. return Str::replaceLast($search, $replace, $subject);
  951. }
  952. }
  953. if (! function_exists('str_singular')) {
  954. /**
  955. * Get the singular form of an English word.
  956. *
  957. * @param string $value
  958. * @return string
  959. *
  960. * @deprecated Str::singular() should be used directly instead. Will be removed in Laravel 5.9.
  961. */
  962. function str_singular($value)
  963. {
  964. return Str::singular($value);
  965. }
  966. }
  967. if (! function_exists('str_slug')) {
  968. /**
  969. * Generate a URL friendly "slug" from a given string.
  970. *
  971. * @param string $title
  972. * @param string $separator
  973. * @param string $language
  974. * @return string
  975. *
  976. * @deprecated Str::slug() should be used directly instead. Will be removed in Laravel 5.9.
  977. */
  978. function str_slug($title, $separator = '-', $language = 'en')
  979. {
  980. return Str::slug($title, $separator, $language);
  981. }
  982. }
  983. if (! function_exists('str_start')) {
  984. /**
  985. * Begin a string with a single instance of a given value.
  986. *
  987. * @param string $value
  988. * @param string $prefix
  989. * @return string
  990. *
  991. * @deprecated Str::start() should be used directly instead. Will be removed in Laravel 5.9.
  992. */
  993. function str_start($value, $prefix)
  994. {
  995. return Str::start($value, $prefix);
  996. }
  997. }
  998. if (! function_exists('studly_case')) {
  999. /**
  1000. * Convert a value to studly caps case.
  1001. *
  1002. * @param string $value
  1003. * @return string
  1004. *
  1005. * @deprecated Str::studly() should be used directly instead. Will be removed in Laravel 5.9.
  1006. */
  1007. function studly_case($value)
  1008. {
  1009. return Str::studly($value);
  1010. }
  1011. }
  1012. if (! function_exists('tap')) {
  1013. /**
  1014. * Call the given Closure with the given value then return the value.
  1015. *
  1016. * @param mixed $value
  1017. * @param callable|null $callback
  1018. * @return mixed
  1019. */
  1020. function tap($value, $callback = null)
  1021. {
  1022. if (is_null($callback)) {
  1023. return new HigherOrderTapProxy($value);
  1024. }
  1025. $callback($value);
  1026. return $value;
  1027. }
  1028. }
  1029. if (! function_exists('throw_if')) {
  1030. /**
  1031. * Throw the given exception if the given condition is true.
  1032. *
  1033. * @param mixed $condition
  1034. * @param \Throwable|string $exception
  1035. * @param array ...$parameters
  1036. * @return mixed
  1037. *
  1038. * @throws \Throwable
  1039. */
  1040. function throw_if($condition, $exception, ...$parameters)
  1041. {
  1042. if ($condition) {
  1043. throw (is_string($exception) ? new $exception(...$parameters) : $exception);
  1044. }
  1045. return $condition;
  1046. }
  1047. }
  1048. if (! function_exists('throw_unless')) {
  1049. /**
  1050. * Throw the given exception unless the given condition is true.
  1051. *
  1052. * @param mixed $condition
  1053. * @param \Throwable|string $exception
  1054. * @param array ...$parameters
  1055. * @return mixed
  1056. * @throws \Throwable
  1057. */
  1058. function throw_unless($condition, $exception, ...$parameters)
  1059. {
  1060. if (! $condition) {
  1061. throw (is_string($exception) ? new $exception(...$parameters) : $exception);
  1062. }
  1063. return $condition;
  1064. }
  1065. }
  1066. if (! function_exists('title_case')) {
  1067. /**
  1068. * Convert a value to title case.
  1069. *
  1070. * @param string $value
  1071. * @return string
  1072. *
  1073. * @deprecated Str::title() should be used directly instead. Will be removed in Laravel 5.9.
  1074. */
  1075. function title_case($value)
  1076. {
  1077. return Str::title($value);
  1078. }
  1079. }
  1080. if (! function_exists('trait_uses_recursive')) {
  1081. /**
  1082. * Returns all traits used by a trait and its traits.
  1083. *
  1084. * @param string $trait
  1085. * @return array
  1086. */
  1087. function trait_uses_recursive($trait)
  1088. {
  1089. $traits = class_uses($trait);
  1090. foreach ($traits as $trait) {
  1091. $traits += trait_uses_recursive($trait);
  1092. }
  1093. return $traits;
  1094. }
  1095. }
  1096. if (! function_exists('transform')) {
  1097. /**
  1098. * Transform the given value if it is present.
  1099. *
  1100. * @param mixed $value
  1101. * @param callable $callback
  1102. * @param mixed $default
  1103. * @return mixed|null
  1104. */
  1105. function transform($value, callable $callback, $default = null)
  1106. {
  1107. if (filled($value)) {
  1108. return $callback($value);
  1109. }
  1110. if (is_callable($default)) {
  1111. return $default($value);
  1112. }
  1113. return $default;
  1114. }
  1115. }
  1116. if (! function_exists('value')) {
  1117. /**
  1118. * Return the default value of the given value.
  1119. *
  1120. * @param mixed $value
  1121. * @return mixed
  1122. */
  1123. function value($value)
  1124. {
  1125. return $value instanceof Closure ? $value() : $value;
  1126. }
  1127. }
  1128. if (! function_exists('windows_os')) {
  1129. /**
  1130. * Determine whether the current environment is Windows based.
  1131. *
  1132. * @return bool
  1133. */
  1134. function windows_os()
  1135. {
  1136. return strtolower(substr(PHP_OS, 0, 3)) === 'win';
  1137. }
  1138. }
  1139. if (! function_exists('with')) {
  1140. /**
  1141. * Return the given value, optionally passed through the given callback.
  1142. *
  1143. * @param mixed $value
  1144. * @param callable|null $callback
  1145. * @return mixed
  1146. */
  1147. function with($value, callable $callback = null)
  1148. {
  1149. return is_null($callback) ? $value : $callback($value);
  1150. }
  1151. }