helpers.func.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. <?php
  2. /**
  3. * 辅助函数.
  4. *
  5. * @author nj
  6. * @date 2021-05-14 11:05:31
  7. */
  8. use Illuminate\Support\Arr;
  9. use Illuminate\Support\Str;
  10. use Illuminate\Support\Collection;
  11. if (! function_exists('array_add')) {
  12. /**
  13. * Add an element to an array using "dot" notation if it doesn't exist.
  14. *
  15. * @param array $array
  16. * @param string $key
  17. * @param mixed $value
  18. * @return array
  19. */
  20. function array_add($array, $key, $value)
  21. {
  22. return Arr::add($array, $key, $value);
  23. }
  24. }
  25. if (! function_exists('array_build')) {
  26. /**
  27. * Build a new array using a callback.
  28. *
  29. * @param array $array
  30. * @param callable $callback
  31. * @return array
  32. */
  33. function array_build($array, callable $callback)
  34. {
  35. return Arr::build($array, $callback);
  36. }
  37. }
  38. if (! function_exists('array_collapse')) {
  39. /**
  40. * Collapse an array of arrays into a single array.
  41. *
  42. * @param \ArrayAccess|array $array
  43. * @return array
  44. */
  45. function array_collapse($array)
  46. {
  47. return Arr::collapse($array);
  48. }
  49. }
  50. if (! function_exists('array_divide')) {
  51. /**
  52. * Divide an array into two arrays. One with keys and the other with values.
  53. *
  54. * @param array $array
  55. * @return array
  56. */
  57. function array_divide($array)
  58. {
  59. return Arr::divide($array);
  60. }
  61. }
  62. if (! function_exists('array_dot')) {
  63. /**
  64. * Flatten a multi-dimensional associative array with dots.
  65. *
  66. * @param array $array
  67. * @param string $prepend
  68. * @return array
  69. */
  70. function array_dot($array, $prepend = '')
  71. {
  72. return Arr::dot($array, $prepend);
  73. }
  74. }
  75. if (! function_exists('array_except')) {
  76. /**
  77. * Get all of the given array except for a specified array of items.
  78. *
  79. * @param array $array
  80. * @param array|string $keys
  81. * @return array
  82. */
  83. function array_except($array, $keys)
  84. {
  85. return Arr::except($array, $keys);
  86. }
  87. }
  88. if (! function_exists('array_fetch')) {
  89. /**
  90. * Fetch a flattened array of a nested array element.
  91. *
  92. * @param array $array
  93. * @param string $key
  94. * @return array
  95. *
  96. * @deprecated since version 5.1. Use array_pluck instead.
  97. */
  98. function array_fetch($array, $key)
  99. {
  100. return Arr::fetch($array, $key);
  101. }
  102. }
  103. if (! function_exists('array_first')) {
  104. /**
  105. * Return the first element in an array passing a given truth test.
  106. *
  107. * @param array $array
  108. * @param callable $callback
  109. * @param mixed $default
  110. * @return mixed
  111. */
  112. function array_first($array, callable $callback, $default = null)
  113. {
  114. return Arr::first($array, $callback, $default);
  115. }
  116. }
  117. if (! function_exists('array_flatten')) {
  118. /**
  119. * Flatten a multi-dimensional array into a single level.
  120. *
  121. * @param array $array
  122. * @return array
  123. */
  124. function array_flatten($array)
  125. {
  126. return Arr::flatten($array);
  127. }
  128. }
  129. if (! function_exists('array_forget')) {
  130. /**
  131. * Remove one or many array items from a given array using "dot" notation.
  132. *
  133. * @param array $array
  134. * @param array|string $keys
  135. * @return void
  136. */
  137. function array_forget(&$array, $keys)
  138. {
  139. return Arr::forget($array, $keys);
  140. }
  141. }
  142. if (! function_exists('array_get')) {
  143. /**
  144. * Get an item from an array using "dot" notation.
  145. *
  146. * @param array $array
  147. * @param string $key
  148. * @param mixed $default
  149. * @return mixed
  150. */
  151. function array_get($array, $key, $default = null)
  152. {
  153. return Arr::get($array, $key, $default);
  154. }
  155. }
  156. if (! function_exists('array_has')) {
  157. /**
  158. * Check if an item exists in an array using "dot" notation.
  159. *
  160. * @param array $array
  161. * @param string $key
  162. * @return bool
  163. */
  164. function array_has($array, $key)
  165. {
  166. return Arr::has($array, $key);
  167. }
  168. }
  169. if (! function_exists('array_last')) {
  170. /**
  171. * Return the last element in an array passing a given truth test.
  172. *
  173. * @param array $array
  174. * @param callable $callback
  175. * @param mixed $default
  176. * @return mixed
  177. */
  178. function array_last($array, $callback, $default = null)
  179. {
  180. return Arr::last($array, $callback, $default);
  181. }
  182. }
  183. if (! function_exists('array_only')) {
  184. /**
  185. * Get a subset of the items from the given array.
  186. *
  187. * @param array $array
  188. * @param array|string $keys
  189. * @return array
  190. */
  191. function array_only($array, $keys)
  192. {
  193. return Arr::only($array, $keys);
  194. }
  195. }
  196. if (! function_exists('array_pluck')) {
  197. /**
  198. * Pluck an array of values from an array.
  199. *
  200. * @param array $array
  201. * @param string|array $value
  202. * @param string|array|null $key
  203. * @return array
  204. */
  205. function array_pluck($array, $value, $key = null)
  206. {
  207. return Arr::pluck($array, $value, $key);
  208. }
  209. }
  210. if (! function_exists('array_prepend')) {
  211. /**
  212. * Push an item onto the beginning of an array.
  213. *
  214. * @param array $array
  215. * @param mixed $value
  216. * @param mixed $key
  217. * @return array
  218. */
  219. function array_prepend($array, $value, $key = null)
  220. {
  221. return Arr::prepend($array, $value, $key);
  222. }
  223. }
  224. if (! function_exists('array_pull')) {
  225. /**
  226. * Get a value from the array, and remove it.
  227. *
  228. * @param array $array
  229. * @param string $key
  230. * @param mixed $default
  231. * @return mixed
  232. */
  233. function array_pull(&$array, $key, $default = null)
  234. {
  235. return Arr::pull($array, $key, $default);
  236. }
  237. }
  238. if (! function_exists('array_set')) {
  239. /**
  240. * Set an array item to a given value using "dot" notation.
  241. *
  242. * If no key is given to the method, the entire array will be replaced.
  243. *
  244. * @param array $array
  245. * @param string $key
  246. * @param mixed $value
  247. * @return array
  248. */
  249. function array_set(&$array, $key, $value)
  250. {
  251. return Arr::set($array, $key, $value);
  252. }
  253. }
  254. if (! function_exists('array_sort')) {
  255. /**
  256. * Sort the array using the given callback.
  257. *
  258. * @param array $array
  259. * @param callable $callback
  260. * @return array
  261. */
  262. function array_sort($array, callable $callback)
  263. {
  264. return Arr::sort($array, $callback);
  265. }
  266. }
  267. if (! function_exists('array_sort_recursive')) {
  268. /**
  269. * Recursively sort an array by keys and values.
  270. *
  271. * @param array $array
  272. * @return array
  273. */
  274. function array_sort_recursive($array)
  275. {
  276. return Arr::sortRecursive($array);
  277. }
  278. }
  279. if (! function_exists('array_where')) {
  280. /**
  281. * Filter the array using the given callback.
  282. *
  283. * @param array $array
  284. * @param callable $callback
  285. * @return array
  286. */
  287. function array_where($array, callable $callback)
  288. {
  289. return Arr::where($array, $callback);
  290. }
  291. }
  292. if (! function_exists('camel_case')) {
  293. /**
  294. * Convert a value to camel case.
  295. *
  296. * @param string $value
  297. * @return string
  298. */
  299. function camel_case($value)
  300. {
  301. return Str::camel($value);
  302. }
  303. }
  304. if (! function_exists('class_basename')) {
  305. /**
  306. * Get the class "basename" of the given object / class.
  307. *
  308. * @param string|object $class
  309. * @return string
  310. */
  311. function class_basename($class)
  312. {
  313. $class = is_object($class) ? get_class($class) : $class;
  314. return basename(str_replace('\\', '/', $class));
  315. }
  316. }
  317. if (! function_exists('class_uses_recursive')) {
  318. /**
  319. * Returns all traits used by a class, its subclasses and trait of their traits.
  320. *
  321. * @param string $class
  322. * @return array
  323. */
  324. function class_uses_recursive($class)
  325. {
  326. $results = [];
  327. foreach (array_merge([$class => $class], class_parents($class)) as $class) {
  328. $results += trait_uses_recursive($class);
  329. }
  330. return array_unique($results);
  331. }
  332. }
  333. if (! function_exists('collect')) {
  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. * Get an item from an array or object using "dot" notation.
  348. *
  349. * @param mixed $target
  350. * @param string|array $key
  351. * @param mixed $default
  352. * @return mixed
  353. */
  354. function data_get($target, $key, $default = null)
  355. {
  356. if (is_null($key)) {
  357. return $target;
  358. }
  359. $key = is_array($key) ? $key : explode('.', $key);
  360. foreach ($key as $segment) {
  361. if (is_array($target)) {
  362. if (! array_key_exists($segment, $target)) {
  363. return value($default);
  364. }
  365. $target = $target[$segment];
  366. } elseif ($target instanceof ArrayAccess) {
  367. if (! isset($target[$segment])) {
  368. return value($default);
  369. }
  370. $target = $target[$segment];
  371. } elseif (is_object($target)) {
  372. if (! isset($target->{$segment})) {
  373. return value($default);
  374. }
  375. $target = $target->{$segment};
  376. } else {
  377. return value($default);
  378. }
  379. }
  380. return $target;
  381. }
  382. }
  383. if (! function_exists('ends_with')) {
  384. /**
  385. * Determine if a given string ends with a given substring.
  386. *
  387. * @param string $haystack
  388. * @param string|array $needles
  389. * @return bool
  390. */
  391. function ends_with($haystack, $needles)
  392. {
  393. return Str::endsWith($haystack, $needles);
  394. }
  395. }
  396. if (! function_exists('preg_replace_sub')) {
  397. /**
  398. * Replace a given pattern with each value in the array in sequentially.
  399. *
  400. * @param string $pattern
  401. * @param array $replacements
  402. * @param string $subject
  403. * @return string
  404. */
  405. function preg_replace_sub($pattern, &$replacements, $subject)
  406. {
  407. return preg_replace_callback($pattern, function ($match) use (&$replacements) {
  408. foreach ($replacements as $key => $value) {
  409. return array_shift($replacements);
  410. }
  411. }, $subject);
  412. }
  413. }
  414. if (! function_exists('snake_case')) {
  415. /**
  416. * Convert a string to snake case.
  417. *
  418. * @param string $value
  419. * @param string $delimiter
  420. * @return string
  421. */
  422. function snake_case($value, $delimiter = '_')
  423. {
  424. return Str::snake($value, $delimiter);
  425. }
  426. }
  427. if (! function_exists('starts_with')) {
  428. /**
  429. * Determine if a given string starts with a given substring.
  430. *
  431. * @param string $haystack
  432. * @param string|array $needles
  433. * @return bool
  434. */
  435. function starts_with($haystack, $needles)
  436. {
  437. return Str::startsWith($haystack, $needles);
  438. }
  439. }
  440. if (! function_exists('str_contains')) {
  441. /**
  442. * Determine if a given string contains a given substring.
  443. *
  444. * @param string $haystack
  445. * @param string|array $needles
  446. * @return bool
  447. */
  448. function str_contains($haystack, $needles)
  449. {
  450. return Str::contains($haystack, $needles);
  451. }
  452. }
  453. if (! function_exists('str_finish')) {
  454. /**
  455. * Cap a string with a single instance of a given value.
  456. *
  457. * @param string $value
  458. * @param string $cap
  459. * @return string
  460. */
  461. function str_finish($value, $cap)
  462. {
  463. return Str::finish($value, $cap);
  464. }
  465. }
  466. if (! function_exists('str_is')) {
  467. /**
  468. * Determine if a given string matches a given pattern.
  469. *
  470. * @param string $pattern
  471. * @param string $value
  472. * @return bool
  473. */
  474. function str_is($pattern, $value)
  475. {
  476. return Str::is($pattern, $value);
  477. }
  478. }
  479. if (! function_exists('str_limit')) {
  480. /**
  481. * Limit the number of characters in a string.
  482. *
  483. * @param string $value
  484. * @param int $limit
  485. * @param string $end
  486. * @return string
  487. */
  488. function str_limit($value, $limit = 100, $end = '...')
  489. {
  490. return Str::limit($value, $limit, $end);
  491. }
  492. }
  493. if (! function_exists('str_plural')) {
  494. /**
  495. * Get the plural form of an English word.
  496. *
  497. * @param string $value
  498. * @param int $count
  499. * @return string
  500. */
  501. function str_plural($value, $count = 2)
  502. {
  503. return Str::plural($value, $count);
  504. }
  505. }
  506. if (! function_exists('str_random')) {
  507. /**
  508. * Generate a more truly "random" alpha-numeric string.
  509. *
  510. * @param int $length
  511. * @return string
  512. *
  513. * @throws \RuntimeException
  514. */
  515. function str_random($length = 16)
  516. {
  517. return Str::random($length);
  518. }
  519. }
  520. if (! function_exists('str_replace_array')) {
  521. /**
  522. * Replace a given value in the string sequentially with an array.
  523. *
  524. * @param string $search
  525. * @param array $replace
  526. * @param string $subject
  527. * @return string
  528. */
  529. function str_replace_array($search, array $replace, $subject)
  530. {
  531. foreach ($replace as $value) {
  532. $subject = preg_replace('/'.$search.'/', $value, $subject, 1);
  533. }
  534. return $subject;
  535. }
  536. }
  537. if (! function_exists('str_singular')) {
  538. /**
  539. * Get the singular form of an English word.
  540. *
  541. * @param string $value
  542. * @return string
  543. */
  544. function str_singular($value)
  545. {
  546. return Str::singular($value);
  547. }
  548. }
  549. if (! function_exists('str_slug')) {
  550. /**
  551. * Generate a URL friendly "slug" from a given string.
  552. *
  553. * @param string $title
  554. * @param string $separator
  555. * @return string
  556. */
  557. function str_slug($title, $separator = '-')
  558. {
  559. return Str::slug($title, $separator);
  560. }
  561. }
  562. if (! function_exists('studly_case')) {
  563. /**
  564. * Convert a value to studly caps case.
  565. *
  566. * @param string $value
  567. * @return string
  568. */
  569. function studly_case($value)
  570. {
  571. return Str::studly($value);
  572. }
  573. }
  574. if (! function_exists('title_case')) {
  575. /**
  576. * Convert a value to title case.
  577. *
  578. * @param string $value
  579. * @return string
  580. */
  581. function title_case($value)
  582. {
  583. return Str::title($value);
  584. }
  585. }
  586. if (! function_exists('make')) {
  587. /**
  588. * @param $abstract
  589. * @param array $parameters
  590. * @return mixed
  591. * @throws @\Illuminate\Contracts\Container\BindingResolutionException
  592. */
  593. function make($abstract, array $parameters = [])
  594. {
  595. return app()->make($abstract, $parameters = []);
  596. }
  597. }