PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Streaming-Safe-for-Kids/vendor/laravel/framework/src/Illuminate/Support/Arr.php

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