PluginProbe
Elementor Website Builder – more than just a page builder / 3.3.1
Elementor Website Builder – more than just a page builder v3.3.1
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
← All changes | core/utils/collection.php +53 -216 4.2.23.3.1 View file →
@@ -1,12 +1,9 @@
1 1 <?php
2 2 /**
3 3 * Inspired by Laravel Collection.
4 - *
5 4 * @link https://github.com/illuminate/collections
6 - * @package Elementor\Core\Utils
7 5 */
8 -
9 6 namespace Elementor\Core\Utils;
10 7
11 8 if ( ! defined( 'ABSPATH' ) ) {
12 9 exit; // Exit if accessed directly.
@@ -11,13 +8,8 @@
11 8 if ( ! defined( 'ABSPATH' ) ) {
12 9 exit; // Exit if accessed directly.
13 10 }
14 11
15 -/**
16 - * Inspired by Laravel Collection.
17 - *
18 - * @link https://github.com/illuminate/collections
19 - */
20 12 class Collection implements \ArrayAccess, \Countable, \IteratorAggregate {
21 13 /**
22 14 * The items contained in the collection.
23 15 *
@@ -29,27 +21,18 @@
29 21 * Collection constructor.
30 22 *
31 23 * @param array $items
32 24 */
33 - public function __construct( array $items = [] ) {
25 + public function __construct( array $items ) {
34 26 $this->items = $items;
35 27 }
36 28
37 29 /**
38 - * @param array $items
39 - *
40 - * @return static
41 - */
42 - public static function make( array $items = [] ) {
43 - return new static( $items );
44 - }
45 -
46 - /**
47 30 * @param callable|null $callback
48 31 *
49 32 * @return $this
50 33 */
51 - public function filter( ?callable $callback = null ) {
34 + public function filter( callable $callback = null ) {
52 35 if ( ! $callback ) {
53 36 return new static( array_filter( $this->items ) );
54 37 }
55 38
@@ -123,9 +106,9 @@
123 106
124 107 /**
125 108 * Run a map over each of the items.
126 109 *
127 - * @param callable $callback
110 + * @param callable $callback
128 111 * @return $this
129 112 */
130 113 public function map( callable $callback ) {
131 114 $keys = array_keys( $this->items );
@@ -135,25 +118,9 @@
135 118 return new static( array_combine( $keys, $items ) );
136 119 }
137 120
138 121 /**
139 - * Run a callback over each of the items.
140 - *
141 122 * @param callable $callback
142 - * @return $this
143 - */
144 - public function each( callable $callback ) {
145 - foreach ( $this->items as $key => $value ) {
146 - if ( false === $callback( $value, $key ) ) {
147 - break;
148 - }
149 - }
150 -
151 - return $this;
152 - }
153 -
154 - /**
155 - * @param callable $callback
156 123 * @param null $initial
157 124 *
158 125 * @return mixed|null
159 126 */
@@ -166,12 +133,8 @@
166 133
167 134 return $result;
168 135 }
169 136
170 - public function reverse() {
171 - return new static( array_reverse( $this->items ) );
172 - }
173 -
174 137 /**
175 138 * @param callable $callback
176 139 *
177 140 * @return $this
@@ -225,10 +188,14 @@
225 188 */
226 189 public function pluck( $key ) {
227 190 $result = [];
228 191
229 - foreach ( $this->items as $item ) {
230 - $result[] = $this->get_item_value( $item, $key );
192 + foreach ( $this->items as $value ) {
193 + if ( is_object( $value ) && isset( $value->{$key} ) ) {
194 + $result[] = $value->{$key};
195 + } elseif ( is_array( $value ) && isset( $value[ $key ] ) ) {
196 + $result[] = $value[ $key ];
197 + }
231 198 }
232 199
233 200 return new static( $result );
234 201 }
@@ -242,12 +209,18 @@
242 209 */
243 210 public function group_by( $group_by ) {
244 211 $result = [];
245 212
246 - foreach ( $this->items as $item ) {
247 - $group_key = $this->get_item_value( $item, $group_by, 0 );
213 + foreach ( $this->items as $value ) {
214 + $group_key = 0;
248 215
249 - $result[ $group_key ][] = $item;
216 + if ( is_object( $value ) && isset( $value->{$group_by} ) ) {
217 + $group_key = $value->{$group_by};
218 + } elseif ( is_array( $value ) && isset( $value[ $group_by ] ) ) {
219 + $group_key = $value[ $group_by ];
220 + }
221 +
222 + $result[ $group_key ][] = $value;
250 223 }
251 224
252 225 return new static( $result );
253 226 }
@@ -274,15 +247,15 @@
274 247 /**
275 248 * Get specific item from the collection.
276 249 *
277 250 * @param $key
278 - * @param null $fallback
251 + * @param null $default
279 252 *
280 253 * @return mixed|null
281 254 */
282 - public function get( $key, $fallback = null ) {
255 + public function get( $key, $default = null ) {
283 256 if ( ! array_key_exists( $key, $this->items ) ) {
284 - return $fallback;
257 + return $default;
285 258 }
286 259
287 260 return $this->items[ $key ];
288 261 }
@@ -289,15 +262,15 @@
289 262
290 263 /**
291 264 * Get the first item.
292 265 *
293 - * @param null $fallback
266 + * @param null $default
294 267 *
295 268 * @return mixed|null
296 269 */
297 - public function first( $fallback = null ) {
270 + public function first( $default = null ) {
298 271 if ( $this->is_empty() ) {
299 - return $fallback;
272 + return $default;
300 273 }
301 274
302 275 foreach ( $this->items as $item ) {
303 276 return $item;
@@ -307,13 +280,13 @@
307 280 /**
308 281 * Find an element from the items.
309 282 *
310 283 * @param callable $callback
311 - * @param null $fallback
284 + * @param null $default
312 285 *
313 286 * @return mixed|null
314 287 */
315 - public function find( callable $callback, $fallback = null ) {
288 + public function find( callable $callback, $default = null ) {
316 289 foreach ( $this->all() as $key => $item ) {
317 290 if ( $callback( $item, $key ) ) {
318 291 return $item;
319 292 }
@@ -318,83 +291,37 @@
318 291 return $item;
319 292 }
320 293 }
321 294
322 - return $fallback;
295 + return $default;
323 296 }
324 297
325 298 /**
326 - * @param callable|string|int $value
327 - *
328 - * @return bool
329 - */
330 - public function contains( $value ) {
331 - $callback = $value instanceof \Closure
332 - ? $value
333 - : function ( $item ) use ( $value ) {
334 - return $item === $value;
335 - };
336 -
337 - foreach ( $this->all() as $key => $item ) {
338 - if ( $callback( $item, $key ) ) {
339 - return true;
340 - }
341 - }
342 -
343 - return false;
344 - }
345 -
346 - /**
347 - * Run array_diff between the collection and other array or collection.
348 - *
349 - * @param $filter
350 - *
351 - * @return $this
352 - */
353 - public function diff( $filter ) {
354 - if ( $filter instanceof self ) {
355 - $filter = $filter->all();
356 - }
357 -
358 - return new static( array_diff( $this->all(), $filter ) );
359 - }
360 -
361 - /**
362 299 * Make sure all the values inside the array are uniques.
363 300 *
364 - * @param null|string|string[] $keys
301 + * @param null $key
365 302 *
366 303 * @return $this
367 304 */
368 - public function unique( $keys = null ) {
369 - if ( ! $keys ) {
305 + public function unique( $key = null ) {
306 + if ( ! $key ) {
370 307 return new static(
371 308 array_unique( $this->items )
372 309 );
373 310 }
374 311
375 - if ( ! is_array( $keys ) ) {
376 - $keys = [ $keys ];
377 - }
378 -
379 312 $exists = [];
380 313
381 - return $this->filter( function ( $item ) use ( $keys, &$exists ) {
314 + return $this->filter( function ( $item ) use ( $key, &$exists ) {
382 315 $value = null;
383 316
384 - foreach ( $keys as $key ) {
385 - $current_value = $this->get_item_value( $item, $key );
386 -
387 - $value .= "{$key}:{$current_value};";
317 + if ( is_object( $item ) && isset( $item->{$key} ) ) {
318 + $value = $item->{$key};
319 + } elseif ( is_array( $item ) && isset( $item[ $key ] ) ) {
320 + $value = $item[ $key ];
388 321 }
389 322
390 - // If no value for the specific key return the item.
391 - if ( null === $value ) {
392 - return true;
393 - }
394 -
395 - // If value is not exists, add to the exists array and return the item.
396 - if ( ! in_array( $value, $exists, true ) ) {
323 + if ( null !== $value && ! in_array( $value, $exists, true ) ) {
397 324 $exists[] = $value;
398 325
399 326 return true;
400 327 }
@@ -402,10 +329,13 @@
402 329 return false;
403 330 } );
404 331 }
405 332
333 + /**
334 + * @return array
335 + */
406 336 public function keys() {
407 - return new static( array_keys( $this->items ) );
337 + return array_keys( $this->items );
408 338 }
409 339
410 340 /**
411 341 * @return bool
@@ -428,120 +358,47 @@
428 358 return array_values( $this->all() );
429 359 }
430 360
431 361 /**
432 - * Support only one level depth.
362 + * @param mixed $key
433 363 *
434 - * @return $this
435 - */
436 - public function flatten() {
437 - $result = [];
438 -
439 - foreach ( $this->all() as $item ) {
440 - $item = $item instanceof Collection ? $item->all() : $item;
441 -
442 - if ( ! is_array( $item ) ) {
443 - $result[] = $item;
444 - } else {
445 - $values = array_values( $item );
446 -
447 - foreach ( $values as $value ) {
448 - $result[] = $value;
449 - }
450 - }
451 - }
452 -
453 - return new static( $result );
454 - }
455 -
456 - public function flip() {
457 - return new static( array_flip( $this->items ) );
458 - }
459 -
460 - /**
461 - * @param array ...$values
462 - *
463 - * @return $this
464 - */
465 - public function push( ...$values ) {
466 - foreach ( $values as $value ) {
467 - $this->items[] = $value;
468 - }
469 -
470 - return $this;
471 - }
472 -
473 - public function prepend( ...$values ) {
474 - $this->items = array_merge( $values, $this->items );
475 -
476 - return $this;
477 - }
478 -
479 - public function some( callable $callback ) {
480 - foreach ( $this->items as $key => $item ) {
481 - if ( $callback( $item, $key ) ) {
482 - return true;
483 - }
484 - }
485 -
486 - return false;
487 - }
488 -
489 - public function every( callable $callback ) {
490 - foreach ( $this->items as $key => $item ) {
491 - if ( ! $callback( $item, $key ) ) {
492 - return false;
493 - }
494 - }
495 -
496 - return true;
497 - }
498 -
499 - /**
500 - * @param mixed $offset
501 - *
502 364 * @return bool
503 365 */
504 - #[\ReturnTypeWillChange]
505 - public function offsetExists( $offset ) {
506 - return isset( $this->items[ $offset ] );
366 + public function offsetExists( $key ) {
367 + return isset( $this->items[ $key ] );
507 368 }
508 369
509 370 /**
510 - * @param mixed $offset
371 + * @param mixed $key
511 372 *
512 373 * @return mixed
513 374 */
514 - #[\ReturnTypeWillChange]
515 - public function offsetGet( $offset ) {
516 - return $this->items[ $offset ];
375 + public function offsetGet( $key ) {
376 + return $this->items[ $key ];
517 377 }
518 378
519 379 /**
520 - * @param mixed $offset
380 + * @param mixed $key
521 381 * @param mixed $value
522 382 */
523 - #[\ReturnTypeWillChange]
524 - public function offsetSet( $offset, $value ) {
525 - if ( is_null( $offset ) ) {
383 + public function offsetSet( $key, $value ) {
384 + if ( is_null( $key ) ) {
526 385 $this->items[] = $value;
527 386 } else {
528 - $this->items[ $offset ] = $value;
387 + $this->items[ $key ] = $value;
529 388 }
530 389 }
531 390
532 391 /**
533 - * @param mixed $offset
392 + * @param mixed $key
534 393 */
535 - #[\ReturnTypeWillChange]
536 - public function offsetUnset( $offset ) {
537 - unset( $this->items[ $offset ] );
394 + public function offsetUnset( $key ) {
395 + unset( $this->items[ $key ] );
538 396 }
539 397
540 398 /**
541 399 * @return \ArrayIterator|\Traversable
542 400 */
543 - #[\ReturnTypeWillChange]
544 401 public function getIterator() {
545 402 return new \ArrayIterator( $this->items );
546 403 }
547 404
@@ -547,28 +404,8 @@
547 404
548 405 /**
549 406 * @return int|void
550 407 */
551 - #[\ReturnTypeWillChange]
552 408 public function count() {
553 409 return count( $this->items );
554 - }
555 -
556 - /**
557 - * @param $item
558 - * @param $key
559 - * @param null $fallback
560 - *
561 - * @return mixed|null
562 - */
563 - private function get_item_value( $item, $key, $fallback = null ) {
564 - $value = $fallback;
565 -
566 - if ( is_object( $item ) && isset( $item->{$key} ) ) {
567 - $value = $item->{$key};
568 - } elseif ( is_array( $item ) && isset( $item[ $key ] ) ) {
569 - $value = $item[ $key ];
570 - }
571 -
572 - return $value;
573 410 }
574 411 }