Arr.php 11 KB

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