PluginProbe
Elementor Website Builder – more than just a page builder / 3.5.3
Elementor Website Builder – more than just a page builder v3.5.3
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 +33 -139 4.2.0-dev23.5.3 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
@@ -274,15 +237,15 @@
274 237 /**
275 238 * Get specific item from the collection.
276 239 *
277 240 * @param $key
278 - * @param null $fallback
241 + * @param null $default
279 242 *
280 243 * @return mixed|null
281 244 */
282 - public function get( $key, $fallback = null ) {
245 + public function get( $key, $default = null ) {
283 246 if ( ! array_key_exists( $key, $this->items ) ) {
284 - return $fallback;
247 + return $default;
285 248 }
286 249
287 250 return $this->items[ $key ];
288 251 }
@@ -289,15 +252,15 @@
289 252
290 253 /**
291 254 * Get the first item.
292 255 *
293 - * @param null $fallback
256 + * @param null $default
294 257 *
295 258 * @return mixed|null
296 259 */
297 - public function first( $fallback = null ) {
260 + public function first( $default = null ) {
298 261 if ( $this->is_empty() ) {
299 - return $fallback;
262 + return $default;
300 263 }
301 264
302 265 foreach ( $this->items as $item ) {
303 266 return $item;
@@ -307,13 +270,13 @@
307 270 /**
308 271 * Find an element from the items.
309 272 *
310 273 * @param callable $callback
311 - * @param null $fallback
274 + * @param null $default
312 275 *
313 276 * @return mixed|null
314 277 */
315 - public function find( callable $callback, $fallback = null ) {
278 + public function find( callable $callback, $default = null ) {
316 279 foreach ( $this->all() as $key => $item ) {
317 280 if ( $callback( $item, $key ) ) {
318 281 return $item;
319 282 }
@@ -318,48 +281,12 @@
318 281 return $item;
319 282 }
320 283 }
321 284
322 - return $fallback;
285 + return $default;
323 286 }
324 287
325 288 /**
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 289 * Make sure all the values inside the array are uniques.
363 290 *
364 291 * @param null|string|string[] $keys
365 292 *
@@ -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
@@ -452,14 +382,10 @@
452 382
453 383 return new static( $result );
454 384 }
455 385
456 - public function flip() {
457 - return new static( array_flip( $this->items ) );
458 - }
459 -
460 386 /**
461 - * @param array ...$values
387 + * @param ...$values
462 388 *
463 389 * @return $this
464 390 */
465 391 public function push( ...$values ) {
@@ -469,79 +395,48 @@
469 395
470 396 return $this;
471 397 }
472 398
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 399 /**
500 - * @param mixed $offset
400 + * @param mixed $key
501 401 *
502 402 * @return bool
503 403 */
504 - #[\ReturnTypeWillChange]
505 - public function offsetExists( $offset ) {
506 - return isset( $this->items[ $offset ] );
404 + public function offsetExists( $key ) {
405 + return isset( $this->items[ $key ] );
507 406 }
508 407
509 408 /**
510 - * @param mixed $offset
409 + * @param mixed $key
511 410 *
512 411 * @return mixed
513 412 */
514 - #[\ReturnTypeWillChange]
515 - public function offsetGet( $offset ) {
516 - return $this->items[ $offset ];
413 + public function offsetGet( $key ) {
414 + return $this->items[ $key ];
517 415 }
518 416
519 417 /**
520 - * @param mixed $offset
418 + * @param mixed $key
521 419 * @param mixed $value
522 420 */
523 - #[\ReturnTypeWillChange]
524 - public function offsetSet( $offset, $value ) {
525 - if ( is_null( $offset ) ) {
421 + public function offsetSet( $key, $value ) {
422 + if ( is_null( $key ) ) {
526 423 $this->items[] = $value;
527 424 } else {
528 - $this->items[ $offset ] = $value;
425 + $this->items[ $key ] = $value;
529 426 }
530 427 }
531 428
532 429 /**
533 - * @param mixed $offset
430 + * @param mixed $key
534 431 */
535 - #[\ReturnTypeWillChange]
536 - public function offsetUnset( $offset ) {
537 - unset( $this->items[ $offset ] );
432 + public function offsetUnset( $key ) {
433 + unset( $this->items[ $key ] );
538 434 }
539 435
540 436 /**
541 437 * @return \ArrayIterator|\Traversable
542 438 */
543 - #[\ReturnTypeWillChange]
544 439 public function getIterator() {
545 440 return new \ArrayIterator( $this->items );
546 441 }
547 442
@@ -547,9 +442,8 @@
547 442
548 443 /**
549 444 * @return int|void
550 445 */
551 - #[\ReturnTypeWillChange]
552 446 public function count() {
553 447 return count( $this->items );
554 448 }
555 449
@@ -555,14 +449,14 @@
555 449
556 450 /**
557 451 * @param $item
558 452 * @param $key
559 - * @param null $fallback
453 + * @param null $default
560 454 *
561 455 * @return mixed|null
562 456 */
563 - private function get_item_value( $item, $key, $fallback = null ) {
564 - $value = $fallback;
457 + private function get_item_value( $item, $key, $default = null ) {
458 + $value = $default;
565 459
566 460 if ( is_object( $item ) && isset( $item->{$key} ) ) {
567 461 $value = $item->{$key};
568 462 } elseif ( is_array( $item ) && isset( $item[ $key ] ) ) {