PluginProbe
Elementor Website Builder – more than just a page builder / 3.1.0-dev2
Elementor Website Builder – more than just a page builder v3.1.0-dev2
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.1.0-dev2, at core/utils/collection.php

159 lines 2.7 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 * @param callable $callback
57 *
58 * @return $this
59 */
60 public function map_with_keys( callable $callback ) {
61 $result = [];
62
63 foreach ( $this->items as $key => $value ) {
64 $assoc = $callback( $value, $key );
65
66 foreach ( $assoc as $map_key => $map_value ) {
67 $result[ $map_key ] = $map_value;
68 }
69 }
70
71 return new static( $result );
72 }
73
74 /**
75 * Get all items except for those with the specified keys.
76 *
77 * @param array $keys
78 *
79 * @return $this
80 */
81 public function except( array $keys ) {
82 return $this->filter( function ( $value, $key ) use ( $keys ) {
83 return ! in_array( $key, $keys, true );
84 } );
85 }
86
87 /**
88 * @return array
89 */
90 public function keys() {
91 return array_keys( $this->items );
92 }
93
94 /**
95 * @return bool
96 */
97 public function is_empty() {
98 return empty( $this->items );
99 }
100
101 /**
102 * @return array
103 */
104 public function all() {
105 return $this->items;
106 }
107
108 /**
109 * @param mixed $key
110 *
111 * @return bool
112 */
113 public function offsetExists( $key ) {
114 return isset( $this->items[ $key ] );
115 }
116
117 /**
118 * @param mixed $key
119 *
120 * @return mixed
121 */
122 public function offsetGet( $key ) {
123 return $this->items[ $key ];
124 }
125
126 /**
127 * @param mixed $key
128 * @param mixed $value
129 */
130 public function offsetSet( $key, $value ) {
131 if ( is_null( $key ) ) {
132 $this->items[] = $value;
133 } else {
134 $this->items[ $key ] = $value;
135 }
136 }
137
138 /**
139 * @param mixed $key
140 */
141 public function offsetUnset( $key ) {
142 unset( $this->items[ $key ] );
143 }
144
145 /**
146 * @return \ArrayIterator|\Traversable
147 */
148 public function getIterator() {
149 return new \ArrayIterator( $this->items );
150 }
151
152 /**
153 * @return int|void
154 */
155 public function count() {
156 return count( $this->items );
157 }
158 }
159