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

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

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