PluginProbe
Elementor Website Builder – more than just a page builder / 3.11.2
Elementor Website Builder – more than just a page builder v3.11.2
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 +22 -97 4.1.0-dev33.11.2 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,9 +21,9 @@
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,9 +30,9 @@
38 30 * @param array $items
39 31 *
40 32 * @return static
41 33 */
42 - public static function make( array $items = [] ) {
34 + public static function make( array $items ) {
43 35 return new static( $items );
44 36 }
45 37
46 38 /**
@@ -47,9 +39,9 @@
47 39 * @param callable|null $callback
48 40 *
49 41 * @return $this
50 42 */
51 - public function filter( ?callable $callback = null ) {
43 + public function filter( callable $callback = null ) {
52 44 if ( ! $callback ) {
53 45 return new static( array_filter( $this->items ) );
54 46 }
55 47
@@ -123,9 +115,9 @@
123 115
124 116 /**
125 117 * Run a map over each of the items.
126 118 *
127 - * @param callable $callback
119 + * @param callable $callback
128 120 * @return $this
129 121 */
130 122 public function map( callable $callback ) {
131 123 $keys = array_keys( $this->items );
@@ -138,9 +130,9 @@
138 130 /**
139 131 * Run a callback over each of the items.
140 132 *
141 133 * @param callable $callback
142 - * @return $this
134 + * @return void
143 135 */
144 136 public function each( callable $callback ) {
145 137 foreach ( $this->items as $key => $value ) {
146 138 if ( false === $callback( $value, $key ) ) {
@@ -166,12 +158,8 @@
166 158
167 159 return $result;
168 160 }
169 161
170 - public function reverse() {
171 - return new static( array_reverse( $this->items ) );
172 - }
173 -
174 162 /**
175 163 * @param callable $callback
176 164 *
177 165 * @return $this
@@ -274,15 +262,15 @@
274 262 /**
275 263 * Get specific item from the collection.
276 264 *
277 265 * @param $key
278 - * @param null $fallback
266 + * @param null $default
279 267 *
280 268 * @return mixed|null
281 269 */
282 - public function get( $key, $fallback = null ) {
270 + public function get( $key, $default = null ) {
283 271 if ( ! array_key_exists( $key, $this->items ) ) {
284 - return $fallback;
272 + return $default;
285 273 }
286 274
287 275 return $this->items[ $key ];
288 276 }
@@ -289,15 +277,15 @@
289 277
290 278 /**
291 279 * Get the first item.
292 280 *
293 - * @param null $fallback
281 + * @param null $default
294 282 *
295 283 * @return mixed|null
296 284 */
297 - public function first( $fallback = null ) {
285 + public function first( $default = null ) {
298 286 if ( $this->is_empty() ) {
299 - return $fallback;
287 + return $default;
300 288 }
301 289
302 290 foreach ( $this->items as $item ) {
303 291 return $item;
@@ -307,13 +295,13 @@
307 295 /**
308 296 * Find an element from the items.
309 297 *
310 298 * @param callable $callback
311 - * @param null $fallback
299 + * @param null $default
312 300 *
313 301 * @return mixed|null
314 302 */
315 - public function find( callable $callback, $fallback = null ) {
303 + public function find( callable $callback, $default = null ) {
316 304 foreach ( $this->all() as $key => $item ) {
317 305 if ( $callback( $item, $key ) ) {
318 306 return $item;
319 307 }
@@ -318,48 +306,12 @@
318 306 return $item;
319 307 }
320 308 }
321 309
322 - return $fallback;
310 + return $default;
323 311 }
324 312
325 313 /**
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 314 * Make sure all the values inside the array are uniques.
363 315 *
364 316 * @param null|string|string[] $keys
365 317 *
@@ -402,10 +354,13 @@
402 354 return false;
403 355 } );
404 356 }
405 357
358 + /**
359 + * @return array
360 + */
406 361 public function keys() {
407 - return new static( array_keys( $this->items ) );
362 + return array_keys( $this->items );
408 363 }
409 364
410 365 /**
411 366 * @return bool
@@ -452,14 +407,10 @@
452 407
453 408 return new static( $result );
454 409 }
455 410
456 - public function flip() {
457 - return new static( array_flip( $this->items ) );
458 - }
459 -
460 411 /**
461 - * @param array ...$values
412 + * @param ...$values
462 413 *
463 414 * @return $this
464 415 */
465 416 public function push( ...$values ) {
@@ -469,34 +420,8 @@
469 420
470 421 return $this;
471 422 }
472 423
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 424 /**
500 425 * @param mixed $offset
501 426 *
502 427 * @return bool
@@ -555,14 +480,14 @@
555 480
556 481 /**
557 482 * @param $item
558 483 * @param $key
559 - * @param null $fallback
484 + * @param null $default
560 485 *
561 486 * @return mixed|null
562 487 */
563 - private function get_item_value( $item, $key, $fallback = null ) {
564 - $value = $fallback;
488 + private function get_item_value( $item, $key, $default = null ) {
489 + $value = $default;
565 490
566 491 if ( is_object( $item ) && isset( $item->{$key} ) ) {
567 492 $value = $item->{$key};
568 493 } elseif ( is_array( $item ) && isset( $item[ $key ] ) ) {