PluginProbe
Elementor Website Builder – more than just a page builder / 3.8.0
Elementor Website Builder – more than just a page builder v3.8.0
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / utils / collection.php

collection.php in Elementor Website Builder – more than just a page builder 3.8.0, at core/utils/collection.php

491 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Inspired by Laravel Collection.
4 * @link https://github.com/illuminate/collections
5 */
6 namespace Elementor\Core\Utils;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 class Collection implements \ArrayAccess, \Countable, \IteratorAggregate {
13 /**
14 * The items contained in the collection.
15 *
16 * @var array
17 */
18 protected $items;
19
20 /**
21 * Collection constructor.
22 *
23 * @param array $items
24 */
25 public function __construct( array $items ) {
26 $this->items = $items;
27 }
28
29 /**
30 * @param callable|null $callback
31 *
32 * @return $this
33 */
34 public function filter( callable $callback = null ) {
35 if ( ! $callback ) {
36 return new static( array_filter( $this->items ) );
37 }
38
39 return new static( array_filter( $this->items, $callback, ARRAY_FILTER_USE_BOTH ) );
40 }
41
42 /**
43 * @param $items
44 *
45 * @return $this
46 */
47 public function merge( $items ) {
48 if ( $items instanceof Collection ) {
49 $items = $items->all();
50 }
51
52 return new static( array_merge( $this->items, $items ) );
53 }
54
55 /**
56 * Union the collection with the given items.
57 *
58 * @param array $items
59 *
60 * @return $this
61 */
62 public function union( array $items ) {
63 return new static( $this->all() + $items );
64 }
65
66 /**
67 * Merge array recursively
68 *
69 * @param $items
70 *
71 * @return $this
72 */
73 public function merge_recursive( $items ) {
74 if ( $items instanceof Collection ) {
75 $items = $items->all();
76 }
77
78 return new static( array_merge_recursive( $this->items, $items ) );
79 }
80
81 /**
82 * Replace array recursively
83 *
84 * @param $items
85 *
86 * @return $this
87 */
88 public function replace_recursive( $items ) {
89 if ( $items instanceof Collection ) {
90 $items = $items->all();
91 }
92
93 return new static( array_replace_recursive( $this->items, $items ) );
94 }
95
96 /**
97 * Implode the items
98 *
99 * @param $glue
100 *
101 * @return string
102 */
103 public function implode( $glue ) {
104 return implode( $glue, $this->items );
105 }
106
107 /**
108 * Run a map over each of the items.
109 *
110 * @param callable $callback
111 * @return $this
112 */
113 public function map( callable $callback ) {
114 $keys = array_keys( $this->items );
115
116 $items = array_map( $callback, $this->items, $keys );
117
118 return new static( array_combine( $keys, $items ) );
119 }
120
121 /**
122 * Run a callback over each of the items.
123 *
124 * @param callable $callback
125 * @return void
126 */
127 public function each( callable $callback ) {
128 foreach ( $this->items as $key => $value ) {
129 if ( false === $callback( $value, $key ) ) {
130 break;
131 }
132 }
133
134 return $this;
135 }
136
137 /**
138 * @param callable $callback
139 * @param null $initial
140 *
141 * @return mixed|null
142 */
143 public function reduce( callable $callback, $initial = null ) {
144 $result = $initial;
145
146 foreach ( $this->all() as $key => $value ) {
147 $result = $callback( $result, $value, $key );
148 }
149
150 return $result;
151 }
152
153 /**
154 * @param callable $callback
155 *
156 * @return $this
157 */
158 public function map_with_keys( callable $callback ) {
159 $result = [];
160
161 foreach ( $this->items as $key => $value ) {
162 $assoc = $callback( $value, $key );
163
164 foreach ( $assoc as $map_key => $map_value ) {
165 $result[ $map_key ] = $map_value;
166 }
167 }
168
169 return new static( $result );
170 }
171
172 /**
173 * Get all items except for those with the specified keys.
174 *
175 * @param array $keys
176 *
177 * @return $this
178 */
179 public function except( array $keys ) {
180 return $this->filter( function ( $value, $key ) use ( $keys ) {
181 return ! in_array( $key, $keys, true );
182 } );
183 }
184
185 /**
186 * Get the items with the specified keys.
187 *
188 * @param array $keys
189 *
190 * @return $this
191 */
192 public function only( array $keys ) {
193 return $this->filter( function ( $value, $key ) use ( $keys ) {
194 return in_array( $key, $keys, true );
195 } );
196 }
197
198 /**
199 * Run over the collection to get specific prop from the collection item.
200 *
201 * @param $key
202 *
203 * @return $this
204 */
205 public function pluck( $key ) {
206 $result = [];
207
208 foreach ( $this->items as $item ) {
209 $result[] = $this->get_item_value( $item, $key );
210 }
211
212 return new static( $result );
213 }
214
215 /**
216 * Group the collection items by specific key in each collection item.
217 *
218 * @param $group_by
219 *
220 * @return $this
221 */
222 public function group_by( $group_by ) {
223 $result = [];
224
225 foreach ( $this->items as $item ) {
226 $group_key = $this->get_item_value( $item, $group_by, 0 );
227
228 $result[ $group_key ][] = $item;
229 }
230
231 return new static( $result );
232 }
233
234 /**
235 * Sort keys
236 *
237 * @param false $descending
238 *
239 * @return $this
240 */
241 public function sort_keys( $descending = false ) {
242 $items = $this->items;
243
244 if ( $descending ) {
245 krsort( $items );
246 } else {
247 ksort( $items );
248 }
249
250 return new static( $items );
251 }
252
253 /**
254 * Get specific item from the collection.
255 *
256 * @param $key
257 * @param null $default
258 *
259 * @return mixed|null
260 */
261 public function get( $key, $default = null ) {
262 if ( ! array_key_exists( $key, $this->items ) ) {
263 return $default;
264 }
265
266 return $this->items[ $key ];
267 }
268
269 /**
270 * Get the first item.
271 *
272 * @param null $default
273 *
274 * @return mixed|null
275 */
276 public function first( $default = null ) {
277 if ( $this->is_empty() ) {
278 return $default;
279 }
280
281 foreach ( $this->items as $item ) {
282 return $item;
283 }
284 }
285
286 /**
287 * Find an element from the items.
288 *
289 * @param callable $callback
290 * @param null $default
291 *
292 * @return mixed|null
293 */
294 public function find( callable $callback, $default = null ) {
295 foreach ( $this->all() as $key => $item ) {
296 if ( $callback( $item, $key ) ) {
297 return $item;
298 }
299 }
300
301 return $default;
302 }
303
304 /**
305 * Make sure all the values inside the array are uniques.
306 *
307 * @param null|string|string[] $keys
308 *
309 * @return $this
310 */
311 public function unique( $keys = null ) {
312 if ( ! $keys ) {
313 return new static(
314 array_unique( $this->items )
315 );
316 }
317
318 if ( ! is_array( $keys ) ) {
319 $keys = [ $keys ];
320 }
321
322 $exists = [];
323
324 return $this->filter( function ( $item ) use ( $keys, &$exists ) {
325 $value = null;
326
327 foreach ( $keys as $key ) {
328 $current_value = $this->get_item_value( $item, $key );
329
330 $value .= "{$key}:{$current_value};";
331 }
332
333 // If no value for the specific key return the item.
334 if ( null === $value ) {
335 return true;
336 }
337
338 // If value is not exists, add to the exists array and return the item.
339 if ( ! in_array( $value, $exists, true ) ) {
340 $exists[] = $value;
341
342 return true;
343 }
344
345 return false;
346 } );
347 }
348
349 /**
350 * @return array
351 */
352 public function keys() {
353 return array_keys( $this->items );
354 }
355
356 /**
357 * @return bool
358 */
359 public function is_empty() {
360 return empty( $this->items );
361 }
362
363 /**
364 * @return array
365 */
366 public function all() {
367 return $this->items;
368 }
369
370 /**
371 * @return array
372 */
373 public function values() {
374 return array_values( $this->all() );
375 }
376
377 /**
378 * Support only one level depth.
379 *
380 * @return $this
381 */
382 public function flatten() {
383 $result = [];
384
385 foreach ( $this->all() as $item ) {
386 $item = $item instanceof Collection ? $item->all() : $item;
387
388 if ( ! is_array( $item ) ) {
389 $result[] = $item;
390 } else {
391 $values = array_values( $item );
392
393 foreach ( $values as $value ) {
394 $result[] = $value;
395 }
396 }
397 }
398
399 return new static( $result );
400 }
401
402 /**
403 * @param ...$values
404 *
405 * @return $this
406 */
407 public function push( ...$values ) {
408 foreach ( $values as $value ) {
409 $this->items[] = $value;
410 }
411
412 return $this;
413 }
414
415 /**
416 * @param mixed $offset
417 *
418 * @return bool
419 */
420 #[\ReturnTypeWillChange]
421 public function offsetExists( $offset ) {
422 return isset( $this->items[ $offset ] );
423 }
424
425 /**
426 * @param mixed $offset
427 *
428 * @return mixed
429 */
430 #[\ReturnTypeWillChange]
431 public function offsetGet( $offset ) {
432 return $this->items[ $offset ];
433 }
434
435 /**
436 * @param mixed $offset
437 * @param mixed $value
438 */
439 #[\ReturnTypeWillChange]
440 public function offsetSet( $offset, $value ) {
441 if ( is_null( $offset ) ) {
442 $this->items[] = $value;
443 } else {
444 $this->items[ $offset ] = $value;
445 }
446 }
447
448 /**
449 * @param mixed $offset
450 */
451 #[\ReturnTypeWillChange]
452 public function offsetUnset( $offset ) {
453 unset( $this->items[ $offset ] );
454 }
455
456 /**
457 * @return \ArrayIterator|\Traversable
458 */
459 #[\ReturnTypeWillChange]
460 public function getIterator() {
461 return new \ArrayIterator( $this->items );
462 }
463
464 /**
465 * @return int|void
466 */
467 #[\ReturnTypeWillChange]
468 public function count() {
469 return count( $this->items );
470 }
471
472 /**
473 * @param $item
474 * @param $key
475 * @param null $default
476 *
477 * @return mixed|null
478 */
479 private function get_item_value( $item, $key, $default = null ) {
480 $value = $default;
481
482 if ( is_object( $item ) && isset( $item->{$key} ) ) {
483 $value = $item->{$key};
484 } elseif ( is_array( $item ) && isset( $item[ $key ] ) ) {
485 $value = $item[ $key ];
486 }
487
488 return $value;
489 }
490 }
491