| 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 |
* Merges the defined arguments into defaults array recursively. |
| 83 |
* |
| 84 |
* @param array $array Value to merge with $defaults. |
| 85 |
* @param array $defaults Array that serves as the defaults. |
| 86 |
* |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
public static function mergeDeep(array $array, array $defaults) { |
| 90 |
$merged = $defaults; |
| 91 |
|
| 92 |
foreach ($array as $key => $value) { |
| 93 |
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
| 94 |
$merged[$key] = static::mergeDeep($value, $merged[$key]); |
| 95 |
} else { |
| 96 |
$merged[$key] = $value; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return $merged; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Applies the callback to the elements of the given arrays recursively |
| 105 |
* |
| 106 |
* @param array $array |
| 107 |
* @param $callback |
| 108 |
*/ |
| 109 |
public static function mapDeep( $array, $callback ){ |
| 110 |
if ( ! empty( $array ) ) { |
| 111 |
foreach ( $array as $index => $value ) { |
| 112 |
call_user_func( $callback, $value, $index ); |
| 113 |
if ( is_array( $value ) ) { |
| 114 |
static::mapDeep( $value, $callback ); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Searches the array for a given key and returns the first corresponding value if successful |
| 122 |
* |
| 123 |
* @param $haystack |
| 124 |
* @param $needle |
| 125 |
* |
| 126 |
* @return mixed|null |
| 127 |
*/ |
| 128 |
public static function searchKeyDeep( $haystack, $needle ){ |
| 129 |
$iterator = new RecursiveArrayIterator( $haystack ); |
| 130 |
$recursive = new RecursiveIteratorIterator( |
| 131 |
$iterator, |
| 132 |
RecursiveIteratorIterator::SELF_FIRST |
| 133 |
); |
| 134 |
foreach ( $recursive as $key => $value ) { |
| 135 |
if ( $key === $needle ) { |
| 136 |
return $value; |
| 137 |
} |
| 138 |
} |
| 139 |
return null; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Ensures the value in $args[ $key ] is array |
| 144 |
* |
| 145 |
* @param array $args |
| 146 |
* @param string $key The key of array to be checked |
| 147 |
* @param mixed $default The default value if item was not set in the array. Pass null to skip setting a value. |
| 148 |
* @param string $separator Separator in text-separated string |
| 149 |
* @return void |
| 150 |
*/ |
| 151 |
public static function ensureItemIsArray( &$args, $key, $default = null, $separator = ',' ){ |
| 152 |
if ( isset( $args[ $key ] ) ) { |
| 153 |
if ( !is_array( $args[ $key ] ) ) { |
| 154 |
$args[ $key ] = Str::toArray( $args[ $key ], $separator ); |
| 155 |
} |
| 156 |
} elseif( ! is_null( $default ) ) { |
| 157 |
$args[ $key ] = $default; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
} |
| 162 |
|