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