PluginProbe
Lead Generation Contact Widget & AI Chatbot: Chat Button, Phone Call, Telegram, Email – SiteLeads / trunk
Lead Generation Contact Widget & AI Chatbot: Chat Button, Phone Call, Telegram, Email – SiteLeads vtrunk
1.2.1 1.2.0 1.1.7 trunk 1.0.0 1.1.0 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6
siteleads / lib / Core / LodashBasic.php

LodashBasic.php in Lead Generation Contact Widget & AI Chatbot: Chat Button, Phone Call, Telegram, Email – SiteLeads trunk, at lib/Core/LodashBasic.php

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