PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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