Arr.php 11 KB

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