PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.4.5
JetFormBuilder — Dynamic Blocks Form Builder v3.4.5
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / classes / arrayable / collection.php
jetformbuilder / includes / classes / arrayable Last commit date
array-continue-exception.php 2 years ago array-convert-once.php 2 years ago array-tools.php 2 years ago arrayable-once.php 2 years ago arrayable.php 2 years ago collection-item-interface.php 2 years ago collection.php 2 years ago
collection.php
249 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Classes\Arrayable;
5
6 use JFB_Components\Repository\Repository_Item_Dynamic_Id;
7
8 // If this file is called directly, abort.
9 if ( ! defined( 'WPINC' ) ) {
10 die;
11 }
12
13 class Collection implements \Iterator, \Countable, \ArrayAccess {
14
15 protected $position = 0;
16 protected $items;
17 protected $groups = array();
18
19 public function __construct( array $items = array() ) {
20 $this->items = $items;
21 $this->group_items();
22 }
23
24 public function intersect( $list_of_states ): bool {
25 foreach ( $list_of_states as $state ) {
26 if ( $this->in_array( $state ) ) {
27 return true;
28 }
29 }
30
31 return false;
32 }
33
34 /**
35 * @param $state
36 *
37 * @return bool
38 */
39 public function in_array( $state ): bool {
40 if ( is_object( $state ) ) {
41 if ( $state instanceof Repository_Item_Dynamic_Id ) {
42 return $this->in_array_by_dynamic( $state );
43 }
44 $state = get_class( $state );
45 }
46
47 if ( class_exists( $state ) ) {
48 return $this->in_array_by_class( $state );
49 }
50
51 return array_key_exists( $state, $this->groups );
52 }
53
54 protected function in_array_by_class( $state_class ): bool {
55 foreach ( $this as $state ) {
56 if ( is_a( $state, $state_class ) ) {
57 return true;
58 }
59 }
60
61 return false;
62 }
63
64 protected function in_array_by_dynamic( Repository_Item_Dynamic_Id $current ): bool {
65 foreach ( $this as $state ) {
66 if ( ! is_a( $state, Repository_Item_Dynamic_Id::class ) ) {
67 continue;
68 }
69 if ( $state->get_dynamic_id() === $current->get_dynamic_id() ) {
70 return true;
71 }
72 }
73
74 return false;
75 }
76
77 public function add( $item ): Collection {
78 $this->items[] = $item;
79
80 if ( ! is_a( $item, Collection_Item_Interface::class ) ) {
81 return $this;
82 }
83
84 return $this->add_to_group( $item );
85 }
86
87 public function replace( $item ): Collection {
88 if ( ! is_a( $item, Collection_Item_Interface::class ) ) {
89 return $this;
90 }
91 $this->items[] = $item;
92 $this->groups[ $item->get_id() ] = array();
93
94 return $this->add_to_group( $item );
95 }
96
97 public function delete( $position ): Collection {
98 /** @var Collection_Item_Interface $item */
99 $item = $this->items[ $position ];
100
101 unset( $this->groups[ $item->get_id() ] );
102 unset( $this->items[ $position ] );
103
104 return $this;
105 }
106
107 public function has_by_id( string $id ): bool {
108 return $this->get_by_id( $id )->valid();
109 }
110
111 public function get_by_ids( array $ids ): \Generator {
112 foreach ( $ids as $id ) {
113 yield from $this->get_by_id( $id );
114 }
115 }
116
117 public function get_by_id( string $id ): \Generator {
118 $group = $this->groups[ $id ] ?? array();
119
120 foreach ( $group as $property ) {
121 yield $property;
122 }
123 }
124
125 public function group_items() {
126 /** @var Collection_Item_Interface $item */
127 foreach ( $this->items as $item ) {
128 if ( ! is_a( $item, Collection_Item_Interface::class ) ) {
129 continue;
130 }
131 $this->add_to_group( $item );
132 }
133 }
134
135 protected function add_to_group( $item ): Collection {
136 if ( ! isset( $this->groups[ $item->get_id() ] ) ) {
137 $this->groups[ $item->get_id() ] = array();
138 }
139 $this->groups[ $item->get_id() ][] = $item;
140
141 return $this;
142 }
143
144 public function all(): array {
145 return $this->items;
146 }
147
148 public function remove( ...$positions ) {
149 if ( is_array( $positions[0] ) ) {
150 $positions = $positions[0];
151 }
152 foreach ( $positions as $position ) {
153 $this->delete( $position );
154 }
155 $this->items = array_values( $this->items );
156 }
157
158 /*
159 * \Iterator
160 */
161
162 /**
163 * @return mixed
164 */
165 #[\ReturnTypeWillChange]
166 public function current() {
167 return $this->items[ $this->position ];
168 }
169
170 #[\ReturnTypeWillChange]
171 public function next() {
172 ++$this->position;
173 }
174
175 /**
176 * @return bool|float|int|string|null
177 */
178 #[\ReturnTypeWillChange]
179 public function key() {
180 return $this->position;
181 }
182
183 /**
184 * @return bool
185 */
186 public function valid(): bool {
187 return isset( $this->items[ $this->position ] );
188 }
189
190 #[\ReturnTypeWillChange]
191 public function rewind() {
192 $this->position = 0;
193 }
194
195 /*
196 * \ArrayAccess
197 */
198
199 /**
200 * @param mixed $offset
201 *
202 * @return bool
203 */
204 public function offsetExists( $offset ): bool {
205 return isset( $this->items[ $offset ] );
206 }
207
208 /**
209 * @param mixed $offset
210 *
211 * @return mixed
212 */
213 #[\ReturnTypeWillChange]
214 public function offsetGet( $offset ) {
215 return $this->items[ $offset ] ?? null;
216 }
217
218 /**
219 * @param mixed $offset
220 * @param mixed $value
221 */
222 #[\ReturnTypeWillChange]
223 public function offsetSet( $offset, $value ) {
224 if ( is_null( $offset ) ) {
225 $this->items[] = $value;
226 } else {
227 $this->items[ $offset ] = $value;
228 }
229 }
230
231 /**
232 * @param mixed $offset
233 */
234 #[\ReturnTypeWillChange]
235 public function offsetUnset( $offset ) {
236 $this->delete( $offset );
237 $this->items = array_values( $this->items );
238 }
239
240 /*
241 * \Countable
242 */
243
244 public function count(): int {
245 return count( $this->items );
246 }
247
248 }
249