Background
3 years ago
Blocks
3 years ago
GlobalElements
4 years ago
Layout
4 years ago
License
4 years ago
Separators
4 years ago
StyleManager
3 years ago
Styles
4 years ago
Activation.php
3 years ago
Backup.php
4 years ago
CustomizerImporter.php
3 years ago
Deactivation.php
4 years ago
EditInKubioCustomizerPanel.php
4 years ago
Element.php
4 years ago
ElementBase.php
4 years ago
Importer.php
3 years ago
InnerBlocks.php
4 years ago
LodashBasic.php
4 years ago
Registry.php
4 years ago
Utils.php
3 years ago
LodashBasic.php
295 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core; |
| 4 | |
| 5 | use _; |
| 6 | use IlluminateAgnostic\Arr\Support\Arr; |
| 7 | use IlluminateAgnostic\Str\Support\Str; |
| 8 | use function array_map; |
| 9 | use function array_merge; |
| 10 | use function count; |
| 11 | use function is_array; |
| 12 | |
| 13 | function array_get_value( &$array, $parents, $default = null, $glue = '.' ) { |
| 14 | if ( ! $array || ! is_array( $array ) ) { |
| 15 | return $default; |
| 16 | } |
| 17 | |
| 18 | if ( ! is_array( $parents ) ) { |
| 19 | $parents = explode( $glue, $parents ); |
| 20 | } |
| 21 | |
| 22 | $ref = &$array; |
| 23 | |
| 24 | foreach ( (array) $parents as $parent ) { |
| 25 | if ( is_array( $ref ) && array_key_exists( $parent, $ref ) ) { |
| 26 | $ref = &$ref[ $parent ]; |
| 27 | // walk inside object |
| 28 | } elseif ( is_object( $ref ) && property_exists( $ref, $parent ) ) { |
| 29 | $ref = &$ref->$parent; |
| 30 | } else { |
| 31 | return $default; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | return $ref; |
| 36 | } |
| 37 | |
| 38 | function array_set_value( array &$array, $parents, $value, $glue = '.' ) { |
| 39 | if ( ! is_array( $parents ) ) { |
| 40 | $parents = explode( $glue, (string) $parents ); |
| 41 | } |
| 42 | |
| 43 | $ref = &$array; |
| 44 | |
| 45 | foreach ( $parents as $parent ) { |
| 46 | if ( isset( $ref ) && ! is_array( $ref ) ) { |
| 47 | $ref = array(); |
| 48 | } |
| 49 | |
| 50 | $ref = &$ref[ $parent ]; |
| 51 | } |
| 52 | |
| 53 | $ref = $value; |
| 54 | } |
| 55 | |
| 56 | function array_unset_value( &$array, $parents, $glue = '.' ) { |
| 57 | |
| 58 | if ( ! is_array( $array ) ) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | if ( ! is_array( $parents ) ) { |
| 63 | $parents = explode( $glue, $parents ); |
| 64 | } |
| 65 | |
| 66 | $key = array_shift( $parents ); |
| 67 | |
| 68 | if ( empty( $parents ) ) { |
| 69 | unset( $array[ $key ] ); |
| 70 | } else { |
| 71 | array_unset_value( $array[ $key ], $parents ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | function array_map_by_key( $array, $key ) { |
| 76 | $result = array(); |
| 77 | array_walk( |
| 78 | $array, |
| 79 | function ( $partial ) use ( $result, $key ) { |
| 80 | $id = array_get_value( $partial, $key, null ); |
| 81 | if ( $id !== null ) { |
| 82 | $result[ $id ] = $partial; |
| 83 | } |
| 84 | } |
| 85 | ); |
| 86 | |
| 87 | return $result; |
| 88 | } |
| 89 | |
| 90 | class LodashBasic { |
| 91 | static function array_get_value( &$array, $parents, $default = null, $glue = '.' ) { |
| 92 | return array_get_value( $array, $parents, $default, $glue ); |
| 93 | } |
| 94 | |
| 95 | static function has( $array, $path ) { |
| 96 | return Arr::has( $array, $path ); |
| 97 | } |
| 98 | |
| 99 | static function set( array &$array, $parents, $value ) { |
| 100 | array_set_value( $array, $parents, $value ); |
| 101 | } |
| 102 | |
| 103 | static function unsetValue( &$array, $parents ) { |
| 104 | array_unset_value( $array, $parents ); |
| 105 | } |
| 106 | |
| 107 | static function each( $collection, $iterateFn ) { |
| 108 | _\each( $collection, $iterateFn ); |
| 109 | } |
| 110 | |
| 111 | static function keyBy( $collection, $iteratee ) { |
| 112 | return _\keyBy( $collection, $iteratee ); |
| 113 | } |
| 114 | |
| 115 | static function map( $collection, $iteratee ) { |
| 116 | return _\map( $collection, $iteratee ); |
| 117 | } |
| 118 | |
| 119 | static function mapValues( $array, $mapper ) { |
| 120 | $closure = $mapper; |
| 121 | if ( is_string( $mapper ) ) { |
| 122 | $closure = function ( $value ) use ( $mapper ) { |
| 123 | return LodashBasic::get( $value, $mapper ); |
| 124 | }; |
| 125 | } |
| 126 | |
| 127 | return array_map( $closure, $array ); |
| 128 | } |
| 129 | |
| 130 | static function get( $array, $parents, $default = null ) { |
| 131 | if ( $array ) { |
| 132 | return array_get_value( $array, $parents, $default ); |
| 133 | } |
| 134 | |
| 135 | return $default; |
| 136 | } |
| 137 | |
| 138 | static function find( $array, $closure ) { |
| 139 | return _\find( $array, $closure ); |
| 140 | } |
| 141 | |
| 142 | static function filter( $array, $closure ) { |
| 143 | return _\filter( $array, $closure ); |
| 144 | } |
| 145 | |
| 146 | static function compactWithExceptions( $array, $exceptions = array() ) { |
| 147 | return \array_values( |
| 148 | \array_filter( |
| 149 | $array, |
| 150 | function ( $input ) use ( $exceptions ) { |
| 151 | $isException = in_array( $input, $exceptions, true ); |
| 152 | |
| 153 | return ! ! $input || $isException; |
| 154 | } |
| 155 | ) |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | static function isString( $value ) { |
| 160 | return is_string( $value ); |
| 161 | } |
| 162 | |
| 163 | static function concat( $array, ...$values ) { |
| 164 | $check = function ( $value ) { |
| 165 | return is_array( $value ) ? $value : array( $value ); |
| 166 | }; |
| 167 | |
| 168 | return array_merge( $check( $array ), ...array_map( $check, $values ) ); |
| 169 | } |
| 170 | |
| 171 | static function merge( ...$values ) { |
| 172 | $not_null_values = LodashBasic::compact( $values ); |
| 173 | if ( count( $not_null_values ) > 0 ) { |
| 174 | return array_replace_recursive( ...$not_null_values ); |
| 175 | } |
| 176 | |
| 177 | return array(); |
| 178 | } |
| 179 | |
| 180 | static function compact( $array ) { |
| 181 | return _\compact( $array ); |
| 182 | } |
| 183 | |
| 184 | static function mergeSkipSeqArray( ...$values ) { |
| 185 | $not_null_values = LodashBasic::compact( $values ); |
| 186 | if ( count( $not_null_values ) > 0 ) { |
| 187 | if ( count( $not_null_values ) === 1 ) { |
| 188 | return $not_null_values[0]; |
| 189 | } else { |
| 190 | |
| 191 | // get the first 2 arrays to merge from parameters |
| 192 | $next_arr = array_shift( $not_null_values ); |
| 193 | $second_arr = array_shift( $not_null_values ); |
| 194 | |
| 195 | // if arrays are not assoc use the second array |
| 196 | if ( |
| 197 | is_array( $next_arr ) && count( $next_arr ) && ! Arr::isAssoc( $second_arr ) && |
| 198 | is_array( $second_arr ) && count( $second_arr ) && ! Arr::isAssoc( $next_arr ) |
| 199 | ) { |
| 200 | return $second_arr; |
| 201 | } |
| 202 | |
| 203 | foreach ( $second_arr as $key => $second_value ) { |
| 204 | |
| 205 | $first_value = Arr::get( $next_arr, $key, null ); |
| 206 | if ( is_array( $second_value ) && count( $second_value ) ) { |
| 207 | if ( ! is_array( $first_value ) ) { |
| 208 | $next_arr[ $key ] = $second_value; |
| 209 | } else { |
| 210 | $next_arr[ $key ] = LodashBasic::mergeSkipSeqArray( $first_value, $second_value ); |
| 211 | } |
| 212 | } else { |
| 213 | if (is_array($first_value) && is_array($second_value) && empty($second_value)){ |
| 214 | $next_arr[$key] = $first_value; |
| 215 | } else { |
| 216 | $next_arr[ $key ] = $second_value; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return $next_arr; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return array(); |
| 226 | } |
| 227 | |
| 228 | static function omit( $object, $property ) { |
| 229 | return Arr::except( $object, $property ); |
| 230 | } |
| 231 | |
| 232 | static function pick( $array, $paths ) { |
| 233 | $paths_by_name = array_fill_keys( (array) $paths, true ); |
| 234 | |
| 235 | return array_filter( |
| 236 | $array, |
| 237 | function ( $key ) use ( $paths_by_name ) { |
| 238 | return isset( $paths_by_name[ $key ] ); |
| 239 | }, |
| 240 | ARRAY_FILTER_USE_KEY |
| 241 | ); |
| 242 | } |
| 243 | |
| 244 | static function kebabCase( $string ) { |
| 245 | return Str::kebab( $string ); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * This method returns the first argument it receives. |
| 250 | * |
| 251 | * @param mixed $value Any value. |
| 252 | * |
| 253 | * @return mixed Returns `value`. |
| 254 | * @category Util |
| 255 | * |
| 256 | * @example |
| 257 | * <code> |
| 258 | * $object = ['a' => 1]; |
| 259 | * |
| 260 | * identity($object) === $object; |
| 261 | * // => true |
| 262 | * </code> |
| 263 | */ |
| 264 | static function identity( $value ) { |
| 265 | return _\identity( $value ); |
| 266 | } |
| 267 | |
| 268 | static function uniq( $values ) { |
| 269 | return array_unique( $values ); |
| 270 | } |
| 271 | |
| 272 | static function diff( $a1, $a2 ) { |
| 273 | $r = array(); |
| 274 | foreach ( $a1 as $k => $v ) { |
| 275 | if ( array_key_exists( $k, (array)$a2 ) ) { |
| 276 | if ( is_array( $v ) ) { |
| 277 | $rad = self::diff( $v, $a2[ $k ] ); |
| 278 | if ( count( $rad ) ) { |
| 279 | $r[ $k ] = $rad; |
| 280 | } |
| 281 | } else { |
| 282 | if ( $v != $a2[ $k ] ) { |
| 283 | $r[ $k ] = $v; |
| 284 | } |
| 285 | } |
| 286 | } else { |
| 287 | $r[ $k ] = $v; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | return $r; |
| 292 | } |
| 293 | |
| 294 | } |
| 295 |