| 1 |
<?php |
| 2 |
namespace Averta\Core\Utility; |
| 3 |
|
| 4 |
use RecursiveArrayIterator; |
| 5 |
use RecursiveIteratorIterator; |
| 6 |
|
| 7 |
class Arr |
| 8 |
{ |
| 9 |
|
| 10 |
/** |
| 11 |
* Whether keys exist or not |
| 12 |
* |
| 13 |
* @param array $keys An array of keys to search for |
| 14 |
* @param array $array The array to search in |
| 15 |
* |
| 16 |
* @return bool |
| 17 |
*/ |
| 18 |
public static function keysExist( array $keys, array $array ) |
| 19 |
{ |
| 20 |
return ! array_diff_key( array_flip( $keys ), $array ); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* camelCase keys of an array |
| 26 |
* |
| 27 |
* @param array $array The array or object to check |
| 28 |
* @param string $separator The character that should be removed |
| 29 |
* @param array $ignoreKeys The list of keys to ignore |
| 30 |
* @param bool $recursive Whether to loop in indented arrays recursively or not |
| 31 |
* |
| 32 |
* @return mixed |
| 33 |
*/ |
| 34 |
public static function camelizeKeys( $array, $separator = '_', $ignoreKeys = [], $recursive = false ) { |
| 35 |
|
| 36 |
if( is_object( $array ) ){ |
| 37 |
$array = (array) $array; |
| 38 |
} |
| 39 |
|
| 40 |
if( ! is_array( $array ) ){ |
| 41 |
return $array; |
| 42 |
} |
| 43 |
|
| 44 |
$result = []; |
| 45 |
|
| 46 |
foreach( $array as $key => $value ) { |
| 47 |
if( ! in_array( $key, $ignoreKeys ) ){ |
| 48 |
$key = Str::camelize( $key, $separator ); |
| 49 |
} |
| 50 |
|
| 51 |
if( $recursive && is_array( $value ) ){ |
| 52 |
$value = self::camelizeKeys( $value ); |
| 53 |
} |
| 54 |
|
| 55 |
$result[ $key ] = $value; |
| 56 |
} |
| 57 |
|
| 58 |
return $result; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Merges the defined arguments into defaults array. |
| 63 |
* |
| 64 |
* @param array|object $args Value to merge with $defaults. |
| 65 |
* @param array $defaults Array that serves as the defaults. |
| 66 |
* |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
public static function merge( $args, $defaults = [] ) { |
| 70 |
if ( is_object( $args ) ) { |
| 71 |
$args = get_object_vars( $args ); |
| 72 |
} |
| 73 |
|
| 74 |
if ( is_array( $defaults ) && $defaults ) { |
| 75 |
return array_merge( $defaults, $args ); |
| 76 |
} |
| 77 |
|
| 78 |
return $args; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Applies the callback to the elements of the given arrays recursively |
| 83 |
* |
| 84 |
* @param array $array |
| 85 |
* @param $callback |
| 86 |
*/ |
| 87 |
public static function mapDeep( $array, $callback ){ |
| 88 |
if ( ! empty( $array ) ) { |
| 89 |
foreach ( $array as $index => $value ) { |
| 90 |
call_user_func( $callback, $value, $index ); |
| 91 |
if ( is_array( $value ) ) { |
| 92 |
static::mapDeep( $value, $callback ); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Searches the array for a given key and returns the first corresponding value if successful |
| 100 |
* |
| 101 |
* @param $haystack |
| 102 |
* @param $needle |
| 103 |
* |
| 104 |
* @return mixed|null |
| 105 |
*/ |
| 106 |
public static function searchKeyDeep( $haystack, $needle ){ |
| 107 |
$iterator = new RecursiveArrayIterator( $haystack ); |
| 108 |
$recursive = new RecursiveIteratorIterator( |
| 109 |
$iterator, |
| 110 |
RecursiveIteratorIterator::SELF_FIRST |
| 111 |
); |
| 112 |
foreach ( $recursive as $key => $value ) { |
| 113 |
if ( $key === $needle ) { |
| 114 |
return $value; |
| 115 |
} |
| 116 |
} |
| 117 |
return null; |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|