PageRenderTime 21ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

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