| 1 |
<?php |
| 2 |
/** |
| 3 |
* Inspired by Laravel Collection. |
| 4 |
* @link https://github.com/illuminate/collections |
| 5 |
*/ |
| 6 |
namespace Elementor\Core\Utils; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
class Collection implements \ArrayAccess, \Countable, \IteratorAggregate { |
| 13 |
/** |
| 14 |
* The items contained in the collection. |
| 15 |
* |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
protected $items; |
| 19 |
|
| 20 |
/** |
| 21 |
* Collection constructor. |
| 22 |
* |
| 23 |
* @param array $items |
| 24 |
*/ |
| 25 |
public function __construct( array $items ) { |
| 26 |
$this->items = $items; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @param callable|null $callback |
| 31 |
* |
| 32 |
* @return $this |
| 33 |
*/ |
| 34 |
public function filter( callable $callback = null ) { |
| 35 |
if ( ! $callback ) { |
| 36 |
return new static( array_filter( $this->items ) ); |
| 37 |
} |
| 38 |
|
| 39 |
return new static( array_filter( $this->items, $callback, ARRAY_FILTER_USE_BOTH ) ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @param $items |
| 44 |
* |
| 45 |
* @return $this |
| 46 |
*/ |
| 47 |
public function merge( $items ) { |
| 48 |
if ( $items instanceof Collection ) { |
| 49 |
$items = $items->all(); |
| 50 |
} |
| 51 |
|
| 52 |
return new static( array_merge( $this->items, $items ) ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Merge array recursively |
| 57 |
* |
| 58 |
* @param $items |
| 59 |
* |
| 60 |
* @return $this |
| 61 |
*/ |
| 62 |
public function merge_recursive( $items ) { |
| 63 |
if ( $items instanceof Collection ) { |
| 64 |
$items = $items->all(); |
| 65 |
} |
| 66 |
|
| 67 |
return new static( array_merge_recursive( $this->items, $items ) ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Implode the items |
| 72 |
* |
| 73 |
* @param $glue |
| 74 |
* |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
public function implode( $glue ) { |
| 78 |
return implode( $glue, $this->items ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Run a map over each of the items. |
| 83 |
* |
| 84 |
* @param callable $callback |
| 85 |
* @return $this |
| 86 |
*/ |
| 87 |
public function map( callable $callback ) { |
| 88 |
$keys = array_keys( $this->items ); |
| 89 |
|
| 90 |
$items = array_map( $callback, $this->items, $keys ); |
| 91 |
|
| 92 |
return new static( array_combine( $keys, $items ) ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @param callable $callback |
| 97 |
* |
| 98 |
* @return $this |
| 99 |
*/ |
| 100 |
public function map_with_keys( callable $callback ) { |
| 101 |
$result = []; |
| 102 |
|
| 103 |
foreach ( $this->items as $key => $value ) { |
| 104 |
$assoc = $callback( $value, $key ); |
| 105 |
|
| 106 |
foreach ( $assoc as $map_key => $map_value ) { |
| 107 |
$result[ $map_key ] = $map_value; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return new static( $result ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Get all items except for those with the specified keys. |
| 116 |
* |
| 117 |
* @param array $keys |
| 118 |
* |
| 119 |
* @return $this |
| 120 |
*/ |
| 121 |
public function except( array $keys ) { |
| 122 |
return $this->filter( function ( $value, $key ) use ( $keys ) { |
| 123 |
return ! in_array( $key, $keys, true ); |
| 124 |
} ); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get the items with the specified keys. |
| 129 |
* |
| 130 |
* @param array $keys |
| 131 |
* |
| 132 |
* @return $this |
| 133 |
*/ |
| 134 |
public function only( array $keys ) { |
| 135 |
return $this->filter( function ( $value, $key ) use ( $keys ) { |
| 136 |
return in_array( $key, $keys, true ); |
| 137 |
} ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
public function keys() { |
| 144 |
return array_keys( $this->items ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* @return bool |
| 149 |
*/ |
| 150 |
public function is_empty() { |
| 151 |
return empty( $this->items ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @return array |
| 156 |
*/ |
| 157 |
public function all() { |
| 158 |
return $this->items; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* @param mixed $key |
| 163 |
* |
| 164 |
* @return bool |
| 165 |
*/ |
| 166 |
public function offsetExists( $key ) { |
| 167 |
return isset( $this->items[ $key ] ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* @param mixed $key |
| 172 |
* |
| 173 |
* @return mixed |
| 174 |
*/ |
| 175 |
public function offsetGet( $key ) { |
| 176 |
return $this->items[ $key ]; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* @param mixed $key |
| 181 |
* @param mixed $value |
| 182 |
*/ |
| 183 |
public function offsetSet( $key, $value ) { |
| 184 |
if ( is_null( $key ) ) { |
| 185 |
$this->items[] = $value; |
| 186 |
} else { |
| 187 |
$this->items[ $key ] = $value; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* @param mixed $key |
| 193 |
*/ |
| 194 |
public function offsetUnset( $key ) { |
| 195 |
unset( $this->items[ $key ] ); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* @return \ArrayIterator|\Traversable |
| 200 |
*/ |
| 201 |
public function getIterator() { |
| 202 |
return new \ArrayIterator( $this->items ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* @return int|void |
| 207 |
*/ |
| 208 |
public function count() { |
| 209 |
return count( $this->items ); |
| 210 |
} |
| 211 |
} |
| 212 |
|