PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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