PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/PragmaticLinux/Laravel
PHP | 783 lines | 403 code | 65 blank | 315 comment | 56 complexity | 0b1b4f818e0b69370a0cc51b3248bc74 MD5 | raw file
  1. <?php
  2. use Illuminate\Support\Arr;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Support\Collection;
  5. use Illuminate\Support\Debug\Dumper;
  6. if ( ! function_exists('append_config'))
  7. {
  8. /**
  9. * Assign high numeric IDs to a config item to force appending.
  10. *
  11. * @param array $array
  12. * @return array
  13. */
  14. function append_config(array $array)
  15. {
  16. $start = 9999;
  17. foreach ($array as $key => $value)
  18. {
  19. if (is_numeric($key))
  20. {
  21. $start++;
  22. $array[$start] = array_pull($array, $key);
  23. }
  24. }
  25. return $array;
  26. }
  27. }
  28. if ( ! function_exists('array_add'))
  29. {
  30. /**
  31. * Add an element to an array using "dot" notation if it doesn't exist.
  32. *
  33. * @param array $array
  34. * @param string $key
  35. * @param mixed $value
  36. * @return array
  37. */
  38. function array_add($array, $key, $value)
  39. {
  40. return Arr::add($array, $key, $value);
  41. }
  42. }
  43. if ( ! function_exists('array_build'))
  44. {
  45. /**
  46. * Build a new array using a callback.
  47. *
  48. * @param array $array
  49. * @param callable $callback
  50. * @return array
  51. */
  52. function array_build($array, callable $callback)
  53. {
  54. return Arr::build($array, $callback);
  55. }
  56. }
  57. if ( ! function_exists('array_divide'))
  58. {
  59. /**
  60. * Divide an array into two arrays. One with keys and the other with values.
  61. *
  62. * @param array $array
  63. * @return array
  64. */
  65. function array_divide($array)
  66. {
  67. return Arr::divide($array);
  68. }
  69. }
  70. if ( ! function_exists('array_dot'))
  71. {
  72. /**
  73. * Flatten a multi-dimensional associative array with dots.
  74. *
  75. * @param array $array
  76. * @param string $prepend
  77. * @return array
  78. */
  79. function array_dot($array, $prepend = '')
  80. {
  81. return Arr::dot($array, $prepend);
  82. }
  83. }
  84. if ( ! function_exists('array_except'))
  85. {
  86. /**
  87. * Get all of the given array except for a specified array of items.
  88. *
  89. * @param array $array
  90. * @param array|string $keys
  91. * @return array
  92. */
  93. function array_except($array, $keys)
  94. {
  95. return Arr::except($array, $keys);
  96. }
  97. }
  98. if ( ! function_exists('array_fetch'))
  99. {
  100. /**
  101. * Fetch a flattened array of a nested array element.
  102. *
  103. * @param array $array
  104. * @param string $key
  105. * @return array
  106. */
  107. function array_fetch($array, $key)
  108. {
  109. return Arr::fetch($array, $key);
  110. }
  111. }
  112. if ( ! function_exists('array_first'))
  113. {
  114. /**
  115. * Return the first element in an array passing a given truth test.
  116. *
  117. * @param array $array
  118. * @param callable $callback
  119. * @param mixed $default
  120. * @return mixed
  121. */
  122. function array_first($array, callable $callback, $default = null)
  123. {
  124. return Arr::first($array, $callback, $default);
  125. }
  126. }
  127. if ( ! function_exists('array_last'))
  128. {
  129. /**
  130. * Return the last element in an array passing a given truth test.
  131. *
  132. * @param array $array
  133. * @param callable $callback
  134. * @param mixed $default
  135. * @return mixed
  136. */
  137. function array_last($array, $callback, $default = null)
  138. {
  139. return Arr::last($array, $callback, $default);
  140. }
  141. }
  142. if ( ! function_exists('array_flatten'))
  143. {
  144. /**
  145. * Flatten a multi-dimensional array into a single level.
  146. *
  147. * @param array $array
  148. * @return array
  149. */
  150. function array_flatten($array)
  151. {
  152. return Arr::flatten($array);
  153. }
  154. }
  155. if ( ! function_exists('array_forget'))
  156. {
  157. /**
  158. * Remove one or many array items from a given array using "dot" notation.
  159. *
  160. * @param array $array
  161. * @param array|string $keys
  162. * @return void
  163. */
  164. function array_forget(&$array, $keys)
  165. {
  166. return Arr::forget($array, $keys);
  167. }
  168. }
  169. if ( ! function_exists('array_get'))
  170. {
  171. /**
  172. * Get an item from an array using "dot" notation.
  173. *
  174. * @param array $array
  175. * @param string $key
  176. * @param mixed $default
  177. * @return mixed
  178. */
  179. function array_get($array, $key, $default = null)
  180. {
  181. return Arr::get($array, $key, $default);
  182. }
  183. }
  184. if ( ! function_exists('array_has'))
  185. {
  186. /**
  187. * Check if an item exists in an array using "dot" notation.
  188. *
  189. * @param array $array
  190. * @param string $key
  191. * @return bool
  192. */
  193. function array_has($array, $key)
  194. {
  195. return Arr::has($array, $key);
  196. }
  197. }
  198. if ( ! function_exists('array_only'))
  199. {
  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. function array_only($array, $keys)
  208. {
  209. return Arr::only($array, $keys);
  210. }
  211. }
  212. if ( ! function_exists('array_pluck'))
  213. {
  214. /**
  215. * Pluck an array of values from an array.
  216. *
  217. * @param array $array
  218. * @param string $value
  219. * @param string $key
  220. * @return array
  221. */
  222. function array_pluck($array, $value, $key = null)
  223. {
  224. return Arr::pluck($array, $value, $key);
  225. }
  226. }
  227. if ( ! function_exists('array_pull'))
  228. {
  229. /**
  230. * Get a value from the array, and remove it.
  231. *
  232. * @param array $array
  233. * @param string $key
  234. * @param mixed $default
  235. * @return mixed
  236. */
  237. function array_pull(&$array, $key, $default = null)
  238. {
  239. return Arr::pull($array, $key, $default);
  240. }
  241. }
  242. if ( ! function_exists('array_set'))
  243. {
  244. /**
  245. * Set an array item to a given value using "dot" notation.
  246. *
  247. * If no key is given to the method, the entire array will be replaced.
  248. *
  249. * @param array $array
  250. * @param string $key
  251. * @param mixed $value
  252. * @return array
  253. */
  254. function array_set(&$array, $key, $value)
  255. {
  256. return Arr::set($array, $key, $value);
  257. }
  258. }
  259. if ( ! function_exists('array_sort'))
  260. {
  261. /**
  262. * Sort the array using the given callback.
  263. *
  264. * @param array $array
  265. * @param callable $callback
  266. * @return array
  267. */
  268. function array_sort($array, callable $callback)
  269. {
  270. return Arr::sort($array, $callback);
  271. }
  272. }
  273. if ( ! function_exists('array_where'))
  274. {
  275. /**
  276. * Filter the array using the given callback.
  277. *
  278. * @param array $array
  279. * @param callable $callback
  280. * @return array
  281. */
  282. function array_where($array, callable $callback)
  283. {
  284. return Arr::where($array, $callback);
  285. }
  286. }
  287. if ( ! function_exists('camel_case'))
  288. {
  289. /**
  290. * Convert a value to camel case.
  291. *
  292. * @param string $value
  293. * @return string
  294. */
  295. function camel_case($value)
  296. {
  297. return Str::camel($value);
  298. }
  299. }
  300. if ( ! function_exists('class_basename'))
  301. {
  302. /**
  303. * Get the class "basename" of the given object / class.
  304. *
  305. * @param string|object $class
  306. * @return string
  307. */
  308. function class_basename($class)
  309. {
  310. $class = is_object($class) ? get_class($class) : $class;
  311. return basename(str_replace('\\', '/', $class));
  312. }
  313. }
  314. if ( ! function_exists('class_uses_recursive'))
  315. {
  316. /**
  317. * Returns all traits used by a class, it's subclasses and trait of their traits
  318. *
  319. * @param string $class
  320. * @return array
  321. */
  322. function class_uses_recursive($class)
  323. {
  324. $results = [];
  325. foreach (array_merge([$class => $class], class_parents($class)) as $class)
  326. {
  327. $results += trait_uses_recursive($class);
  328. }
  329. return array_unique($results);
  330. }
  331. }
  332. if ( ! function_exists('collect'))
  333. {
  334. /**
  335. * Create a collection from the given value.
  336. *
  337. * @param mixed $value
  338. * @return \Illuminate\Support\Collection
  339. */
  340. function collect($value = null)
  341. {
  342. return new Collection($value);
  343. }
  344. }
  345. if ( ! function_exists('data_get'))
  346. {
  347. /**
  348. * Get an item from an array or object using "dot" notation.
  349. *
  350. * @param mixed $target
  351. * @param string $key
  352. * @param mixed $default
  353. * @return mixed
  354. */
  355. function data_get($target, $key, $default = null)
  356. {
  357. if (is_null($key)) return $target;
  358. foreach (explode('.', $key) as $segment)
  359. {
  360. if (is_array($target))
  361. {
  362. if ( ! array_key_exists($segment, $target))
  363. {
  364. return value($default);
  365. }
  366. $target = $target[$segment];
  367. }
  368. elseif ($target instanceof ArrayAccess)
  369. {
  370. if ( ! isset($target[$segment]))
  371. {
  372. return value($default);
  373. }
  374. $target = $target[$segment];
  375. }
  376. elseif (is_object($target))
  377. {
  378. if ( ! isset($target->{$segment}))
  379. {
  380. return value($default);
  381. }
  382. $target = $target->{$segment};
  383. }
  384. else
  385. {
  386. return value($default);
  387. }
  388. }
  389. return $target;
  390. }
  391. }
  392. if ( ! function_exists('dd'))
  393. {
  394. /**
  395. * Dump the passed variables and end the script.
  396. *
  397. * @param mixed
  398. * @return void
  399. */
  400. function dd()
  401. {
  402. array_map(function($x) { (new Dumper)->dump($x); }, func_get_args());
  403. die;
  404. }
  405. }
  406. if ( ! function_exists('e'))
  407. {
  408. /**
  409. * Escape HTML entities in a string.
  410. *
  411. * @param string $value
  412. * @return string
  413. */
  414. function e($value)
  415. {
  416. return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
  417. }
  418. }
  419. if ( ! function_exists('ends_with'))
  420. {
  421. /**
  422. * Determine if a given string ends with a given substring.
  423. *
  424. * @param string $haystack
  425. * @param string|array $needles
  426. * @return bool
  427. */
  428. function ends_with($haystack, $needles)
  429. {
  430. return Str::endsWith($haystack, $needles);
  431. }
  432. }
  433. if ( ! function_exists('head'))
  434. {
  435. /**
  436. * Get the first element of an array. Useful for method chaining.
  437. *
  438. * @param array $array
  439. * @return mixed
  440. */
  441. function head($array)
  442. {
  443. return reset($array);
  444. }
  445. }
  446. if ( ! function_exists('last'))
  447. {
  448. /**
  449. * Get the last element from an array.
  450. *
  451. * @param array $array
  452. * @return mixed
  453. */
  454. function last($array)
  455. {
  456. return end($array);
  457. }
  458. }
  459. if ( ! function_exists('object_get'))
  460. {
  461. /**
  462. * Get an item from an object using "dot" notation.
  463. *
  464. * @param object $object
  465. * @param string $key
  466. * @param mixed $default
  467. * @return mixed
  468. */
  469. function object_get($object, $key, $default = null)
  470. {
  471. if (is_null($key) || trim($key) == '') return $object;
  472. foreach (explode('.', $key) as $segment)
  473. {
  474. if ( ! is_object($object) || ! isset($object->{$segment}))
  475. {
  476. return value($default);
  477. }
  478. $object = $object->{$segment};
  479. }
  480. return $object;
  481. }
  482. }
  483. if ( ! function_exists('preg_replace_sub'))
  484. {
  485. /**
  486. * Replace a given pattern with each value in the array in sequentially.
  487. *
  488. * @param string $pattern
  489. * @param array $replacements
  490. * @param string $subject
  491. * @return string
  492. */
  493. function preg_replace_sub($pattern, &$replacements, $subject)
  494. {
  495. return preg_replace_callback($pattern, function($match) use (&$replacements)
  496. {
  497. return array_shift($replacements);
  498. }, $subject);
  499. }
  500. }
  501. if ( ! function_exists('snake_case'))
  502. {
  503. /**
  504. * Convert a string to snake case.
  505. *
  506. * @param string $value
  507. * @param string $delimiter
  508. * @return string
  509. */
  510. function snake_case($value, $delimiter = '_')
  511. {
  512. return Str::snake($value, $delimiter);
  513. }
  514. }
  515. if ( ! function_exists('starts_with'))
  516. {
  517. /**
  518. * Determine if a given string starts with a given substring.
  519. *
  520. * @param string $haystack
  521. * @param string|array $needles
  522. * @return bool
  523. */
  524. function starts_with($haystack, $needles)
  525. {
  526. return Str::startsWith($haystack, $needles);
  527. }
  528. }
  529. if ( ! function_exists('str_contains'))
  530. {
  531. /**
  532. * Determine if a given string contains a given substring.
  533. *
  534. * @param string $haystack
  535. * @param string|array $needles
  536. * @return bool
  537. */
  538. function str_contains($haystack, $needles)
  539. {
  540. return Str::contains($haystack, $needles);
  541. }
  542. }
  543. if ( ! function_exists('str_finish'))
  544. {
  545. /**
  546. * Cap a string with a single instance of a given value.
  547. *
  548. * @param string $value
  549. * @param string $cap
  550. * @return string
  551. */
  552. function str_finish($value, $cap)
  553. {
  554. return Str::finish($value, $cap);
  555. }
  556. }
  557. if ( ! function_exists('str_is'))
  558. {
  559. /**
  560. * Determine if a given string matches a given pattern.
  561. *
  562. * @param string $pattern
  563. * @param string $value
  564. * @return bool
  565. */
  566. function str_is($pattern, $value)
  567. {
  568. return Str::is($pattern, $value);
  569. }
  570. }
  571. if ( ! function_exists('str_limit'))
  572. {
  573. /**
  574. * Limit the number of characters in a string.
  575. *
  576. * @param string $value
  577. * @param int $limit
  578. * @param string $end
  579. * @return string
  580. */
  581. function str_limit($value, $limit = 100, $end = '...')
  582. {
  583. return Str::limit($value, $limit, $end);
  584. }
  585. }
  586. if ( ! function_exists('str_plural'))
  587. {
  588. /**
  589. * Get the plural form of an English word.
  590. *
  591. * @param string $value
  592. * @param int $count
  593. * @return string
  594. */
  595. function str_plural($value, $count = 2)
  596. {
  597. return Str::plural($value, $count);
  598. }
  599. }
  600. if ( ! function_exists('str_random'))
  601. {
  602. /**
  603. * Generate a more truly "random" alpha-numeric string.
  604. *
  605. * @param int $length
  606. * @return string
  607. *
  608. * @throws \RuntimeException
  609. */
  610. function str_random($length = 16)
  611. {
  612. return Str::random($length);
  613. }
  614. }
  615. if ( ! function_exists('str_replace_array'))
  616. {
  617. /**
  618. * Replace a given value in the string sequentially with an array.
  619. *
  620. * @param string $search
  621. * @param array $replace
  622. * @param string $subject
  623. * @return string
  624. */
  625. function str_replace_array($search, array $replace, $subject)
  626. {
  627. foreach ($replace as $value)
  628. {
  629. $subject = preg_replace('/'.$search.'/', $value, $subject, 1);
  630. }
  631. return $subject;
  632. }
  633. }
  634. if ( ! function_exists('str_singular'))
  635. {
  636. /**
  637. * Get the singular form of an English word.
  638. *
  639. * @param string $value
  640. * @return string
  641. */
  642. function str_singular($value)
  643. {
  644. return Str::singular($value);
  645. }
  646. }
  647. if ( ! function_exists('str_slug'))
  648. {
  649. /**
  650. * Generate a URL friendly "slug" from a given string.
  651. *
  652. * @param string $title
  653. * @param string $separator
  654. * @return string
  655. */
  656. function str_slug($title, $separator = '-')
  657. {
  658. return Str::slug($title, $separator);
  659. }
  660. }
  661. if ( ! function_exists('studly_case'))
  662. {
  663. /**
  664. * Convert a value to studly caps case.
  665. *
  666. * @param string $value
  667. * @return string
  668. */
  669. function studly_case($value)
  670. {
  671. return Str::studly($value);
  672. }
  673. }
  674. if ( ! function_exists('trait_uses_recursive'))
  675. {
  676. /**
  677. * Returns all traits used by a trait and its traits
  678. *
  679. * @param string $trait
  680. * @return array
  681. */
  682. function trait_uses_recursive($trait)
  683. {
  684. $traits = class_uses($trait);
  685. foreach ($traits as $trait)
  686. {
  687. $traits += trait_uses_recursive($trait);
  688. }
  689. return $traits;
  690. }
  691. }
  692. if ( ! function_exists('value'))
  693. {
  694. /**
  695. * Return the default value of the given value.
  696. *
  697. * @param mixed $value
  698. * @return mixed
  699. */
  700. function value($value)
  701. {
  702. return $value instanceof Closure ? $value() : $value;
  703. }
  704. }
  705. if ( ! function_exists('with'))
  706. {
  707. /**
  708. * Return the given object. Useful for chaining.
  709. *
  710. * @param mixed $object
  711. * @return mixed
  712. */
  713. function with($object)
  714. {
  715. return $object;
  716. }
  717. }