PluginProbe
Elementor Website Builder – more than just a page builder / 3.2.3
Elementor Website Builder – more than just a page builder v3.2.3
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
← All changes | core/utils/collection.php +44 -310 4.2.23.2.3 View file →
@@ -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,19 +52,8 @@
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 56 * Merge array recursively
85 57 *
86 58 * @param $items
87 59 *
@@ -95,23 +67,8 @@
95 67 return new static( array_merge_recursive( $this->items, $items ) );
96 68 }
97 69
98 70 /**
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 71 * Implode the items
115 72 *
116 73 * @param $glue
117 74 *
@@ -123,9 +80,9 @@
123 80
124 81 /**
125 82 * Run a map over each of the items.
126 83 *
127 - * @param callable $callback
84 + * @param callable $callback
128 85 * @return $this
129 86 */
130 87 public function map( callable $callback ) {
131 88 $keys = array_keys( $this->items );
@@ -135,46 +92,10 @@
135 92 return new static( array_combine( $keys, $items ) );
136 93 }
137 94
138 95 /**
139 - * Run a callback over each of the items.
140 - *
141 96 * @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 97 *
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 98 * @return $this
178 99 */
179 100 public function map_with_keys( callable $callback ) {
180 101 $result = [];
@@ -225,10 +146,14 @@
225 146 */
226 147 public function pluck( $key ) {
227 148 $result = [];
228 149
229 - foreach ( $this->items as $item ) {
230 - $result[] = $this->get_item_value( $item, $key );
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 + }
231 156 }
232 157
233 158 return new static( $result );
234 159 }
@@ -242,12 +167,18 @@
242 167 */
243 168 public function group_by( $group_by ) {
244 169 $result = [];
245 170
246 - foreach ( $this->items as $item ) {
247 - $group_key = $this->get_item_value( $item, $group_by, 0 );
171 + foreach ( $this->items as $value ) {
172 + $group_key = 0;
248 173
249 - $result[ $group_key ][] = $item;
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;
250 181 }
251 182
252 183 return new static( $result );
253 184 }
@@ -252,37 +183,18 @@
252 183 return new static( $result );
253 184 }
254 185
255 186 /**
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 187 * Get specific item from the collection.
276 188 *
277 189 * @param $key
278 - * @param null $fallback
190 + * @param null $default
279 191 *
280 192 * @return mixed|null
281 193 */
282 - public function get( $key, $fallback = null ) {
194 + public function get( $key, $default = null ) {
283 195 if ( ! array_key_exists( $key, $this->items ) ) {
284 - return $fallback;
196 + return $default;
285 197 }
286 198
287 199 return $this->items[ $key ];
288 200 }
@@ -289,15 +201,15 @@
289 201
290 202 /**
291 203 * Get the first item.
292 204 *
293 - * @param null $fallback
205 + * @param null $default
294 206 *
295 207 * @return mixed|null
296 208 */
297 - public function first( $fallback = null ) {
209 + public function first( $default = null ) {
298 210 if ( $this->is_empty() ) {
299 - return $fallback;
211 + return $default;
300 212 }
301 213
302 214 foreach ( $this->items as $item ) {
303 215 return $item;
@@ -304,108 +216,23 @@
304 216 }
305 217 }
306 218
307 219 /**
308 - * Find an element from the items.
220 + * Make sure all the values inside the array are uniques.
309 221 *
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 222 * @return $this
352 223 */
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 ) );
224 + public function unique() {
225 + return new static(
226 + array_unique( $this->items )
227 + );
359 228 }
360 229
361 230 /**
362 - * Make sure all the values inside the array are uniques.
363 - *
364 - * @param null|string|string[] $keys
365 - *
366 - * @return $this
231 + * @return array
367 232 */
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 233 public function keys() {
407 - return new static( array_keys( $this->items ) );
234 + return array_keys( $this->items );
408 235 }
409 236
410 237 /**
411 238 * @return bool
@@ -428,120 +255,47 @@
428 255 return array_values( $this->all() );
429 256 }
430 257
431 258 /**
432 - * Support only one level depth.
259 + * @param mixed $key
433 260 *
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 261 * @return bool
503 262 */
504 - #[\ReturnTypeWillChange]
505 - public function offsetExists( $offset ) {
506 - return isset( $this->items[ $offset ] );
263 + public function offsetExists( $key ) {
264 + return isset( $this->items[ $key ] );
507 265 }
508 266
509 267 /**
510 - * @param mixed $offset
268 + * @param mixed $key
511 269 *
512 270 * @return mixed
513 271 */
514 - #[\ReturnTypeWillChange]
515 - public function offsetGet( $offset ) {
516 - return $this->items[ $offset ];
272 + public function offsetGet( $key ) {
273 + return $this->items[ $key ];
517 274 }
518 275
519 276 /**
520 - * @param mixed $offset
277 + * @param mixed $key
521 278 * @param mixed $value
522 279 */
523 - #[\ReturnTypeWillChange]
524 - public function offsetSet( $offset, $value ) {
525 - if ( is_null( $offset ) ) {
280 + public function offsetSet( $key, $value ) {
281 + if ( is_null( $key ) ) {
526 282 $this->items[] = $value;
527 283 } else {
528 - $this->items[ $offset ] = $value;
284 + $this->items[ $key ] = $value;
529 285 }
530 286 }
531 287
532 288 /**
533 - * @param mixed $offset
289 + * @param mixed $key
534 290 */
535 - #[\ReturnTypeWillChange]
536 - public function offsetUnset( $offset ) {
537 - unset( $this->items[ $offset ] );
291 + public function offsetUnset( $key ) {
292 + unset( $this->items[ $key ] );
538 293 }
539 294
540 295 /**
541 296 * @return \ArrayIterator|\Traversable
542 297 */
543 - #[\ReturnTypeWillChange]
544 298 public function getIterator() {
545 299 return new \ArrayIterator( $this->items );
546 300 }
547 301
@@ -547,28 +301,8 @@
547 301
548 302 /**
549 303 * @return int|void
550 304 */
551 - #[\ReturnTypeWillChange]
552 305 public function count() {
553 306 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 307 }
574 308 }