| @@ -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 | |
| @@ -69,112 +52,10 @@ | ||
| 69 | 52 | return new static( array_merge( $this->items, $items ) ); |
| 70 | 53 | } |
| 71 | 54 | |
| 72 | 55 | /** |
| 73 | - * Union the collection with the given items. | |
| 74 | - * | |
| 75 | - * @param array $items | |
| 76 | - * | |
| 77 | - * @return $this | |
| 78 | - */ | |
| 79 | - public function union( array $items ) { | |
| 80 | - return new static( $this->all() + $items ); | |
| 81 | - } | |
| 82 | - | |
| 83 | - /** | |
| 84 | - * Merge array recursively | |
| 85 | - * | |
| 86 | - * @param $items | |
| 87 | - * | |
| 88 | - * @return $this | |
| 89 | - */ | |
| 90 | - public function merge_recursive( $items ) { | |
| 91 | - if ( $items instanceof Collection ) { | |
| 92 | - $items = $items->all(); | |
| 93 | - } | |
| 94 | - | |
| 95 | - return new static( array_merge_recursive( $this->items, $items ) ); | |
| 96 | - } | |
| 97 | - | |
| 98 | - /** | |
| 99 | - * Replace array recursively | |
| 100 | - * | |
| 101 | - * @param $items | |
| 102 | - * | |
| 103 | - * @return $this | |
| 104 | - */ | |
| 105 | - public function replace_recursive( $items ) { | |
| 106 | - if ( $items instanceof Collection ) { | |
| 107 | - $items = $items->all(); | |
| 108 | - } | |
| 109 | - | |
| 110 | - return new static( array_replace_recursive( $this->items, $items ) ); | |
| 111 | - } | |
| 112 | - | |
| 113 | - /** | |
| 114 | - * Implode the items | |
| 115 | - * | |
| 116 | - * @param $glue | |
| 117 | - * | |
| 118 | - * @return string | |
| 119 | - */ | |
| 120 | - public function implode( $glue ) { | |
| 121 | - return implode( $glue, $this->items ); | |
| 122 | - } | |
| 123 | - | |
| 124 | - /** | |
| 125 | - * Run a map over each of the items. | |
| 126 | - * | |
| 127 | - * @param callable $callback | |
| 128 | - * @return $this | |
| 129 | - */ | |
| 130 | - public function map( callable $callback ) { | |
| 131 | - $keys = array_keys( $this->items ); | |
| 132 | - | |
| 133 | - $items = array_map( $callback, $this->items, $keys ); | |
| 134 | - | |
| 135 | - return new static( array_combine( $keys, $items ) ); | |
| 136 | - } | |
| 137 | - | |
| 138 | - /** | |
| 139 | - * Run a callback over each of the items. | |
| 140 | - * | |
| 141 | 56 | * @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 | - * @param null $initial | |
| 157 | 57 | * |
| 158 | - * @return mixed|null | |
| 159 | - */ | |
| 160 | - public function reduce( callable $callback, $initial = null ) { | |
| 161 | - $result = $initial; | |
| 162 | - | |
| 163 | - foreach ( $this->all() as $key => $value ) { | |
| 164 | - $result = $callback( $result, $value, $key ); | |
| 165 | - } | |
| 166 | - | |
| 167 | - return $result; | |
| 168 | - } | |
| 169 | - | |
| 170 | - public function reverse() { | |
| 171 | - return new static( array_reverse( $this->items ) ); | |
| 172 | - } | |
| 173 | - | |
| 174 | - /** | |
| 175 | - * @param callable $callback | |
| 176 | - * | |
| 177 | 58 | * @return $this |
| 178 | 59 | */ |
| 179 | 60 | public function map_with_keys( callable $callback ) { |
| 180 | 61 | $result = []; |
| @@ -203,209 +84,12 @@ | ||
| 203 | 84 | } ); |
| 204 | 85 | } |
| 205 | 86 | |
| 206 | 87 | /** |
| 207 | - * Get the items with the specified keys. | |
| 208 | - * | |
| 209 | - * @param array $keys | |
| 210 | - * | |
| 211 | - * @return $this | |
| 88 | + * @return array | |
| 212 | 89 | */ |
| 213 | - public function only( array $keys ) { | |
| 214 | - return $this->filter( function ( $value, $key ) use ( $keys ) { | |
| 215 | - return in_array( $key, $keys, true ); | |
| 216 | - } ); | |
| 217 | - } | |
| 218 | - | |
| 219 | - /** | |
| 220 | - * Run over the collection to get specific prop from the collection item. | |
| 221 | - * | |
| 222 | - * @param $key | |
| 223 | - * | |
| 224 | - * @return $this | |
| 225 | - */ | |
| 226 | - public function pluck( $key ) { | |
| 227 | - $result = []; | |
| 228 | - | |
| 229 | - foreach ( $this->items as $item ) { | |
| 230 | - $result[] = $this->get_item_value( $item, $key ); | |
| 231 | - } | |
| 232 | - | |
| 233 | - return new static( $result ); | |
| 234 | - } | |
| 235 | - | |
| 236 | - /** | |
| 237 | - * Group the collection items by specific key in each collection item. | |
| 238 | - * | |
| 239 | - * @param $group_by | |
| 240 | - * | |
| 241 | - * @return $this | |
| 242 | - */ | |
| 243 | - public function group_by( $group_by ) { | |
| 244 | - $result = []; | |
| 245 | - | |
| 246 | - foreach ( $this->items as $item ) { | |
| 247 | - $group_key = $this->get_item_value( $item, $group_by, 0 ); | |
| 248 | - | |
| 249 | - $result[ $group_key ][] = $item; | |
| 250 | - } | |
| 251 | - | |
| 252 | - return new static( $result ); | |
| 253 | - } | |
| 254 | - | |
| 255 | - /** | |
| 256 | - * Sort keys | |
| 257 | - * | |
| 258 | - * @param false $descending | |
| 259 | - * | |
| 260 | - * @return $this | |
| 261 | - */ | |
| 262 | - public function sort_keys( $descending = false ) { | |
| 263 | - $items = $this->items; | |
| 264 | - | |
| 265 | - if ( $descending ) { | |
| 266 | - krsort( $items ); | |
| 267 | - } else { | |
| 268 | - ksort( $items ); | |
| 269 | - } | |
| 270 | - | |
| 271 | - return new static( $items ); | |
| 272 | - } | |
| 273 | - | |
| 274 | - /** | |
| 275 | - * Get specific item from the collection. | |
| 276 | - * | |
| 277 | - * @param $key | |
| 278 | - * @param null $fallback | |
| 279 | - * | |
| 280 | - * @return mixed|null | |
| 281 | - */ | |
| 282 | - public function get( $key, $fallback = null ) { | |
| 283 | - if ( ! array_key_exists( $key, $this->items ) ) { | |
| 284 | - return $fallback; | |
| 285 | - } | |
| 286 | - | |
| 287 | - return $this->items[ $key ]; | |
| 288 | - } | |
| 289 | - | |
| 290 | - /** | |
| 291 | - * Get the first item. | |
| 292 | - * | |
| 293 | - * @param null $fallback | |
| 294 | - * | |
| 295 | - * @return mixed|null | |
| 296 | - */ | |
| 297 | - public function first( $fallback = null ) { | |
| 298 | - if ( $this->is_empty() ) { | |
| 299 | - return $fallback; | |
| 300 | - } | |
| 301 | - | |
| 302 | - foreach ( $this->items as $item ) { | |
| 303 | - return $item; | |
| 304 | - } | |
| 305 | - } | |
| 306 | - | |
| 307 | - /** | |
| 308 | - * Find an element from the items. | |
| 309 | - * | |
| 310 | - * @param callable $callback | |
| 311 | - * @param null $fallback | |
| 312 | - * | |
| 313 | - * @return mixed|null | |
| 314 | - */ | |
| 315 | - public function find( callable $callback, $fallback = null ) { | |
| 316 | - foreach ( $this->all() as $key => $item ) { | |
| 317 | - if ( $callback( $item, $key ) ) { | |
| 318 | - return $item; | |
| 319 | - } | |
| 320 | - } | |
| 321 | - | |
| 322 | - return $fallback; | |
| 323 | - } | |
| 324 | - | |
| 325 | - /** | |
| 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 | - * Make sure all the values inside the array are uniques. | |
| 363 | - * | |
| 364 | - * @param null|string|string[] $keys | |
| 365 | - * | |
| 366 | - * @return $this | |
| 367 | - */ | |
| 368 | - public function unique( $keys = null ) { | |
| 369 | - if ( ! $keys ) { | |
| 370 | - return new static( | |
| 371 | - array_unique( $this->items ) | |
| 372 | - ); | |
| 373 | - } | |
| 374 | - | |
| 375 | - if ( ! is_array( $keys ) ) { | |
| 376 | - $keys = [ $keys ]; | |
| 377 | - } | |
| 378 | - | |
| 379 | - $exists = []; | |
| 380 | - | |
| 381 | - return $this->filter( function ( $item ) use ( $keys, &$exists ) { | |
| 382 | - $value = null; | |
| 383 | - | |
| 384 | - foreach ( $keys as $key ) { | |
| 385 | - $current_value = $this->get_item_value( $item, $key ); | |
| 386 | - | |
| 387 | - $value .= "{$key}:{$current_value};"; | |
| 388 | - } | |
| 389 | - | |
| 390 | - // If no value for the specific key return the item. | |
| 391 | - if ( null === $value ) { | |
| 392 | - return true; | |
| 393 | - } | |
| 394 | - | |
| 395 | - // If value is not exists, add to the exists array and return the item. | |
| 396 | - if ( ! in_array( $value, $exists, true ) ) { | |
| 397 | - $exists[] = $value; | |
| 398 | - | |
| 399 | - return true; | |
| 400 | - } | |
| 401 | - | |
| 402 | - return false; | |
| 403 | - } ); | |
| 404 | - } | |
| 405 | - | |
| 406 | 90 | public function keys() { |
| 407 | - return new static( array_keys( $this->items ) ); | |
| 91 | + return array_keys( $this->items ); | |
| 408 | 92 | } |
| 409 | 93 | |
| 410 | 94 | /** |
| 411 | 95 | * @return bool |
| @@ -421,127 +105,47 @@ | ||
| 421 | 105 | return $this->items; |
| 422 | 106 | } |
| 423 | 107 | |
| 424 | 108 | /** |
| 425 | - * @return array | |
| 426 | - */ | |
| 427 | - public function values() { | |
| 428 | - return array_values( $this->all() ); | |
| 429 | - } | |
| 430 | - | |
| 431 | - /** | |
| 432 | - * Support only one level depth. | |
| 109 | + * @param mixed $key | |
| 433 | 110 | * |
| 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 | 111 | * @return bool |
| 503 | 112 | */ |
| 504 | - #[\ReturnTypeWillChange] | |
| 505 | - public function offsetExists( $offset ) { | |
| 506 | - return isset( $this->items[ $offset ] ); | |
| 113 | + public function offsetExists( $key ) { | |
| 114 | + return isset( $this->items[ $key ] ); | |
| 507 | 115 | } |
| 508 | 116 | |
| 509 | 117 | /** |
| 510 | - * @param mixed $offset | |
| 118 | + * @param mixed $key | |
| 511 | 119 | * |
| 512 | 120 | * @return mixed |
| 513 | 121 | */ |
| 514 | - #[\ReturnTypeWillChange] | |
| 515 | - public function offsetGet( $offset ) { | |
| 516 | - return $this->items[ $offset ]; | |
| 122 | + public function offsetGet( $key ) { | |
| 123 | + return $this->items[ $key ]; | |
| 517 | 124 | } |
| 518 | 125 | |
| 519 | 126 | /** |
| 520 | - * @param mixed $offset | |
| 127 | + * @param mixed $key | |
| 521 | 128 | * @param mixed $value |
| 522 | 129 | */ |
| 523 | - #[\ReturnTypeWillChange] | |
| 524 | - public function offsetSet( $offset, $value ) { | |
| 525 | - if ( is_null( $offset ) ) { | |
| 130 | + public function offsetSet( $key, $value ) { | |
| 131 | + if ( is_null( $key ) ) { | |
| 526 | 132 | $this->items[] = $value; |
| 527 | 133 | } else { |
| 528 | - $this->items[ $offset ] = $value; | |
| 134 | + $this->items[ $key ] = $value; | |
| 529 | 135 | } |
| 530 | 136 | } |
| 531 | 137 | |
| 532 | 138 | /** |
| 533 | - * @param mixed $offset | |
| 139 | + * @param mixed $key | |
| 534 | 140 | */ |
| 535 | - #[\ReturnTypeWillChange] | |
| 536 | - public function offsetUnset( $offset ) { | |
| 537 | - unset( $this->items[ $offset ] ); | |
| 141 | + public function offsetUnset( $key ) { | |
| 142 | + unset( $this->items[ $key ] ); | |
| 538 | 143 | } |
| 539 | 144 | |
| 540 | 145 | /** |
| 541 | 146 | * @return \ArrayIterator|\Traversable |
| 542 | 147 | */ |
| 543 | - #[\ReturnTypeWillChange] | |
| 544 | 148 | public function getIterator() { |
| 545 | 149 | return new \ArrayIterator( $this->items ); |
| 546 | 150 | } |
| 547 | 151 | |
| @@ -547,28 +151,8 @@ | ||
| 547 | 151 | |
| 548 | 152 | /** |
| 549 | 153 | * @return int|void |
| 550 | 154 | */ |
| 551 | - #[\ReturnTypeWillChange] | |
| 552 | 155 | public function count() { |
| 553 | 156 | return count( $this->items ); |
| 554 | - } | |
| 555 | - | |
| 556 | - /** | |
| 557 | - * @param $item | |
| 558 | - * @param $key | |
| 559 | - * @param null $fallback | |
| 560 | - * | |
| 561 | - * @return mixed|null | |
| 562 | - */ | |
| 563 | - private function get_item_value( $item, $key, $fallback = null ) { | |
| 564 | - $value = $fallback; | |
| 565 | - | |
| 566 | - if ( is_object( $item ) && isset( $item->{$key} ) ) { | |
| 567 | - $value = $item->{$key}; | |
| 568 | - } elseif ( is_array( $item ) && isset( $item[ $key ] ) ) { | |
| 569 | - $value = $item[ $key ]; | |
| 570 | - } | |
| 571 | - | |
| 572 | - return $value; | |
| 573 | 157 | } |
| 574 | 158 | } |