| @@ -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 | * |
| @@ -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 ); |
| @@ -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,9 +306,9 @@ | ||
| 318 | 306 | return $item; |
| 319 | 307 | } |
| 320 | 308 | } |
| 321 | 309 | |
| 322 | - return $fallback; | |
| 310 | + return $default; | |
| 323 | 311 | } |
| 324 | 312 | |
| 325 | 313 | /** |
| 326 | 314 | * @param callable|string|int $value |
| @@ -343,23 +331,8 @@ | ||
| 343 | 331 | return false; |
| 344 | 332 | } |
| 345 | 333 | |
| 346 | 334 | /** |
| 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 | 335 | * Make sure all the values inside the array are uniques. |
| 363 | 336 | * |
| 364 | 337 | * @param null|string|string[] $keys |
| 365 | 338 | * |
| @@ -402,10 +375,13 @@ | ||
| 402 | 375 | return false; |
| 403 | 376 | } ); |
| 404 | 377 | } |
| 405 | 378 | |
| 379 | + /** | |
| 380 | + * @return array | |
| 381 | + */ | |
| 406 | 382 | public function keys() { |
| 407 | - return new static( array_keys( $this->items ) ); | |
| 383 | + return array_keys( $this->items ); | |
| 408 | 384 | } |
| 409 | 385 | |
| 410 | 386 | /** |
| 411 | 387 | * @return bool |
| @@ -452,14 +428,10 @@ | ||
| 452 | 428 | |
| 453 | 429 | return new static( $result ); |
| 454 | 430 | } |
| 455 | 431 | |
| 456 | - public function flip() { | |
| 457 | - return new static( array_flip( $this->items ) ); | |
| 458 | - } | |
| 459 | - | |
| 460 | 432 | /** |
| 461 | - * @param array ...$values | |
| 433 | + * @param ...$values | |
| 462 | 434 | * |
| 463 | 435 | * @return $this |
| 464 | 436 | */ |
| 465 | 437 | public function push( ...$values ) { |
| @@ -485,18 +457,8 @@ | ||
| 485 | 457 | |
| 486 | 458 | return false; |
| 487 | 459 | } |
| 488 | 460 | |
| 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 | 461 | /** |
| 500 | 462 | * @param mixed $offset |
| 501 | 463 | * |
| 502 | 464 | * @return bool |
| @@ -555,14 +517,14 @@ | ||
| 555 | 517 | |
| 556 | 518 | /** |
| 557 | 519 | * @param $item |
| 558 | 520 | * @param $key |
| 559 | - * @param null $fallback | |
| 521 | + * @param null $default | |
| 560 | 522 | * |
| 561 | 523 | * @return mixed|null |
| 562 | 524 | */ |
| 563 | - private function get_item_value( $item, $key, $fallback = null ) { | |
| 564 | - $value = $fallback; | |
| 525 | + private function get_item_value( $item, $key, $default = null ) { | |
| 526 | + $value = $default; | |
| 565 | 527 | |
| 566 | 528 | if ( is_object( $item ) && isset( $item->{$key} ) ) { |
| 567 | 529 | $value = $item->{$key}; |
| 568 | 530 | } elseif ( is_array( $item ) && isset( $item[ $key ] ) ) { |