helpers.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. <?php
  2. /**
  3. * Laravel Helpers File
  4. * Extracted by Anthony Rappa
  5. * rappa819@gmail.com
  6. */
  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] = array_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. if (is_null(get($array, $key))) {
  38. set($array, $key, $value);
  39. }
  40. return $array;
  41. }
  42. }
  43. if (!function_exists('array_build')) {
  44. /**
  45. * Build a new array using a callback.
  46. *
  47. * @param array $array
  48. * @param \Closure $callback
  49. * @return array
  50. */
  51. function array_build($array, Closure $callback)
  52. {
  53. $results = array();
  54. foreach ($array as $key => $value) {
  55. list($innerKey, $innerValue) = call_user_func($callback, $key, $value);
  56. $results[$innerKey] = $innerValue;
  57. }
  58. return $results;
  59. }
  60. }
  61. if (!function_exists('array_divide')) {
  62. /**
  63. * Divide an array into two arrays. One with keys and the other with values.
  64. *
  65. * @param array $array
  66. * @return array
  67. */
  68. function array_divide($array)
  69. {
  70. return array(array_keys($array), array_values($array));
  71. }
  72. }
  73. if (!function_exists('array_dot')) {
  74. /**
  75. * Flatten a multi-dimensional associative array with dots.
  76. *
  77. * @param array $array
  78. * @param string $prepend
  79. * @return array
  80. */
  81. function array_dot($array, $prepend = '')
  82. {
  83. $results = array();
  84. foreach ($array as $key => $value) {
  85. if (is_array($value)) {
  86. $results = array_merge($results, dot($value, $prepend . $key . '.'));
  87. } else {
  88. $results[$prepend . $key] = $value;
  89. }
  90. }
  91. return $results;
  92. }
  93. }
  94. if (!function_exists('array_except')) {
  95. /**
  96. * Get all of the given array except for a specified array of items.
  97. *
  98. * @param array $array
  99. * @param array|string $keys
  100. * @return array
  101. */
  102. function array_except($array, $keys)
  103. {
  104. return array_diff_key($array, array_flip((array)$keys));
  105. }
  106. }
  107. if (!function_exists('array_fetch')) {
  108. /**
  109. * Fetch a flattened array of a nested array element.
  110. *
  111. * @param array $array
  112. * @param string $key
  113. * @return array
  114. */
  115. function array_fetch($array, $key)
  116. {
  117. $results = array();
  118. foreach (explode('.', $key) as $segment) {
  119. foreach ($array as $value) {
  120. if (array_key_exists($segment, $value = (array)$value)) {
  121. $results[] = $value[$segment];
  122. }
  123. }
  124. $array = array_values($results);
  125. }
  126. return array_values($results);
  127. }
  128. }
  129. if (!function_exists('array_first')) {
  130. /**
  131. * Return the first element in an array passing a given truth test.
  132. *
  133. * @param array $array
  134. * @param \Closure $callback
  135. * @param mixed $default
  136. * @return mixed
  137. */
  138. function array_first($array, $callback, $default = null)
  139. {
  140. foreach ($array as $key => $value) {
  141. if (call_user_func($callback, $key, $value)) {
  142. return $value;
  143. }
  144. }
  145. return value($default);
  146. }
  147. }
  148. if (!function_exists('array_last')) {
  149. /**
  150. * Return the last element in an array passing a given truth test.
  151. *
  152. * @param array $array
  153. * @param \Closure $callback
  154. * @param mixed $default
  155. * @return mixed
  156. */
  157. function array_last($array, $callback, $default = null)
  158. {
  159. return first(array_reverse($array), $callback, $default);
  160. }
  161. }
  162. if (!function_exists('array_flatten')) {
  163. /**
  164. * Flatten a multi-dimensional array into a single level.
  165. *
  166. * @param array $array
  167. * @return array
  168. */
  169. function array_flatten($array)
  170. {
  171. $return = array();
  172. array_walk_recursive($array, function ($x) use (&$return) {
  173. $return[] = $x;
  174. });
  175. return $return;
  176. }
  177. }
  178. if (!function_exists('array_forget')) {
  179. /**
  180. * Remove one or many array items from a given array using "dot" notation.
  181. *
  182. * @param array $array
  183. * @param array|string $keys
  184. * @return void
  185. */
  186. function array_forget(&$array, $keys)
  187. {
  188. $original =& $array;
  189. foreach ((array)$keys as $key) {
  190. $parts = explode('.', $key);
  191. while (count($parts) > 1) {
  192. $part = array_shift($parts);
  193. if (isset($array[$part]) && is_array($array[$part])) {
  194. $array =& $array[$part];
  195. }
  196. }
  197. unset($array[array_shift($parts)]);
  198. // clean up after each pass
  199. $array =& $original;
  200. }
  201. }
  202. }
  203. if (!function_exists('array_get')) {
  204. /**
  205. * Get an item from an array using "dot" notation.
  206. *
  207. * @param array $array
  208. * @param string $key
  209. * @param mixed $default
  210. * @return mixed
  211. */
  212. function array_get($array, $key, $default = null)
  213. {
  214. if (is_null($key)) {
  215. return $array;
  216. }
  217. if (isset($array[$key])) {
  218. return $array[$key];
  219. }
  220. foreach (explode('.', $key) as $segment) {
  221. if (!is_array($array) || !array_key_exists($segment, $array)) {
  222. return value($default);
  223. }
  224. $array = $array[$segment];
  225. }
  226. return $array;
  227. }
  228. }
  229. if (!function_exists('array_has')) {
  230. /**
  231. * Check if an item exists in an array using "dot" notation.
  232. *
  233. * @param array $array
  234. * @param string $key
  235. * @return bool
  236. */
  237. function array_has($array, $key)
  238. {
  239. if (empty($array) || is_null($key)) {
  240. return false;
  241. }
  242. if (array_key_exists($key, $array)) {
  243. return true;
  244. }
  245. foreach (explode('.', $key) as $segment) {
  246. if (!is_array($array) || !array_key_exists($segment, $array)) {
  247. return false;
  248. }
  249. $array = $array[$segment];
  250. }
  251. return true;
  252. }
  253. }
  254. if (!function_exists('array_only')) {
  255. /**
  256. * Get a subset of the items from the given array.
  257. *
  258. * @param array $array
  259. * @param array|string $keys
  260. * @return array
  261. */
  262. function array_only($array, $keys)
  263. {
  264. return array_intersect_key($array, array_flip((array)$keys));
  265. }
  266. }
  267. if (!function_exists('array_pluck')) {
  268. /**
  269. * Pluck an array of values from an array.
  270. *
  271. * @param array $array
  272. * @param string $value
  273. * @param string $key
  274. * @return array
  275. */
  276. function array_pluck($array, $value, $key = null)
  277. {
  278. $results = array();
  279. foreach ($array as $item) {
  280. $itemValue = data_get($item, $value);
  281. // If the key is "null", we will just append the value to the array and keep
  282. // looping. Otherwise we will key the array using the value of the key we
  283. // received from the developer. Then we'll return the final array form.
  284. if (is_null($key)) {
  285. $results[] = $itemValue;
  286. } else {
  287. $itemKey = data_get($item, $key);
  288. $results[$itemKey] = $itemValue;
  289. }
  290. }
  291. return $results;
  292. }
  293. }
  294. if (!function_exists('array_pull')) {
  295. /**
  296. * Get a value from the array, and remove it.
  297. *
  298. * @param array $array
  299. * @param string $key
  300. * @param mixed $default
  301. * @return mixed
  302. */
  303. function array_pull(&$array, $key, $default = null)
  304. {
  305. $value = get($array, $key, $default);
  306. forget($array, $key);
  307. return $value;
  308. }
  309. }
  310. if (!function_exists('array_set')) {
  311. /**
  312. * Set an array item to a given value using "dot" notation.
  313. *
  314. * If no key is given to the method, the entire array will be replaced.
  315. *
  316. * @param array $array
  317. * @param string $key
  318. * @param mixed $value
  319. * @return array
  320. */
  321. function array_set(&$array, $key, $value)
  322. {
  323. if (is_null($key)) {
  324. return $array = $value;
  325. }
  326. $keys = explode('.', $key);
  327. while (count($keys) > 1) {
  328. $key = array_shift($keys);
  329. // If the key doesn't exist at this depth, we will just create an empty array
  330. // to hold the next value, allowing us to create the arrays to hold final
  331. // values at the correct depth. Then we'll keep digging into the array.
  332. if (!isset($array[$key]) || !is_array($array[$key])) {
  333. $array[$key] = array();
  334. }
  335. $array =& $array[$key];
  336. }
  337. $array[array_shift($keys)] = $value;
  338. return $array;
  339. }
  340. }
  341. if (!function_exists('array_where')) {
  342. /**
  343. * Filter the array using the given Closure.
  344. *
  345. * @param array $array
  346. * @param \Closure $callback
  347. * @return array
  348. */
  349. function array_where($array, Closure $callback)
  350. {
  351. $filtered = array();
  352. foreach ($array as $key => $value) {
  353. if (call_user_func($callback, $key, $value)) {
  354. $filtered[$key] = $value;
  355. }
  356. }
  357. return $filtered;
  358. }
  359. }
  360. if (!function_exists('camel_case')) {
  361. /**
  362. * Convert a value to camel case.
  363. *
  364. * @param string $value
  365. * @return string
  366. */
  367. function camel_case($value)
  368. {
  369. static $camelCache = [];
  370. if (isset($camelCache[$value])) {
  371. return $camelCache[$value];
  372. }
  373. return $camelCache[$value] = lcfirst(studly($value));
  374. }
  375. }
  376. if (!function_exists('class_basename')) {
  377. /**
  378. * Get the class "basename" of the given object / class.
  379. *
  380. * @param string|object $class
  381. * @return string
  382. */
  383. function class_basename($class)
  384. {
  385. $class = is_object($class) ? get_class($class) : $class;
  386. return basename(str_replace('\\', '/', $class));
  387. }
  388. }
  389. if (!function_exists('class_uses_recursive')) {
  390. /**
  391. * Returns all traits used by a class, it's subclasses and trait of their traits
  392. *
  393. * @param string $class
  394. * @return array
  395. */
  396. function class_uses_recursive($class)
  397. {
  398. $results = [];
  399. foreach (array_merge([$class => $class], class_parents($class)) as $class) {
  400. $results += trait_uses_recursive($class);
  401. }
  402. return array_unique($results);
  403. }
  404. }
  405. if (!function_exists('data_get')) {
  406. /**
  407. * Get an item from an array or object using "dot" notation.
  408. *
  409. * @param mixed $target
  410. * @param string $key
  411. * @param mixed $default
  412. * @return mixed
  413. */
  414. function data_get($target, $key, $default = null)
  415. {
  416. if (is_null($key)) {
  417. return $target;
  418. }
  419. foreach (explode('.', $key) as $segment) {
  420. if (is_array($target)) {
  421. if (!array_key_exists($segment, $target)) {
  422. return value($default);
  423. }
  424. $target = $target[$segment];
  425. } elseif ($target instanceof ArrayAccess) {
  426. if (!isset($target[$segment])) {
  427. return value($default);
  428. }
  429. $target = $target[$segment];
  430. } elseif (is_object($target)) {
  431. if (!isset($target->{$segment})) {
  432. return value($default);
  433. }
  434. $target = $target->{$segment};
  435. } else {
  436. return value($default);
  437. }
  438. }
  439. return $target;
  440. }
  441. }
  442. if (!function_exists('e')) {
  443. /**
  444. * Escape HTML entities in a string.
  445. *
  446. * @param string $value
  447. * @return string
  448. */
  449. function e($value)
  450. {
  451. return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
  452. }
  453. }
  454. if (!function_exists('ends_with')) {
  455. /**
  456. * Determine if a given string ends with a given substring.
  457. *
  458. * @param string $haystack
  459. * @param string|array $needles
  460. * @return bool
  461. */
  462. function ends_with($haystack, $needles)
  463. {
  464. foreach ((array)$needles as $needle) {
  465. if ((string)$needle === substr($haystack, -strlen($needle))) {
  466. return true;
  467. }
  468. }
  469. return false;
  470. }
  471. }
  472. if (!function_exists('head')) {
  473. /**
  474. * Get the first element of an array. Useful for method chaining.
  475. *
  476. * @param array $array
  477. * @return mixed
  478. */
  479. function head($array)
  480. {
  481. return reset($array);
  482. }
  483. }
  484. if (!function_exists('last')) {
  485. /**
  486. * Get the last element from an array.
  487. *
  488. * @param array $array
  489. * @return mixed
  490. */
  491. function last($array)
  492. {
  493. return end($array);
  494. }
  495. }
  496. if (!function_exists('object_get')) {
  497. /**
  498. * Get an item from an object using "dot" notation.
  499. *
  500. * @param object $object
  501. * @param string $key
  502. * @param mixed $default
  503. * @return mixed
  504. */
  505. function object_get($object, $key, $default = null)
  506. {
  507. if (is_null($key) || trim($key) == '') {
  508. return $object;
  509. }
  510. foreach (explode('.', $key) as $segment) {
  511. if (!is_object($object) || !isset($object->{$segment})) {
  512. return value($default);
  513. }
  514. $object = $object->{$segment};
  515. }
  516. return $object;
  517. }
  518. }
  519. if (!function_exists('preg_replace_sub')) {
  520. /**
  521. * Replace a given pattern with each value in the array in sequentially.
  522. *
  523. * @param string $pattern
  524. * @param array $replacements
  525. * @param string $subject
  526. * @return string
  527. */
  528. function preg_replace_sub($pattern, &$replacements, $subject)
  529. {
  530. return preg_replace_callback($pattern, function () use (&$replacements) {
  531. return array_shift($replacements);
  532. }, $subject);
  533. }
  534. }
  535. if (!function_exists('snake_case')) {
  536. /**
  537. * Convert a string to snake case.
  538. *
  539. * @param string $value
  540. * @param string $delimiter
  541. * @return string
  542. */
  543. function snake_case($value, $delimiter = '_')
  544. {
  545. static $snakeCache = [];
  546. $key = $value . $delimiter;
  547. if (isset($snakeCache[$key])) {
  548. return $snakeCache[$key];
  549. }
  550. if (!ctype_lower($value)) {
  551. $value = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1' . $delimiter, $value));
  552. }
  553. return $snakeCache[$key] = $value;
  554. }
  555. }
  556. if (!function_exists('starts_with')) {
  557. /**
  558. * Determine if a given string starts with a given substring.
  559. *
  560. * @param string $haystack
  561. * @param string|array $needles
  562. * @return bool
  563. */
  564. function starts_with($haystack, $needles)
  565. {
  566. foreach ((array)$needles as $needle) {
  567. if ($needle != '' && strpos($haystack, $needle) === 0) {
  568. return true;
  569. }
  570. }
  571. return false;
  572. }
  573. }
  574. if (!function_exists('str_contains')) {
  575. /**
  576. * Determine if a given string contains a given substring.
  577. *
  578. * @param string $haystack
  579. * @param string|array $needles
  580. * @return bool
  581. */
  582. function str_contains($haystack, $needles)
  583. {
  584. foreach ((array)$needles as $needle) {
  585. if ($needle != '' && strpos($haystack, $needle) !== false) {
  586. return true;
  587. }
  588. }
  589. return false;
  590. }
  591. }
  592. if (!function_exists('str_finish')) {
  593. /**
  594. * Cap a string with a single instance of a given value.
  595. *
  596. * @param string $value
  597. * @param string $cap
  598. * @return string
  599. */
  600. function str_finish($value, $cap)
  601. {
  602. $quoted = preg_quote($cap, '/');
  603. return preg_replace('/(?:' . $quoted . ')+$/', '', $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. if ($pattern == $value) {
  617. return true;
  618. }
  619. $pattern = preg_quote($pattern, '#');
  620. // Asterisks are translated into zero-or-more regular expression wildcards
  621. // to make it convenient to check if the strings starts with the given
  622. // pattern such as "library/*", making any string check convenient.
  623. $pattern = str_replace('\*', '.*', $pattern) . '\z';
  624. return (bool)preg_match('#^' . $pattern . '#', $value);
  625. }
  626. }
  627. if (!function_exists('str_limit')) {
  628. /**
  629. * Limit the number of characters in a string.
  630. *
  631. * @param string $value
  632. * @param int $limit
  633. * @param string $end
  634. * @return string
  635. */
  636. function str_limit($value, $limit = 100, $end = '...')
  637. {
  638. if (mb_strlen($value) <= $limit) {
  639. return $value;
  640. }
  641. return rtrim(mb_substr($value, 0, $limit, 'UTF-8')) . $end;
  642. }
  643. }
  644. if (!function_exists('str_random')) {
  645. /**
  646. * Generate a more truly "random" alpha-numeric string.
  647. *
  648. * @param int $length
  649. * @return string
  650. *
  651. * @throws \RuntimeException
  652. */
  653. function str_random($length = 16)
  654. {
  655. if (!function_exists('openssl_random_pseudo_bytes')) {
  656. throw new RuntimeException('OpenSSL extension is required.');
  657. }
  658. $bytes = openssl_random_pseudo_bytes($length * 2);
  659. if ($bytes === false) {
  660. throw new RuntimeException('Unable to generate random string.');
  661. }
  662. return substr(str_replace(array('/', '+', '='), '', base64_encode($bytes)), 0, $length);
  663. }
  664. }
  665. if (!function_exists('str_replace_array')) {
  666. /**
  667. * Replace a given value in the string sequentially with an array.
  668. *
  669. * @param string $search
  670. * @param array $replace
  671. * @param string $subject
  672. * @return string
  673. */
  674. function str_replace_array($search, array $replace, $subject)
  675. {
  676. foreach ($replace as $value) {
  677. $subject = preg_replace('/' . $search . '/', $value, $subject, 1);
  678. }
  679. return $subject;
  680. }
  681. }
  682. if (!function_exists('str_slug')) {
  683. /**
  684. * Generate a URL friendly "slug" from a given string.
  685. *
  686. * @param string $title
  687. * @param string $separator
  688. * @return string
  689. */
  690. function str_slug($title, $separator = '-')
  691. {
  692. $title = ascii($title);
  693. // Convert all dashes/underscores into separator
  694. $flip = $separator == '-' ? '_' : '-';
  695. $title = preg_replace('![' . preg_quote($flip) . ']+!u', $separator, $title);
  696. // Remove all characters that are not the separator, letters, numbers, or whitespace.
  697. $title = preg_replace('![^' . preg_quote($separator) . '\pL\pN\s]+!u', '', mb_strtolower($title));
  698. // Replace all separator characters and whitespace by a single separator
  699. $title = preg_replace('![' . preg_quote($separator) . '\s]+!u', $separator, $title);
  700. return trim($title, $separator);
  701. }
  702. }
  703. if (!function_exists('ascii')) {
  704. /**
  705. * Transliterate a UTF-8 value to ASCII.
  706. *
  707. * @param string $value
  708. * @return string
  709. */
  710. function ascii($value)
  711. {
  712. foreach (charsArray() as $key => $val) {
  713. $value = str_replace($val, $key, $value);
  714. }
  715. return preg_replace('/[^\x20-\x7E]/u', '', $value);
  716. }
  717. }
  718. if (!function_exists('charsArray')) {
  719. /**
  720. * Returns the replacements for the ascii method.
  721. *
  722. * Note: Adapted from Stringy\Stringy.
  723. *
  724. * @see https://github.com/danielstjules/Stringy/blob/2.3.1/LICENSE.txt
  725. *
  726. * @return array
  727. */
  728. function charsArray()
  729. {
  730. static $charsArray;
  731. if (isset($charsArray)) {
  732. return $charsArray;
  733. }
  734. return $charsArray = [
  735. '0' => ['°', '₀', '۰'],
  736. '1' => ['¹', '₁', '۱'],
  737. '2' => ['²', '₂', '۲'],
  738. '3' => ['³', '₃', '۳'],
  739. '4' => ['⁴', '₄', '۴', '٤'],
  740. '5' => ['⁵', '₅', '۵', '٥'],
  741. '6' => ['⁶', '₆', '۶', '٦'],
  742. '7' => ['⁷', '₇', '۷'],
  743. '8' => ['⁸', '₈', '۸'],
  744. '9' => ['⁹', '₉', '۹'],
  745. 'a' => [
  746. 'à',
  747. 'á',
  748. 'ả',
  749. 'ã',
  750. 'ạ',
  751. 'ă',
  752. 'ắ',
  753. 'ằ',
  754. 'ẳ',
  755. 'ẵ',
  756. 'ặ',
  757. 'â',
  758. 'ấ',
  759. 'ầ',
  760. 'ẩ',
  761. 'ẫ',
  762. 'ậ',
  763. 'ā',
  764. 'ą',
  765. 'å',
  766. 'α',
  767. 'ά',
  768. 'ἀ',
  769. 'ἁ',
  770. 'ἂ',
  771. 'ἃ',
  772. 'ἄ',
  773. 'ἅ',
  774. 'ἆ',
  775. 'ἇ',
  776. 'ᾀ',
  777. 'ᾁ',
  778. 'ᾂ',
  779. 'ᾃ',
  780. 'ᾄ',
  781. 'ᾅ',
  782. 'ᾆ',
  783. 'ᾇ',
  784. 'ὰ',
  785. 'ά',
  786. 'ᾰ',
  787. 'ᾱ',
  788. 'ᾲ',
  789. 'ᾳ',
  790. 'ᾴ',
  791. 'ᾶ',
  792. 'ᾷ',
  793. 'а',
  794. 'أ',
  795. 'အ',
  796. 'ာ',
  797. 'ါ',
  798. 'ǻ',
  799. 'ǎ',
  800. 'ª',
  801. 'ა',
  802. 'अ',
  803. 'ا'
  804. ],
  805. 'b' => ['б', 'β', 'Ъ', 'Ь', 'ب', 'ဗ', 'ბ'],
  806. 'c' => ['ç', 'ć', 'č', 'ĉ', 'ċ'],
  807. 'd' => ['ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д', 'δ', 'د', 'ض', 'ဍ', 'ဒ', 'დ'],
  808. 'e' => [
  809. 'é',
  810. 'è',
  811. 'ẻ',
  812. 'ẽ',
  813. 'ẹ',
  814. 'ê',
  815. 'ế',
  816. 'ề',
  817. 'ể',
  818. 'ễ',
  819. 'ệ',
  820. 'ë',
  821. 'ē',
  822. 'ę',
  823. 'ě',
  824. 'ĕ',
  825. 'ė',
  826. 'ε',
  827. 'έ',
  828. 'ἐ',
  829. 'ἑ',
  830. 'ἒ',
  831. 'ἓ',
  832. 'ἔ',
  833. 'ἕ',
  834. 'ὲ',
  835. 'έ',
  836. 'е',
  837. 'ё',
  838. 'э',
  839. 'є',
  840. 'ə',
  841. 'ဧ',
  842. 'ေ',
  843. 'ဲ',
  844. 'ე',
  845. 'ए',
  846. 'إ',
  847. 'ئ'
  848. ],
  849. 'f' => ['ф', 'φ', 'ف', 'ƒ', 'ფ'],
  850. 'g' => ['ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ဂ', 'გ', 'گ'],
  851. 'h' => ['ĥ', 'ħ', 'η', 'ή', 'ح', 'ه', 'ဟ', 'ှ', 'ჰ'],
  852. 'i' => [
  853. 'í',
  854. 'ì',
  855. 'ỉ',
  856. 'ĩ',
  857. 'ị',
  858. 'î',
  859. 'ï',
  860. 'ī',
  861. 'ĭ',
  862. 'į',
  863. 'ı',
  864. 'ι',
  865. 'ί',
  866. 'ϊ',
  867. 'ΐ',
  868. 'ἰ',
  869. 'ἱ',
  870. 'ἲ',
  871. 'ἳ',
  872. 'ἴ',
  873. 'ἵ',
  874. 'ἶ',
  875. 'ἷ',
  876. 'ὶ',
  877. 'ί',
  878. 'ῐ',
  879. 'ῑ',
  880. 'ῒ',
  881. 'ΐ',
  882. 'ῖ',
  883. 'ῗ',
  884. 'і',
  885. 'ї',
  886. 'и',
  887. 'ဣ',
  888. 'ိ',
  889. 'ီ',
  890. 'ည်',
  891. 'ǐ',
  892. 'ი',
  893. 'इ'
  894. ],
  895. 'j' => ['ĵ', 'ј', 'Ј', 'ჯ', 'ج'],
  896. 'k' => ['ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك', 'က', 'კ', 'ქ', 'ک'],
  897. 'l' => ['ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل', 'လ', 'ლ'],
  898. 'm' => ['м', 'μ', 'م', 'မ', 'მ'],
  899. 'n' => ['ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن', 'န', 'ნ'],
  900. 'o' => [
  901. 'ó',
  902. 'ò',
  903. 'ỏ',
  904. 'õ',
  905. 'ọ',
  906. 'ô',
  907. 'ố',
  908. 'ồ',
  909. 'ổ',
  910. 'ỗ',
  911. 'ộ',
  912. 'ơ',
  913. 'ớ',
  914. 'ờ',
  915. 'ở',
  916. 'ỡ',
  917. 'ợ',
  918. 'ø',
  919. 'ō',
  920. 'ő',
  921. 'ŏ',
  922. 'ο',
  923. 'ὀ',
  924. 'ὁ',
  925. 'ὂ',
  926. 'ὃ',
  927. 'ὄ',
  928. 'ὅ',
  929. 'ὸ',
  930. 'ό',
  931. 'о',
  932. 'و',
  933. 'θ',
  934. 'ို',
  935. 'ǒ',
  936. 'ǿ',
  937. 'º',
  938. 'ო',
  939. 'ओ'
  940. ],
  941. 'p' => ['п', 'π', 'ပ', 'პ', 'پ'],
  942. 'q' => ['ყ'],
  943. 'r' => ['ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر', 'რ'],
  944. 's' => ['ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص', 'စ', 'ſ', 'ს'],
  945. 't' => ['ť', 'ţ', 'т', 'τ', 'ț', 'ت', 'ط', 'ဋ', 'တ', 'ŧ', 'თ', 'ტ'],
  946. 'u' => [
  947. 'ú',
  948. 'ù',
  949. 'ủ',
  950. 'ũ',
  951. 'ụ',
  952. 'ư',
  953. 'ứ',
  954. 'ừ',
  955. 'ử',
  956. 'ữ',
  957. 'ự',
  958. 'û',
  959. 'ū',
  960. 'ů',
  961. 'ű',
  962. 'ŭ',
  963. 'ų',
  964. 'µ',
  965. 'у',
  966. 'ဉ',
  967. 'ု',
  968. 'ူ',
  969. 'ǔ',
  970. 'ǖ',
  971. 'ǘ',
  972. 'ǚ',
  973. 'ǜ',
  974. 'უ',
  975. 'उ'
  976. ],
  977. 'v' => ['в', 'ვ', 'ϐ'],
  978. 'w' => ['ŵ', 'ω', 'ώ', 'ဝ', 'ွ'],
  979. 'x' => ['χ', 'ξ'],
  980. 'y' => ['ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'ÿ', 'ŷ', 'й', 'ы', 'υ', 'ϋ', 'ύ', 'ΰ', 'ي', 'ယ'],
  981. 'z' => ['ź', 'ž', 'ż', 'з', 'ζ', 'ز', 'ဇ', 'ზ'],
  982. 'aa' => ['ع', 'आ', 'آ'],
  983. 'ae' => ['ä', 'æ', 'ǽ'],
  984. 'ai' => ['ऐ'],
  985. 'at' => ['@'],
  986. 'ch' => ['ч', 'ჩ', 'ჭ', 'چ'],
  987. 'dj' => ['ђ', 'đ'],
  988. 'dz' => ['џ', 'ძ'],
  989. 'ei' => ['ऍ'],
  990. 'gh' => ['غ', 'ღ'],
  991. 'ii' => ['ई'],
  992. 'ij' => ['ij'],
  993. 'kh' => ['х', 'خ', 'ხ'],
  994. 'lj' => ['љ'],
  995. 'nj' => ['њ'],
  996. 'oe' => ['ö', 'œ', 'ؤ'],
  997. 'oi' => ['ऑ'],
  998. 'oii' => ['ऒ'],
  999. 'ps' => ['ψ'],
  1000. 'sh' => ['ш', 'შ', 'ش'],
  1001. 'shch' => ['щ'],
  1002. 'ss' => ['ß'],
  1003. 'sx' => ['ŝ'],
  1004. 'th' => ['þ', 'ϑ', 'ث', 'ذ', 'ظ'],
  1005. 'ts' => ['ц', 'ც', 'წ'],
  1006. 'ue' => ['ü'],
  1007. 'uu' => ['ऊ'],
  1008. 'ya' => ['я'],
  1009. 'yu' => ['ю'],
  1010. 'zh' => ['ж', 'ჟ', 'ژ'],
  1011. '(c)' => ['©'],
  1012. 'A' => [
  1013. 'Á',
  1014. 'À',
  1015. 'Ả',
  1016. 'Ã',
  1017. 'Ạ',
  1018. 'Ă',
  1019. 'Ắ',
  1020. 'Ằ',
  1021. 'Ẳ',
  1022. 'Ẵ',
  1023. 'Ặ',
  1024. 'Â',
  1025. 'Ấ',
  1026. 'Ầ',
  1027. 'Ẩ',
  1028. 'Ẫ',
  1029. 'Ậ',
  1030. 'Å',
  1031. 'Ā',
  1032. 'Ą',
  1033. 'Α',
  1034. 'Ά',
  1035. 'Ἀ',
  1036. 'Ἁ',
  1037. 'Ἂ',
  1038. 'Ἃ',
  1039. 'Ἄ',
  1040. 'Ἅ',
  1041. 'Ἆ',
  1042. 'Ἇ',
  1043. 'ᾈ',
  1044. 'ᾉ',
  1045. 'ᾊ',
  1046. 'ᾋ',
  1047. 'ᾌ',
  1048. 'ᾍ',
  1049. 'ᾎ',
  1050. 'ᾏ',
  1051. 'Ᾰ',
  1052. 'Ᾱ',
  1053. 'Ὰ',
  1054. 'Ά',
  1055. 'ᾼ',
  1056. 'А',
  1057. 'Ǻ',
  1058. 'Ǎ'
  1059. ],
  1060. 'B' => ['Б', 'Β', 'ब'],
  1061. 'C' => ['Ç', 'Ć', 'Č', 'Ĉ', 'Ċ'],
  1062. 'D' => ['Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д', 'Δ'],
  1063. 'E' => [
  1064. 'É',
  1065. 'È',
  1066. 'Ẻ',
  1067. 'Ẽ',
  1068. 'Ẹ',
  1069. 'Ê',
  1070. 'Ế',
  1071. 'Ề',
  1072. 'Ể',
  1073. 'Ễ',
  1074. 'Ệ',
  1075. 'Ë',
  1076. 'Ē',
  1077. 'Ę',
  1078. 'Ě',
  1079. 'Ĕ',
  1080. 'Ė',
  1081. 'Ε',
  1082. 'Έ',
  1083. 'Ἐ',
  1084. 'Ἑ',
  1085. 'Ἒ',
  1086. 'Ἓ',
  1087. 'Ἔ',
  1088. 'Ἕ',
  1089. 'Έ',
  1090. 'Ὲ',
  1091. 'Е',
  1092. 'Ё',
  1093. 'Э',
  1094. 'Є',
  1095. 'Ə'
  1096. ],
  1097. 'F' => ['Ф', 'Φ'],
  1098. 'G' => ['Ğ', 'Ġ', 'Ģ', 'Г', 'Ґ', 'Γ'],
  1099. 'H' => ['Η', 'Ή', 'Ħ'],
  1100. 'I' => [
  1101. 'Í',
  1102. 'Ì',
  1103. 'Ỉ',
  1104. 'Ĩ',
  1105. 'Ị',
  1106. 'Î',
  1107. 'Ï',
  1108. 'Ī',
  1109. 'Ĭ',
  1110. 'Į',
  1111. 'İ',
  1112. 'Ι',
  1113. 'Ί',
  1114. 'Ϊ',
  1115. 'Ἰ',
  1116. 'Ἱ',
  1117. 'Ἳ',
  1118. 'Ἴ',
  1119. 'Ἵ',
  1120. 'Ἶ',
  1121. 'Ἷ',
  1122. 'Ῐ',
  1123. 'Ῑ',
  1124. 'Ὶ',
  1125. 'Ί',
  1126. 'И',
  1127. 'І',
  1128. 'Ї',
  1129. 'Ǐ',
  1130. 'ϒ'
  1131. ],
  1132. 'K' => ['К', 'Κ'],
  1133. 'L' => ['Ĺ', 'Ł', 'Л', 'Λ', 'Ļ', 'Ľ', 'Ŀ', 'ल'],
  1134. 'M' => ['М', 'Μ'],
  1135. 'N' => ['Ń', 'Ñ', 'Ň', 'Ņ', 'Ŋ', 'Н', 'Ν'],
  1136. 'O' => [
  1137. 'Ó',
  1138. 'Ò',
  1139. 'Ỏ',
  1140. 'Õ',
  1141. 'Ọ',
  1142. 'Ô',
  1143. 'Ố',
  1144. 'Ồ',
  1145. 'Ổ',
  1146. 'Ỗ',
  1147. 'Ộ',
  1148. 'Ơ',
  1149. 'Ớ',
  1150. 'Ờ',
  1151. 'Ở',
  1152. 'Ỡ',
  1153. 'Ợ',
  1154. 'Ø',
  1155. 'Ō',
  1156. 'Ő',
  1157. 'Ŏ',
  1158. 'Ο',
  1159. 'Ό',
  1160. 'Ὀ',
  1161. 'Ὁ',
  1162. 'Ὂ',
  1163. 'Ὃ',
  1164. 'Ὄ',
  1165. 'Ὅ',
  1166. 'Ὸ',
  1167. 'Ό',
  1168. 'О',
  1169. 'Θ',
  1170. 'Ө',
  1171. 'Ǒ',
  1172. 'Ǿ'
  1173. ],
  1174. 'P' => ['П', 'Π'],
  1175. 'R' => ['Ř', 'Ŕ', 'Р', 'Ρ', 'Ŗ'],
  1176. 'S' => ['Ş', 'Ŝ', 'Ș', 'Š', 'Ś', 'С', 'Σ'],
  1177. 'T' => ['Ť', 'Ţ', 'Ŧ', 'Ț', 'Т', 'Τ'],
  1178. 'U' => [
  1179. 'Ú',
  1180. 'Ù',
  1181. 'Ủ',
  1182. 'Ũ',
  1183. 'Ụ',
  1184. 'Ư',
  1185. 'Ứ',
  1186. 'Ừ',
  1187. 'Ử',
  1188. 'Ữ',
  1189. 'Ự',
  1190. 'Û',
  1191. 'Ū',
  1192. 'Ů',
  1193. 'Ű',
  1194. 'Ŭ',
  1195. 'Ų',
  1196. 'У',
  1197. 'Ǔ',
  1198. 'Ǖ',
  1199. 'Ǘ',
  1200. 'Ǚ',
  1201. 'Ǜ'
  1202. ],
  1203. 'V' => ['В'],
  1204. 'W' => ['Ω', 'Ώ', 'Ŵ'],
  1205. 'X' => ['Χ', 'Ξ'],
  1206. 'Y' => ['Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ', 'Ÿ', 'Ῠ', 'Ῡ', 'Ὺ', 'Ύ', 'Ы', 'Й', 'Υ', 'Ϋ', 'Ŷ'],
  1207. 'Z' => ['Ź', 'Ž', 'Ż', 'З', 'Ζ'],
  1208. 'AE' => ['Ä', 'Æ', 'Ǽ'],
  1209. 'CH' => ['Ч'],
  1210. 'DJ' => ['Ђ'],
  1211. 'DZ' => ['Џ'],
  1212. 'GX' => ['Ĝ'],
  1213. 'HX' => ['Ĥ'],
  1214. 'IJ' => ['IJ'],
  1215. 'JX' => ['Ĵ'],
  1216. 'KH' => ['Х'],
  1217. 'LJ' => ['Љ'],
  1218. 'NJ' => ['Њ'],
  1219. 'OE' => ['Ö', 'Œ'],
  1220. 'PS' => ['Ψ'],
  1221. 'SH' => ['Ш'],
  1222. 'SHCH' => ['Щ'],
  1223. 'SS' => ['ẞ'],
  1224. 'TH' => ['Þ'],
  1225. 'TS' => ['Ц'],
  1226. 'UE' => ['Ü'],
  1227. 'YA' => ['Я'],
  1228. 'YU' => ['Ю'],
  1229. 'ZH' => ['Ж'],
  1230. ' ' => [
  1231. "\xC2\xA0",
  1232. "\xE2\x80\x80",
  1233. "\xE2\x80\x81",
  1234. "\xE2\x80\x82",
  1235. "\xE2\x80\x83",
  1236. "\xE2\x80\x84",
  1237. "\xE2\x80\x85",
  1238. "\xE2\x80\x86",
  1239. "\xE2\x80\x87",
  1240. "\xE2\x80\x88",
  1241. "\xE2\x80\x89",
  1242. "\xE2\x80\x8A",
  1243. "\xE2\x80\xAF",
  1244. "\xE2\x81\x9F",
  1245. "\xE3\x80\x80"
  1246. ],
  1247. ];
  1248. }
  1249. }
  1250. if (!function_exists('studly_case')) {
  1251. /**
  1252. * Convert a value to studly caps case.
  1253. *
  1254. * @param string $value
  1255. * @return string
  1256. */
  1257. function studly_case($value)
  1258. {
  1259. static $studlyCache = [];
  1260. $key = $value;
  1261. if (isset($studlyCache[$key])) {
  1262. return $studlyCache[$key];
  1263. }
  1264. $value = ucwords(str_replace(array('-', '_'), ' ', $value));
  1265. return $studlyCache[$key] = str_replace(' ', '', $value);
  1266. }
  1267. }
  1268. if (!function_exists('title_case')) {
  1269. /**
  1270. * Convert a value to title case.
  1271. *
  1272. * @param string $value
  1273. * @return string
  1274. */
  1275. function title_case($value)
  1276. {
  1277. return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
  1278. }
  1279. }
  1280. if (!function_exists('trait_uses_recursive')) {
  1281. /**
  1282. * Returns all traits used by a trait and its traits
  1283. *
  1284. * @param string $trait
  1285. * @return array
  1286. */
  1287. function trait_uses_recursive($trait)
  1288. {
  1289. $traits = class_uses($trait);
  1290. foreach ($traits as $trait) {
  1291. $traits += trait_uses_recursive($trait);
  1292. }
  1293. return $traits;
  1294. }
  1295. }
  1296. if (!function_exists('value')) {
  1297. /**
  1298. * Return the default value of the given value.
  1299. *
  1300. * @param mixed $value
  1301. * @return mixed
  1302. */
  1303. function value($value)
  1304. {
  1305. return $value instanceof Closure ? $value() : $value;
  1306. }
  1307. }
  1308. if (!function_exists('with')) {
  1309. /**
  1310. * Return the given object. Useful for chaining.
  1311. *
  1312. * @param mixed $object
  1313. * @return mixed
  1314. */
  1315. function with($object)
  1316. {
  1317. return $object;
  1318. }
  1319. }
  1320. /**
  1321. * Helper functions for the helper functions, that can still be used standalone
  1322. */
  1323. if (!function_exists('studly')) {
  1324. /**
  1325. * Convert a value to studly caps case.
  1326. *
  1327. * @param string $value
  1328. * @return string
  1329. */
  1330. function studly($value)
  1331. {
  1332. static $studlyCache = [];
  1333. $key = $value;
  1334. if (isset($studlyCache[$key])) {
  1335. return $studlyCache[$key];
  1336. }
  1337. $value = ucwords(str_replace(array('-', '_'), ' ', $value));
  1338. return $studlyCache[$key] = str_replace(' ', '', $value);
  1339. }
  1340. }
  1341. if (!function_exists('get')) {
  1342. /**
  1343. * Get an item from an array using "dot" notation.
  1344. *
  1345. * @param array $array
  1346. * @param string $key
  1347. * @param mixed $default
  1348. * @return mixed
  1349. */
  1350. function get($array, $key, $default = null)
  1351. {
  1352. if (is_null($key)) {
  1353. return $array;
  1354. }
  1355. if (isset($array[$key])) {
  1356. return $array[$key];
  1357. }
  1358. foreach (explode('.', $key) as $segment) {
  1359. if (!is_array($array) || !array_key_exists($segment, $array)) {
  1360. return value($default);
  1361. }
  1362. $array = $array[$segment];
  1363. }
  1364. return $array;
  1365. }
  1366. }
  1367. if (!function_exists('set')) {
  1368. /**
  1369. * Set an array item to a given value using "dot" notation.
  1370. *
  1371. * If no key is given to the method, the entire array will be replaced.
  1372. *
  1373. * @param array $array
  1374. * @param string $key
  1375. * @param mixed $value
  1376. * @return array
  1377. */
  1378. function set(&$array, $key, $value)
  1379. {
  1380. if (is_null($key)) {
  1381. return $array = $value;
  1382. }
  1383. $keys = explode('.', $key);
  1384. while (count($keys) > 1) {
  1385. $key = array_shift($keys);
  1386. // If the key doesn't exist at this depth, we will just create an empty array
  1387. // to hold the next value, allowing us to create the arrays to hold final
  1388. // values at the correct depth. Then we'll keep digging into the array.
  1389. if (!isset($array[$key]) || !is_array($array[$key])) {
  1390. $array[$key] = array();
  1391. }
  1392. $array =& $array[$key];
  1393. }
  1394. $array[array_shift($keys)] = $value;
  1395. return $array;
  1396. }
  1397. }
  1398. if (!function_exists('dot')) {
  1399. /**
  1400. * Flatten a multi-dimensional associative array with dots.
  1401. *
  1402. * @param array $array
  1403. * @param string $prepend
  1404. * @return array
  1405. */
  1406. function dot($array, $prepend = '')
  1407. {
  1408. $results = array();
  1409. foreach ($array as $key => $value) {
  1410. if (is_array($value)) {
  1411. $results = array_merge($results, dot($value, $prepend . $key . '.'));
  1412. } else {
  1413. $results[$prepend . $key] = $value;
  1414. }
  1415. }
  1416. return $results;
  1417. }
  1418. }
  1419. if (!function_exists('first')) {
  1420. /**
  1421. * Return the first element in an array passing a given truth test.
  1422. *
  1423. * @param array $array
  1424. * @param \Closure $callback
  1425. * @param mixed $default
  1426. * @return mixed
  1427. */
  1428. function first($array, $callback, $default = null)
  1429. {
  1430. foreach ($array as $key => $value) {
  1431. if (call_user_func($callback, $key, $value)) {
  1432. return $value;
  1433. }
  1434. }
  1435. return value($default);
  1436. }
  1437. }
  1438. if (!function_exists('forget')) {
  1439. /**
  1440. * Remove one or many array items from a given array using "dot" notation.
  1441. *
  1442. * @param array $array
  1443. * @param array|string $keys
  1444. * @return void
  1445. */
  1446. function forget(&$array, $keys)
  1447. {
  1448. $original =& $array;
  1449. foreach ((array)$keys as $key) {
  1450. $parts = explode('.', $key);
  1451. while (count($parts) > 1) {
  1452. $part = array_shift($parts);
  1453. if (isset($array[$part]) && is_array($array[$part])) {
  1454. $array =& $array[$part];
  1455. }
  1456. }
  1457. unset($array[array_shift($parts)]);
  1458. // clean up after each pass
  1459. $array =& $original;
  1460. }
  1461. }
  1462. }
  1463. if (!function_exists('bcrypt')) {
  1464. /**
  1465. * Password hash the given value.
  1466. *
  1467. * @param string $value
  1468. * @param array $options
  1469. * @return string
  1470. *
  1471. * @throws \RuntimeException
  1472. */
  1473. function bcrypt($value, $options = [])
  1474. {
  1475. $cost = isset($options['rounds']) ? $options['rounds'] : 10;
  1476. $hashedValue = password_hash($value, PASSWORD_BCRYPT, ['cost' => $cost]);
  1477. if ($hashedValue === false) {
  1478. throw new RuntimeException('Bcrypt hashing not supported.');
  1479. }
  1480. return $hashedValue;
  1481. }
  1482. }
  1483. if (!function_exists('tap')) {
  1484. /**
  1485. * Call the given Closure with the given value then return the value.
  1486. *
  1487. * @param mixed $value
  1488. * @param callable $callback
  1489. * @return mixed
  1490. */
  1491. function tap($value, $callback)
  1492. {
  1493. $callback($value);
  1494. return $value;
  1495. }
  1496. }
  1497. if (!function_exists('dd')) {
  1498. /**
  1499. * Dump the passed variables and end the script.
  1500. *
  1501. * @param mixed
  1502. * @return void
  1503. */
  1504. function dd()
  1505. {
  1506. array_map(function ($x) {
  1507. var_dump($x);
  1508. }, func_get_args());
  1509. die(1);
  1510. }
  1511. }
  1512. if (!function_exists('data_fill')) {
  1513. /**
  1514. * Fill in data where it's missing.
  1515. *
  1516. * @param mixed $target
  1517. * @param string|array $key
  1518. * @param mixed $value
  1519. * @return mixed
  1520. */
  1521. function data_fill(&$target, $key, $value)
  1522. {
  1523. return data_set($target, $key, $value, false);
  1524. }
  1525. }
  1526. if (! function_exists('data_set')) {
  1527. /**
  1528. * Set an item on an array or object using dot notation.
  1529. *
  1530. * @param mixed $target
  1531. * @param string|array $key
  1532. * @param mixed $value
  1533. * @param bool $overwrite
  1534. * @return mixed
  1535. */
  1536. function data_set(&$target, $key, $value, $overwrite = true)
  1537. {
  1538. $segments = is_array($key) ? $key : explode('.', $key);
  1539. if (($segment = array_shift($segments)) === '*') {
  1540. if (! accessible($target)) {
  1541. $target = [];
  1542. }
  1543. if ($segments) {
  1544. foreach ($target as &$inner) {
  1545. data_set($inner, $segments, $value, $overwrite);
  1546. }
  1547. } elseif ($overwrite) {
  1548. foreach ($target as &$inner) {
  1549. $inner = $value;
  1550. }
  1551. }
  1552. } elseif (accessible($target)) {
  1553. if ($segments) {
  1554. if (! exists($target, $segment)) {
  1555. $target[$segment] = [];
  1556. }
  1557. data_set($target[$segment], $segments, $value, $overwrite);
  1558. } elseif ($overwrite || ! exists($target, $segment)) {
  1559. $target[$segment] = $value;
  1560. }
  1561. } elseif (is_object($target)) {
  1562. if ($segments) {
  1563. if (! isset($target->{$segment})) {
  1564. $target->{$segment} = [];
  1565. }
  1566. data_set($target->{$segment}, $segments, $value, $overwrite);
  1567. } elseif ($overwrite || ! isset($target->{$segment})) {
  1568. $target->{$segment} = $value;
  1569. }
  1570. } else {
  1571. $target = [];
  1572. if ($segments) {
  1573. data_set($target[$segment], $segments, $value, $overwrite);
  1574. } elseif ($overwrite) {
  1575. $target[$segment] = $value;
  1576. }
  1577. }
  1578. return $target;
  1579. }
  1580. /**
  1581. * Determine whether the given value is array accessible.
  1582. *
  1583. * @param mixed $value
  1584. * @return bool
  1585. */
  1586. function accessible($value)
  1587. {
  1588. return is_array($value) || $value instanceof ArrayAccess;
  1589. }
  1590. /**
  1591. * Determine if the given key exists in the provided array.
  1592. *
  1593. * @param \ArrayAccess|array $array
  1594. * @param string|int $key
  1595. * @return bool
  1596. */
  1597. function exists($array, $key)
  1598. {
  1599. if ($array instanceof ArrayAccess) {
  1600. return $array->offsetExists($key);
  1601. }
  1602. return array_key_exists($key, $array);
  1603. }
  1604. }