PluginProbe
Elementor Website Builder – more than just a page builder / 3.2.4
Elementor Website Builder – more than just a page builder v3.2.4
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / utils / collection.php

collection.php in Elementor Website Builder – more than just a page builder 3.2.4, at core/utils/collection.php

309 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Run over the collection to get specific prop from the collection item.
142 *
143 * @param $key
144 *
145 * @return $this
146 */
147 public function pluck( $key ) {
148 $result = [];
149
150 foreach ( $this->items as $value ) {
151 if ( is_object( $value ) && isset( $value->{$key} ) ) {
152 $result[] = $value->{$key};
153 } elseif ( is_array( $value ) && isset( $value[ $key ] ) ) {
154 $result[] = $value[ $key ];
155 }
156 }
157
158 return new static( $result );
159 }
160
161 /**
162 * Group the collection items by specific key in each collection item.
163 *
164 * @param $group_by
165 *
166 * @return $this
167 */
168 public function group_by( $group_by ) {
169 $result = [];
170
171 foreach ( $this->items as $value ) {
172 $group_key = 0;
173
174 if ( is_object( $value ) && isset( $value->{$group_by} ) ) {
175 $group_key = $value->{$group_by};
176 } elseif ( is_array( $value ) && isset( $value[ $group_by ] ) ) {
177 $group_key = $value[ $group_by ];
178 }
179
180 $result[ $group_key ][] = $value;
181 }
182
183 return new static( $result );
184 }
185
186 /**
187 * Get specific item from the collection.
188 *
189 * @param $key
190 * @param null $default
191 *
192 * @return mixed|null
193 */
194 public function get( $key, $default = null ) {
195 if ( ! array_key_exists( $key, $this->items ) ) {
196 return $default;
197 }
198
199 return $this->items[ $key ];
200 }
201
202 /**
203 * Get the first item.
204 *
205 * @param null $default
206 *
207 * @return mixed|null
208 */
209 public function first( $default = null ) {
210 if ( $this->is_empty() ) {
211 return $default;
212 }
213
214 foreach ( $this->items as $item ) {
215 return $item;
216 }
217 }
218
219 /**
220 * Make sure all the values inside the array are uniques.
221 *
222 * @return $this
223 */
224 public function unique() {
225 return new static(
226 array_unique( $this->items )
227 );
228 }
229
230 /**
231 * @return array
232 */
233 public function keys() {
234 return array_keys( $this->items );
235 }
236
237 /**
238 * @return bool
239 */
240 public function is_empty() {
241 return empty( $this->items );
242 }
243
244 /**
245 * @return array
246 */
247 public function all() {
248 return $this->items;
249 }
250
251 /**
252 * @return array
253 */
254 public function values() {
255 return array_values( $this->all() );
256 }
257
258 /**
259 * @param mixed $key
260 *
261 * @return bool
262 */
263 public function offsetExists( $key ) {
264 return isset( $this->items[ $key ] );
265 }
266
267 /**
268 * @param mixed $key
269 *
270 * @return mixed
271 */
272 public function offsetGet( $key ) {
273 return $this->items[ $key ];
274 }
275
276 /**
277 * @param mixed $key
278 * @param mixed $value
279 */
280 public function offsetSet( $key, $value ) {
281 if ( is_null( $key ) ) {
282 $this->items[] = $value;
283 } else {
284 $this->items[ $key ] = $value;
285 }
286 }
287
288 /**
289 * @param mixed $key
290 */
291 public function offsetUnset( $key ) {
292 unset( $this->items[ $key ] );
293 }
294
295 /**
296 * @return \ArrayIterator|\Traversable
297 */
298 public function getIterator() {
299 return new \ArrayIterator( $this->items );
300 }
301
302 /**
303 * @return int|void
304 */
305 public function count() {
306 return count( $this->items );
307 }
308 }
309