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

https://gitlab.com/techniconline/kmc · PHP · 385 lines · 173 code · 66 blank · 146 comment · 25 complexity · a4d43b604017fcc6174e62358a2d259a MD5 · raw file

  1. <?php namespace Illuminate\Support;
  2. use Illuminate\Support\Traits\Macroable;
  3. class Arr
  4. {
  5. use Macroable;
  6. /**
  7. * Add an element to an array using "dot" notation if it doesn't exist.
  8. *
  9. * @param array $array
  10. * @param string $key
  11. * @param mixed $value
  12. * @return array
  13. */
  14. public static function add($array, $key, $value)
  15. {
  16. if (is_null(static::get($array, $key))) {
  17. static::set($array, $key, $value);
  18. }
  19. return $array;
  20. }
  21. /**
  22. * Build a new array using a callback.
  23. *
  24. * @param array $array
  25. * @param callable $callback
  26. * @return array
  27. */
  28. public static function build($array, callable $callback)
  29. {
  30. $results = [];
  31. foreach ($array as $key => $value) {
  32. list($innerKey, $innerValue) = call_user_func($callback, $key, $value);
  33. $results[$innerKey] = $innerValue;
  34. }
  35. return $results;
  36. }
  37. /**
  38. * Collapse an array of arrays into a single array.
  39. *
  40. * @param array|\ArrayAccess $array
  41. * @return array
  42. */
  43. public static function collapse($array)
  44. {
  45. $results = [];
  46. foreach ($array as $values) {
  47. if ($values instanceof Collection) $values = $values->all();
  48. $results = array_merge($results, $values);
  49. }
  50. return $results;
  51. }
  52. /**
  53. * Divide an array into two arrays. One with keys and the other with values.
  54. *
  55. * @param array $array
  56. * @return array
  57. */
  58. public static function divide($array)
  59. {
  60. return [array_keys($array), array_values($array)];
  61. }
  62. /**
  63. * Flatten a multi-dimensional associative array with dots.
  64. *
  65. * @param array $array
  66. * @param string $prepend
  67. * @return array
  68. */
  69. public static function dot($array, $prepend = '')
  70. {
  71. $results = [];
  72. foreach ($array as $key => $value) {
  73. if (is_array($value)) {
  74. $results = array_merge($results, static::dot($value, $prepend . $key . '.'));
  75. } else {
  76. $results[$prepend . $key] = $value;
  77. }
  78. }
  79. return $results;
  80. }
  81. /**
  82. * Get all of the given array except for a specified array of items.
  83. *
  84. * @param array $array
  85. * @param array|string $keys
  86. * @return array
  87. */
  88. public static function except($array, $keys)
  89. {
  90. static::forget($array, $keys);
  91. return $array;
  92. }
  93. /**
  94. * Fetch a flattened array of a nested array element.
  95. *
  96. * @param array $array
  97. * @param string $key
  98. * @return array
  99. */
  100. public static function fetch($array, $key)
  101. {
  102. foreach (explode('.', $key) as $segment) {
  103. $results = [];
  104. foreach ($array as $value) {
  105. if (array_key_exists($segment, $value = (array)$value)) {
  106. $results[] = $value[$segment];
  107. }
  108. }
  109. $array = array_values($results);
  110. }
  111. return array_values($results);
  112. }
  113. /**
  114. * Return the first element in an array passing a given truth test.
  115. *
  116. * @param array $array
  117. * @param callable $callback
  118. * @param mixed $default
  119. * @return mixed
  120. */
  121. public static function first($array, callable $callback, $default = null)
  122. {
  123. foreach ($array as $key => $value) {
  124. if (call_user_func($callback, $key, $value)) return $value;
  125. }
  126. return value($default);
  127. }
  128. /**
  129. * Return the last element in an array passing a given truth test.
  130. *
  131. * @param array $array
  132. * @param callable $callback
  133. * @param mixed $default
  134. * @return mixed
  135. */
  136. public static function last($array, callable $callback, $default = null)
  137. {
  138. return static::first(array_reverse($array), $callback, $default);
  139. }
  140. /**
  141. * Flatten a multi-dimensional array into a single level.
  142. *
  143. * @param array $array
  144. * @return array
  145. */
  146. public static function flatten($array)
  147. {
  148. $return = [];
  149. array_walk_recursive($array, function ($x) use (&$return) {
  150. $return[] = $x;
  151. });
  152. return $return;
  153. }
  154. /**
  155. * Remove one or many array items from a given array using "dot" notation.
  156. *
  157. * @param array $array
  158. * @param array|string $keys
  159. * @return void
  160. */
  161. public static function forget(&$array, $keys)
  162. {
  163. $original =& $array;
  164. foreach ((array)$keys as $key) {
  165. $parts = explode('.', $key);
  166. while (count($parts) > 1) {
  167. $part = array_shift($parts);
  168. if (isset($array[$part]) && is_array($array[$part])) {
  169. $array =& $array[$part];
  170. }
  171. }
  172. unset($array[array_shift($parts)]);
  173. // clean up after each pass
  174. $array =& $original;
  175. }
  176. }
  177. /**
  178. * Get an item from an array using "dot" notation.
  179. *
  180. * @param array $array
  181. * @param string $key
  182. * @param mixed $default
  183. * @return mixed
  184. */
  185. public static function get($array, $key, $default = null)
  186. {
  187. if (is_null($key)) return $array;
  188. if (isset($array[$key])) return $array[$key];
  189. foreach (explode('.', $key) as $segment) {
  190. if (!is_array($array) || !array_key_exists($segment, $array)) {
  191. return value($default);
  192. }
  193. $array = $array[$segment];
  194. }
  195. return $array;
  196. }
  197. /**
  198. * Check if an item exists in an array using "dot" notation.
  199. *
  200. * @param array $array
  201. * @param string $key
  202. * @return bool
  203. */
  204. public static function has($array, $key)
  205. {
  206. if (empty($array) || is_null($key)) return false;
  207. if (array_key_exists($key, $array)) return true;
  208. foreach (explode('.', $key) as $segment) {
  209. if (!is_array($array) || !array_key_exists($segment, $array)) {
  210. return false;
  211. }
  212. $array = $array[$segment];
  213. }
  214. return true;
  215. }
  216. /**
  217. * Get a subset of the items from the given array.
  218. *
  219. * @param array $array
  220. * @param array|string $keys
  221. * @return array
  222. */
  223. public static function only($array, $keys)
  224. {
  225. return array_intersect_key($array, array_flip((array)$keys));
  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. public static function pluck($array, $value, $key = null)
  236. {
  237. $results = [];
  238. foreach ($array as $item) {
  239. $itemValue = data_get($item, $value);
  240. // If the key is "null", we will just append the value to the array and keep
  241. // looping. Otherwise we will key the array using the value of the key we
  242. // received from the developer. Then we'll return the final array form.
  243. if (is_null($key)) {
  244. $results[] = $itemValue;
  245. } else {
  246. $itemKey = data_get($item, $key);
  247. $results[$itemKey] = $itemValue;
  248. }
  249. }
  250. return $results;
  251. }
  252. /**
  253. * Get a value from the array, and remove it.
  254. *
  255. * @param array $array
  256. * @param string $key
  257. * @param mixed $default
  258. * @return mixed
  259. */
  260. public static function pull(&$array, $key, $default = null)
  261. {
  262. $value = static::get($array, $key, $default);
  263. static::forget($array, $key);
  264. return $value;
  265. }
  266. /**
  267. * Set an array item to a given value using "dot" notation.
  268. *
  269. * If no key is given to the method, the entire array will be replaced.
  270. *
  271. * @param array $array
  272. * @param string $key
  273. * @param mixed $value
  274. * @return array
  275. */
  276. public static function set(&$array, $key, $value)
  277. {
  278. if (is_null($key)) return $array = $value;
  279. $keys = explode('.', $key);
  280. while (count($keys) > 1) {
  281. $key = array_shift($keys);
  282. // If the key doesn't exist at this depth, we will just create an empty array
  283. // to hold the next value, allowing us to create the arrays to hold final
  284. // values at the correct depth. Then we'll keep digging into the array.
  285. if (!isset($array[$key]) || !is_array($array[$key])) {
  286. $array[$key] = [];
  287. }
  288. $array =& $array[$key];
  289. }
  290. $array[array_shift($keys)] = $value;
  291. return $array;
  292. }
  293. /**
  294. * Sort the array using the given callback.
  295. *
  296. * @param array $array
  297. * @param callable $callback
  298. * @return array
  299. */
  300. public static function sort($array, callable $callback)
  301. {
  302. return Collection::make($array)->sortBy($callback)->all();
  303. }
  304. /**
  305. * Filter the array using the given callback.
  306. *
  307. * @param array $array
  308. * @param callable $callback
  309. * @return array
  310. */
  311. public static function where($array, callable $callback)
  312. {
  313. $filtered = [];
  314. foreach ($array as $key => $value) {
  315. if (call_user_func($callback, $key, $value)) $filtered[$key] = $value;
  316. }
  317. return $filtered;
  318. }
  319. }