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