| @@ -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 |
| @@ -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,9 +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 | 410 | } |
| 555 | 411 | |
| @@ -555,14 +411,14 @@ | ||
| 555 | 411 | |
| 556 | 412 | /** |
| 557 | 413 | * @param $item |
| 558 | 414 | * @param $key |
| 559 | - * @param null $fallback | |
| 415 | + * @param null $default | |
| 560 | 416 | * |
| 561 | 417 | * @return mixed|null |
| 562 | 418 | */ |
| 563 | - private function get_item_value( $item, $key, $fallback = null ) { | |
| 564 | - $value = $fallback; | |
| 419 | + private function get_item_value( $item, $key, $default = null ) { | |
| 420 | + $value = $default; | |
| 565 | 421 | |
| 566 | 422 | if ( is_object( $item ) && isset( $item->{$key} ) ) { |
| 567 | 423 | $value = $item->{$key}; |
| 568 | 424 | } elseif ( is_array( $item ) && isset( $item[ $key ] ) ) { |