PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/btkm/correct_fb
PHP | 885 lines | 448 code | 76 blank | 361 comment | 75 complexity | 20051684d87d16ddc618175095b90fc8 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. }
  443. return $target;
  444. }
  445. }
  446. if (! function_exists('dd')) {
  447. /**
  448. * Dump the passed variables and end the script.
  449. *
  450. * @param mixed
  451. * @return void
  452. */
  453. function dd()
  454. {
  455. array_map(function ($x) {
  456. (new Dumper)->dump($x);
  457. }, func_get_args());
  458. die(1);
  459. }
  460. }
  461. if (! function_exists('e')) {
  462. /**
  463. * Escape HTML entities in a string.
  464. *
  465. * @param \Illuminate\Contracts\Support\Htmlable|string $value
  466. * @return string
  467. */
  468. function e($value)
  469. {
  470. if ($value instanceof Htmlable) {
  471. return $value->toHtml();
  472. }
  473. return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
  474. }
  475. }
  476. if (! function_exists('ends_with')) {
  477. /**
  478. * Determine if a given string ends with a given substring.
  479. *
  480. * @param string $haystack
  481. * @param string|array $needles
  482. * @return bool
  483. */
  484. function ends_with($haystack, $needles)
  485. {
  486. return Str::endsWith($haystack, $needles);
  487. }
  488. }
  489. if (! function_exists('head')) {
  490. /**
  491. * Get the first element of an array. Useful for method chaining.
  492. *
  493. * @param array $array
  494. * @return mixed
  495. */
  496. function head($array)
  497. {
  498. return reset($array);
  499. }
  500. }
  501. if (! function_exists('last')) {
  502. /**
  503. * Get the last element from an array.
  504. *
  505. * @param array $array
  506. * @return mixed
  507. */
  508. function last($array)
  509. {
  510. return end($array);
  511. }
  512. }
  513. if (! function_exists('object_get')) {
  514. /**
  515. * Get an item from an object using "dot" notation.
  516. *
  517. * @param object $object
  518. * @param string $key
  519. * @param mixed $default
  520. * @return mixed
  521. */
  522. function object_get($object, $key, $default = null)
  523. {
  524. if (is_null($key) || trim($key) == '') {
  525. return $object;
  526. }
  527. foreach (explode('.', $key) as $segment) {
  528. if (! is_object($object) || ! isset($object->{$segment})) {
  529. return value($default);
  530. }
  531. $object = $object->{$segment};
  532. }
  533. return $object;
  534. }
  535. }
  536. if (! function_exists('preg_replace_sub')) {
  537. /**
  538. * Replace a given pattern with each value in the array in sequentially.
  539. *
  540. * @param string $pattern
  541. * @param array $replacements
  542. * @param string $subject
  543. * @return string
  544. */
  545. function preg_replace_sub($pattern, &$replacements, $subject)
  546. {
  547. return preg_replace_callback($pattern, function ($match) use (&$replacements) {
  548. foreach ($replacements as $key => $value) {
  549. return array_shift($replacements);
  550. }
  551. }, $subject);
  552. }
  553. }
  554. if (! function_exists('snake_case')) {
  555. /**
  556. * Convert a string to snake case.
  557. *
  558. * @param string $value
  559. * @param string $delimiter
  560. * @return string
  561. */
  562. function snake_case($value, $delimiter = '_')
  563. {
  564. return Str::snake($value, $delimiter);
  565. }
  566. }
  567. if (! function_exists('starts_with')) {
  568. /**
  569. * Determine if a given string starts with a given substring.
  570. *
  571. * @param string $haystack
  572. * @param string|array $needles
  573. * @return bool
  574. */
  575. function starts_with($haystack, $needles)
  576. {
  577. return Str::startsWith($haystack, $needles);
  578. }
  579. }
  580. if (! function_exists('str_contains')) {
  581. /**
  582. * Determine if a given string contains a given substring.
  583. *
  584. * @param string $haystack
  585. * @param string|array $needles
  586. * @return bool
  587. */
  588. function str_contains($haystack, $needles)
  589. {
  590. return Str::contains($haystack, $needles);
  591. }
  592. }
  593. if (! function_exists('str_finish')) {
  594. /**
  595. * Cap a string with a single instance of a given value.
  596. *
  597. * @param string $value
  598. * @param string $cap
  599. * @return string
  600. */
  601. function str_finish($value, $cap)
  602. {
  603. return Str::finish($value, $cap);
  604. }
  605. }
  606. if (! function_exists('str_is')) {
  607. /**
  608. * Determine if a given string matches a given pattern.
  609. *
  610. * @param string $pattern
  611. * @param string $value
  612. * @return bool
  613. */
  614. function str_is($pattern, $value)
  615. {
  616. return Str::is($pattern, $value);
  617. }
  618. }
  619. if (! function_exists('str_limit')) {
  620. /**
  621. * Limit the number of characters in a string.
  622. *
  623. * @param string $value
  624. * @param int $limit
  625. * @param string $end
  626. * @return string
  627. */
  628. function str_limit($value, $limit = 100, $end = '...')
  629. {
  630. return Str::limit($value, $limit, $end);
  631. }
  632. }
  633. if (! function_exists('str_plural')) {
  634. /**
  635. * Get the plural form of an English word.
  636. *
  637. * @param string $value
  638. * @param int $count
  639. * @return string
  640. */
  641. function str_plural($value, $count = 2)
  642. {
  643. return Str::plural($value, $count);
  644. }
  645. }
  646. if (! function_exists('str_random')) {
  647. /**
  648. * Generate a more truly "random" alpha-numeric string.
  649. *
  650. * @param int $length
  651. * @return string
  652. *
  653. * @throws \RuntimeException
  654. */
  655. function str_random($length = 16)
  656. {
  657. return Str::random($length);
  658. }
  659. }
  660. if (! function_exists('str_replace_array')) {
  661. /**
  662. * Replace a given value in the string sequentially with an array.
  663. *
  664. * @param string $search
  665. * @param array $replace
  666. * @param string $subject
  667. * @return string
  668. */
  669. function str_replace_array($search, array $replace, $subject)
  670. {
  671. foreach ($replace as $value) {
  672. $subject = preg_replace('/'.$search.'/', $value, $subject, 1);
  673. }
  674. return $subject;
  675. }
  676. }
  677. if (! function_exists('str_replace_first')) {
  678. /**
  679. * Replace the first occurrence of a given value in the string.
  680. *
  681. * @param string $search
  682. * @param string $replace
  683. * @param string $subject
  684. * @return string
  685. */
  686. function str_replace_first($search, $replace, $subject)
  687. {
  688. return Str::replaceFirst($search, $replace, $subject);
  689. }
  690. }
  691. if (! function_exists('str_replace_last')) {
  692. /**
  693. * Replace the last occurrence of a given value in the string.
  694. *
  695. * @param string $search
  696. * @param string $replace
  697. * @param string $subject
  698. * @return string
  699. */
  700. function str_replace_last($search, $replace, $subject)
  701. {
  702. return Str::replaceLast($search, $replace, $subject);
  703. }
  704. }
  705. if (! function_exists('str_singular')) {
  706. /**
  707. * Get the singular form of an English word.
  708. *
  709. * @param string $value
  710. * @return string
  711. */
  712. function str_singular($value)
  713. {
  714. return Str::singular($value);
  715. }
  716. }
  717. if (! function_exists('str_slug')) {
  718. /**
  719. * Generate a URL friendly "slug" from a given string.
  720. *
  721. * @param string $title
  722. * @param string $separator
  723. * @return string
  724. */
  725. function str_slug($title, $separator = '-')
  726. {
  727. return Str::slug($title, $separator);
  728. }
  729. }
  730. if (! function_exists('studly_case')) {
  731. /**
  732. * Convert a value to studly caps case.
  733. *
  734. * @param string $value
  735. * @return string
  736. */
  737. function studly_case($value)
  738. {
  739. return Str::studly($value);
  740. }
  741. }
  742. if (! function_exists('title_case')) {
  743. /**
  744. * Convert a value to title case.
  745. *
  746. * @param string $value
  747. * @return string
  748. */
  749. function title_case($value)
  750. {
  751. return Str::title($value);
  752. }
  753. }
  754. if (! function_exists('trait_uses_recursive')) {
  755. /**
  756. * Returns all traits used by a trait and its traits.
  757. *
  758. * @param string $trait
  759. * @return array
  760. */
  761. function trait_uses_recursive($trait)
  762. {
  763. $traits = class_uses($trait);
  764. foreach ($traits as $trait) {
  765. $traits += trait_uses_recursive($trait);
  766. }
  767. return $traits;
  768. }
  769. }
  770. if (! function_exists('value')) {
  771. /**
  772. * Return the default value of the given value.
  773. *
  774. * @param mixed $value
  775. * @return mixed
  776. */
  777. function value($value)
  778. {
  779. return $value instanceof Closure ? $value() : $value;
  780. }
  781. }
  782. if (! function_exists('windows_os')) {
  783. /**
  784. * Determine whether the current envrionment is Windows based.
  785. *
  786. * @return bool
  787. */
  788. function windows_os()
  789. {
  790. return strtolower(substr(PHP_OS, 0, 3)) === 'win';
  791. }
  792. }
  793. if (! function_exists('with')) {
  794. /**
  795. * Return the given object. Useful for chaining.
  796. *
  797. * @param mixed $object
  798. * @return mixed
  799. */
  800. function with($object)
  801. {
  802. return $object;
  803. }
  804. }