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

/engine/System/vendor/illuminate/support/Arr.php

https://gitlab.com/leon0399/damnit-engine
PHP | 475 lines | 227 code | 77 blank | 171 comment | 35 complexity | 7d2db6c7daa78b1f1a8ca98c9ed8a195 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Support;
  3. use Illuminate\Support\Traits\Macroable;
  4. class Arr
  5. {
  6. use Macroable;
  7. /**
  8. * Add an element to an array using "dot" notation if it doesn't exist.
  9. *
  10. * @param array $array
  11. * @param string $key
  12. * @param mixed $value
  13. * @return array
  14. */
  15. public static function add($array, $key, $value)
  16. {
  17. if (is_null(static::get($array, $key))) {
  18. static::set($array, $key, $value);
  19. }
  20. return $array;
  21. }
  22. /**
  23. * Build a new array using a callback.
  24. *
  25. * @param array $array
  26. * @param callable $callback
  27. * @return array
  28. *
  29. * @deprecated since version 5.2.
  30. */
  31. public static function build($array, callable $callback)
  32. {
  33. $results = [];
  34. foreach ($array as $key => $value) {
  35. list($innerKey, $innerValue) = call_user_func($callback, $key, $value);
  36. $results[$innerKey] = $innerValue;
  37. }
  38. return $results;
  39. }
  40. /**
  41. * Collapse an array of arrays into a single array.
  42. *
  43. * @param \ArrayAccess|array $array
  44. * @return array
  45. */
  46. public static function collapse($array)
  47. {
  48. $results = [];
  49. foreach ($array as $values) {
  50. if ($values instanceof Collection) {
  51. $values = $values->all();
  52. }
  53. if (! is_array($values)) {
  54. continue;
  55. }
  56. $results = array_merge($results, $values);
  57. }
  58. return $results;
  59. }
  60. /**
  61. * Divide an array into two arrays. One with keys and the other with values.
  62. *
  63. * @param array $array
  64. * @return array
  65. */
  66. public static function divide($array)
  67. {
  68. return [array_keys($array), array_values($array)];
  69. }
  70. /**
  71. * Flatten a multi-dimensional associative array with dots.
  72. *
  73. * @param array $array
  74. * @param string $prepend
  75. * @return array
  76. */
  77. public static function dot($array, $prepend = '')
  78. {
  79. $results = [];
  80. foreach ($array as $key => $value) {
  81. if (is_array($value)) {
  82. $results = array_merge($results, static::dot($value, $prepend.$key.'.'));
  83. } else {
  84. $results[$prepend.$key] = $value;
  85. }
  86. }
  87. return $results;
  88. }
  89. /**
  90. * Get all of the given array except for a specified array of items.
  91. *
  92. * @param array $array
  93. * @param array|string $keys
  94. * @return array
  95. */
  96. public static function except($array, $keys)
  97. {
  98. static::forget($array, $keys);
  99. return $array;
  100. }
  101. /**
  102. * Return the first element in an array passing a given truth test.
  103. *
  104. * @param array $array
  105. * @param callable $callback
  106. * @param mixed $default
  107. * @return mixed
  108. */
  109. public static function first($array, callable $callback, $default = null)
  110. {
  111. foreach ($array as $key => $value) {
  112. if (call_user_func($callback, $key, $value)) {
  113. return $value;
  114. }
  115. }
  116. return value($default);
  117. }
  118. /**
  119. * Return the last element in an array passing a given truth test.
  120. *
  121. * @param array $array
  122. * @param callable $callback
  123. * @param mixed $default
  124. * @return mixed
  125. */
  126. public static function last($array, callable $callback, $default = null)
  127. {
  128. return static::first(array_reverse($array), $callback, $default);
  129. }
  130. /**
  131. * Flatten a multi-dimensional array into a single level.
  132. *
  133. * @param array $array
  134. * @param int $depth
  135. * @return array
  136. */
  137. public static function flatten($array, $depth = INF)
  138. {
  139. return array_reduce($array, function ($result, $item) use ($depth) {
  140. $item = $item instanceof Collection ? $item->all() : $item;
  141. if (is_array($item)) {
  142. if ($depth === 1) {
  143. return array_merge($result, $item);
  144. }
  145. return array_merge($result, static::flatten($item, $depth - 1));
  146. }
  147. $result[] = $item;
  148. return $result;
  149. }, []);
  150. }
  151. /**
  152. * Remove one or many array items from a given array using "dot" notation.
  153. *
  154. * @param array $array
  155. * @param array|string $keys
  156. * @return void
  157. */
  158. public static function forget(&$array, $keys)
  159. {
  160. $original = &$array;
  161. $keys = (array) $keys;
  162. if (count($keys) === 0) {
  163. return;
  164. }
  165. foreach ($keys as $key) {
  166. $parts = explode('.', $key);
  167. // clean up before each pass
  168. $array = &$original;
  169. while (count($parts) > 1) {
  170. $part = array_shift($parts);
  171. if (isset($array[$part]) && is_array($array[$part])) {
  172. $array = &$array[$part];
  173. } else {
  174. continue 2;
  175. }
  176. }
  177. unset($array[array_shift($parts)]);
  178. }
  179. }
  180. /**
  181. * Get an item from an array using "dot" notation.
  182. *
  183. * @param array $array
  184. * @param string $key
  185. * @param mixed $default
  186. * @return mixed
  187. */
  188. public static function get($array, $key, $default = null)
  189. {
  190. if (is_null($key)) {
  191. return $array;
  192. }
  193. if (isset($array[$key])) {
  194. return $array[$key];
  195. }
  196. foreach (explode('.', $key) as $segment) {
  197. if (! is_array($array) || ! array_key_exists($segment, $array)) {
  198. return value($default);
  199. }
  200. $array = $array[$segment];
  201. }
  202. return $array;
  203. }
  204. /**
  205. * Check if an item exists in an array using "dot" notation.
  206. *
  207. * @param array $array
  208. * @param string $key
  209. * @return bool
  210. */
  211. public static function has($array, $key)
  212. {
  213. if (empty($array) || is_null($key)) {
  214. return false;
  215. }
  216. if (array_key_exists($key, $array)) {
  217. return true;
  218. }
  219. foreach (explode('.', $key) as $segment) {
  220. if (! is_array($array) || ! array_key_exists($segment, $array)) {
  221. return false;
  222. }
  223. $array = $array[$segment];
  224. }
  225. return true;
  226. }
  227. /**
  228. * Determines if an array is associative.
  229. *
  230. * An array is "associative" if it doesn't have sequential numerical keys beginning with zero.
  231. *
  232. * @param array $array
  233. * @return bool
  234. */
  235. public static function isAssoc(array $array)
  236. {
  237. $keys = array_keys($array);
  238. return array_keys($keys) !== $keys;
  239. }
  240. /**
  241. * Get a subset of the items from the given array.
  242. *
  243. * @param array $array
  244. * @param array|string $keys
  245. * @return array
  246. */
  247. public static function only($array, $keys)
  248. {
  249. return array_intersect_key($array, array_flip((array) $keys));
  250. }
  251. /**
  252. * Pluck an array of values from an array.
  253. *
  254. * @param array|\ArrayAccess $array
  255. * @param string|array $value
  256. * @param string|array|null $key
  257. * @return array
  258. */
  259. public static function pluck($array, $value, $key = null)
  260. {
  261. $results = [];
  262. list($value, $key) = static::explodePluckParameters($value, $key);
  263. foreach ($array as $item) {
  264. $itemValue = data_get($item, $value);
  265. // If the key is "null", we will just append the value to the array and keep
  266. // looping. Otherwise we will key the array using the value of the key we
  267. // received from the developer. Then we'll return the final array form.
  268. if (is_null($key)) {
  269. $results[] = $itemValue;
  270. } else {
  271. $itemKey = data_get($item, $key);
  272. $results[$itemKey] = $itemValue;
  273. }
  274. }
  275. return $results;
  276. }
  277. /**
  278. * Explode the "value" and "key" arguments passed to "pluck".
  279. *
  280. * @param string|array $value
  281. * @param string|array|null $key
  282. * @return array
  283. */
  284. protected static function explodePluckParameters($value, $key)
  285. {
  286. $value = is_string($value) ? explode('.', $value) : $value;
  287. $key = is_null($key) || is_array($key) ? $key : explode('.', $key);
  288. return [$value, $key];
  289. }
  290. /**
  291. * Push an item onto the beginning of an array.
  292. *
  293. * @param array $array
  294. * @param mixed $value
  295. * @param mixed $key
  296. * @return array
  297. */
  298. public static function prepend($array, $value, $key = null)
  299. {
  300. if (is_null($key)) {
  301. array_unshift($array, $value);
  302. } else {
  303. $array = [$key => $value] + $array;
  304. }
  305. return $array;
  306. }
  307. /**
  308. * Get a value from the array, and remove it.
  309. *
  310. * @param array $array
  311. * @param string $key
  312. * @param mixed $default
  313. * @return mixed
  314. */
  315. public static function pull(&$array, $key, $default = null)
  316. {
  317. $value = static::get($array, $key, $default);
  318. static::forget($array, $key);
  319. return $value;
  320. }
  321. /**
  322. * Set an array item to a given value using "dot" notation.
  323. *
  324. * If no key is given to the method, the entire array will be replaced.
  325. *
  326. * @param array $array
  327. * @param string $key
  328. * @param mixed $value
  329. * @return array
  330. */
  331. public static function set(&$array, $key, $value)
  332. {
  333. if (is_null($key)) {
  334. return $array = $value;
  335. }
  336. $keys = explode('.', $key);
  337. while (count($keys) > 1) {
  338. $key = array_shift($keys);
  339. // If the key doesn't exist at this depth, we will just create an empty array
  340. // to hold the next value, allowing us to create the arrays to hold final
  341. // values at the correct depth. Then we'll keep digging into the array.
  342. if (! isset($array[$key]) || ! is_array($array[$key])) {
  343. $array[$key] = [];
  344. }
  345. $array = &$array[$key];
  346. }
  347. $array[array_shift($keys)] = $value;
  348. return $array;
  349. }
  350. /**
  351. * Sort the array using the given callback.
  352. *
  353. * @param array $array
  354. * @param callable $callback
  355. * @return array
  356. */
  357. public static function sort($array, callable $callback)
  358. {
  359. return Collection::make($array)->sortBy($callback)->all();
  360. }
  361. /**
  362. * Recursively sort an array by keys and values.
  363. *
  364. * @param array $array
  365. * @return array
  366. */
  367. public static function sortRecursive($array)
  368. {
  369. foreach ($array as &$value) {
  370. if (is_array($value)) {
  371. $value = static::sortRecursive($value);
  372. }
  373. }
  374. if (static::isAssoc($array)) {
  375. ksort($array);
  376. } else {
  377. sort($array);
  378. }
  379. return $array;
  380. }
  381. /**
  382. * Filter the array using the given callback.
  383. *
  384. * @param array $array
  385. * @param callable $callback
  386. * @return array
  387. */
  388. public static function where($array, callable $callback)
  389. {
  390. $filtered = [];
  391. foreach ($array as $key => $value) {
  392. if (call_user_func($callback, $key, $value)) {
  393. $filtered[$key] = $value;
  394. }
  395. }
  396. return $filtered;
  397. }
  398. }