* Inspired by Laravel Collection.
* @link https://github.com/illuminate/collections
* @package Elementor\Core\Utils
namespace Elementor\Core\Utils;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
* Inspired by Laravel Collection.
* @link https://github.com/illuminate/collections
class Collection implements \ArrayAccess, \Countable, \IteratorAggregate {
* The items contained in the collection.
* Collection constructor.
public function __construct( array $items = [] ) {
public static function make( array $items = [] ) {
return new static( $items );
* @param callable|null $callback
public function filter( ?callable $callback = null ) {
return new static( array_filter( $this->items ) );
return new static( array_filter( $this->items, $callback, ARRAY_FILTER_USE_BOTH ) );
public function merge( $items ) {
if ( $items instanceof Collection ) {
return new static( array_merge( $this->items, $items ) );
* Union the collection with the given items.
public function union( array $items ) {
return new static( $this->all() + $items );
* Merge array recursively
public function merge_recursive( $items ) {
if ( $items instanceof Collection ) {
return new static( array_merge_recursive( $this->items, $items ) );
* Replace array recursively
public function replace_recursive( $items ) {
if ( $items instanceof Collection ) {
return new static( array_replace_recursive( $this->items, $items ) );
public function implode( $glue ) {
return implode( $glue, $this->items );
* Run a map over each of the items.
* @param callable $callback
public function map( callable $callback ) {
$keys = array_keys( $this->items );
$items = array_map( $callback, $this->items, $keys );
return new static( array_combine( $keys, $items ) );
* Run a callback over each of the items.
* @param callable $callback
public function each( callable $callback ) {
foreach ( $this->items as $key => $value ) {
if ( false === $callback( $value, $key ) ) {
* @param callable $callback
public function reduce( callable $callback, $initial = null ) {
foreach ( $this->all() as $key => $value ) {
$result = $callback( $result, $value, $key );
public function reverse() {
return new static( array_reverse( $this->items ) );
* @param callable $callback
public function map_with_keys( callable $callback ) {
foreach ( $this->items as $key => $value ) {
$assoc = $callback( $value, $key );
foreach ( $assoc as $map_key => $map_value ) {
$result[ $map_key ] = $map_value;
return new static( $result );
* Get all items except for those with the specified keys.
public function except( array $keys ) {
return $this->filter( function ( $value, $key ) use ( $keys ) {
return ! in_array( $key, $keys, true );
* Get the items with the specified keys.
public function only( array $keys ) {
return $this->filter( function ( $value, $key ) use ( $keys ) {
return in_array( $key, $keys, true );
* Run over the collection to get specific prop from the collection item.
public function pluck( $key ) {
foreach ( $this->items as $item ) {
$result[] = $this->get_item_value( $item, $key );
return new static( $result );
* Group the collection items by specific key in each collection item.
public function group_by( $group_by ) {
foreach ( $this->items as $item ) {
$group_key = $this->get_item_value( $item, $group_by, 0 );
$result[ $group_key ][] = $item;
return new static( $result );
* @param false $descending
public function sort_keys( $descending = false ) {
return new static( $items );
* Get specific item from the collection.
public function get( $key, $fallback = null ) {
if ( ! array_key_exists( $key, $this->items ) ) {
return $this->items[ $key ];
public function first( $fallback = null ) {
if ( $this->is_empty() ) {
foreach ( $this->items as $item ) {
* Find an element from the items.
* @param callable $callback
public function find( callable $callback, $fallback = null ) {
foreach ( $this->all() as $key => $item ) {
if ( $callback( $item, $key ) ) {
* @param callable|string|int $value
public function contains( $value ) {
$callback = $value instanceof \Closure
: function ( $item ) use ( $value ) {
foreach ( $this->all() as $key => $item ) {
if ( $callback( $item, $key ) ) {
* Run array_diff between the collection and other array or collection.
public function diff( $filter ) {
if ( $filter instanceof self ) {
$filter = $filter->all();
return new static( array_diff( $this->all(), $filter ) );
* Make sure all the values inside the array are uniques.
* @param null|string|string[] $keys
public function unique( $keys = null ) {
array_unique( $this->items )
if ( ! is_array( $keys ) ) {
return $this->filter( function ( $item ) use ( $keys, &$exists ) {
foreach ( $keys as $key ) {
$current_value = $this->get_item_value( $item, $key );
$value .= "{$key}:{$current_value};";
// If no value for the specific key return the item.
// If value is not exists, add to the exists array and return the item.
if ( ! in_array( $value, $exists, true ) ) {
return new static( array_keys( $this->items ) );
public function is_empty() {
return empty( $this->items );
public function values() {
return array_values( $this->all() );
* Support only one level depth.
public function flatten() {
foreach ( $this->all() as $item ) {
$item = $item instanceof Collection ? $item->all() : $item;
if ( ! is_array( $item ) ) {
$values = array_values( $item );
foreach ( $values as $value ) {
return new static( $result );
return new static( array_flip( $this->items ) );
* @param array ...$values
public function push( ...$values ) {
foreach ( $values as $value ) {
public function prepend( ...$values ) {
$this->items = array_merge( $values, $this->items );
public function some( callable $callback ) {
foreach ( $this->items as $key => $item ) {
if ( $callback( $item, $key ) ) {
public function every( callable $callback ) {
foreach ( $this->items as $key => $item ) {
if ( ! $callback( $item, $key ) ) {