PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 1.2.3
Kubio AI Page Builder v1.2.3
2.8.5 2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / src / Core / LodashBasic.php
kubio / lib / src / Core Last commit date
Background 4 years ago Blocks 4 years ago GlobalElements 4 years ago Layout 4 years ago License 4 years ago Separators 4 years ago StyleManager 4 years ago Styles 4 years ago Activation.php 4 years ago Backup.php 4 years ago CustomizerImporter.php 4 years ago Deactivation.php 4 years ago EditInKubioCustomizerPanel.php 4 years ago Element.php 4 years ago ElementBase.php 4 years ago Importer.php 4 years ago InnerBlocks.php 4 years ago LodashBasic.php 4 years ago Registry.php 4 years ago Utils.php 4 years ago
LodashBasic.php
293 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 if ( is_array( $second_value ) && count( $second_value ) ) {
206
207 $first_value = Arr::get( $next_arr, $key, null );
208
209 if ( ! is_array( $first_value ) ) {
210 $next_arr[ $key ] = $second_value;
211 } else {
212 $next_arr[ $key ] = LodashBasic::mergeSkipSeqArray( $first_value, $second_value );
213 }
214 } else {
215 $next_arr[ $key ] = $second_value;
216 }
217 }
218
219 return $next_arr;
220 }
221 }
222
223 return array();
224 }
225
226 static function omit( $object, $property ) {
227 return Arr::except( $object, $property );
228 }
229
230 static function pick( $array, $paths ) {
231 $paths_by_name = array_fill_keys( (array) $paths, true );
232
233 return array_filter(
234 $array,
235 function ( $key ) use ( $paths_by_name ) {
236 return isset( $paths_by_name[ $key ] );
237 },
238 ARRAY_FILTER_USE_KEY
239 );
240 }
241
242 static function kebabCase( $string ) {
243 return Str::kebab( $string );
244 }
245
246 /**
247 * This method returns the first argument it receives.
248 *
249 * @param mixed $value Any value.
250 *
251 * @return mixed Returns `value`.
252 * @category Util
253 *
254 * @example
255 * <code>
256 * $object = ['a' => 1];
257 *
258 * identity($object) === $object;
259 * // => true
260 * </code>
261 */
262 static function identity( $value ) {
263 return _\identity( $value );
264 }
265
266 static function uniq( $values ) {
267 return array_unique( $values );
268 }
269
270 static function diff( $a1, $a2 ) {
271 $r = array();
272 foreach ( $a1 as $k => $v ) {
273 if ( array_key_exists( $k, $a2 ) ) {
274 if ( is_array( $v ) ) {
275 $rad = self::diff( $v, $a2[ $k ] );
276 if ( count( $rad ) ) {
277 $r[ $k ] = $rad;
278 }
279 } else {
280 if ( $v != $a2[ $k ] ) {
281 $r[ $k ] = $v;
282 }
283 }
284 } else {
285 $r[ $k ] = $v;
286 }
287 }
288
289 return $r;
290 }
291
292 }
293