PluginProbe
Elementor Website Builder – more than just a page builder / 3.7.3
Elementor Website Builder – more than just a page builder v3.7.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 +21 -105 4.2.23.7.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 );
@@ -138,9 +121,9 @@
138 121 /**
139 122 * Run a callback over each of the items.
140 123 *
141 124 * @param callable $callback
142 - * @return $this
125 + * @return void
143 126 */
144 127 public function each( callable $callback ) {
145 128 foreach ( $this->items as $key => $value ) {
146 129 if ( false === $callback( $value, $key ) ) {
@@ -166,12 +149,8 @@
166 149
167 150 return $result;
168 151 }
169 152
170 - public function reverse() {
171 - return new static( array_reverse( $this->items ) );
172 - }
173 -
174 153 /**
175 154 * @param callable $callback
176 155 *
177 156 * @return $this
@@ -274,15 +253,15 @@
274 253 /**
275 254 * Get specific item from the collection.
276 255 *
277 256 * @param $key
278 - * @param null $fallback
257 + * @param null $default
279 258 *
280 259 * @return mixed|null
281 260 */
282 - public function get( $key, $fallback = null ) {
261 + public function get( $key, $default = null ) {
283 262 if ( ! array_key_exists( $key, $this->items ) ) {
284 - return $fallback;
263 + return $default;
285 264 }
286 265
287 266 return $this->items[ $key ];
288 267 }
@@ -289,15 +268,15 @@
289 268
290 269 /**
291 270 * Get the first item.
292 271 *
293 - * @param null $fallback
272 + * @param null $default
294 273 *
295 274 * @return mixed|null
296 275 */
297 - public function first( $fallback = null ) {
276 + public function first( $default = null ) {
298 277 if ( $this->is_empty() ) {
299 - return $fallback;
278 + return $default;
300 279 }
301 280
302 281 foreach ( $this->items as $item ) {
303 282 return $item;
@@ -307,13 +286,13 @@
307 286 /**
308 287 * Find an element from the items.
309 288 *
310 289 * @param callable $callback
311 - * @param null $fallback
290 + * @param null $default
312 291 *
313 292 * @return mixed|null
314 293 */
315 - public function find( callable $callback, $fallback = null ) {
294 + public function find( callable $callback, $default = null ) {
316 295 foreach ( $this->all() as $key => $item ) {
317 296 if ( $callback( $item, $key ) ) {
318 297 return $item;
319 298 }
@@ -318,48 +297,12 @@
318 297 return $item;
319 298 }
320 299 }
321 300
322 - return $fallback;
301 + return $default;
323 302 }
324 303
325 304 /**
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 305 * Make sure all the values inside the array are uniques.
363 306 *
364 307 * @param null|string|string[] $keys
365 308 *
@@ -402,10 +345,13 @@
402 345 return false;
403 346 } );
404 347 }
405 348
349 + /**
350 + * @return array
351 + */
406 352 public function keys() {
407 - return new static( array_keys( $this->items ) );
353 + return array_keys( $this->items );
408 354 }
409 355
410 356 /**
411 357 * @return bool
@@ -452,14 +398,10 @@
452 398
453 399 return new static( $result );
454 400 }
455 401
456 - public function flip() {
457 - return new static( array_flip( $this->items ) );
458 - }
459 -
460 402 /**
461 - * @param array ...$values
403 + * @param ...$values
462 404 *
463 405 * @return $this
464 406 */
465 407 public function push( ...$values ) {
@@ -469,34 +411,8 @@
469 411
470 412 return $this;
471 413 }
472 414
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 415 /**
500 416 * @param mixed $offset
501 417 *
502 418 * @return bool
@@ -555,14 +471,14 @@
555 471
556 472 /**
557 473 * @param $item
558 474 * @param $key
559 - * @param null $fallback
475 + * @param null $default
560 476 *
561 477 * @return mixed|null
562 478 */
563 - private function get_item_value( $item, $key, $fallback = null ) {
564 - $value = $fallback;
479 + private function get_item_value( $item, $key, $default = null ) {
480 + $value = $default;
565 481
566 482 if ( is_object( $item ) && isset( $item->{$key} ) ) {
567 483 $value = $item->{$key};
568 484 } elseif ( is_array( $item ) && isset( $item[ $key ] ) ) {