PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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