| @@ -1,248 +1,244 @@ | ||
| 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 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | + | |
| 4 | +namespace Jet_Form_Builder\Classes\Arrayable; | |
| 5 | + | |
| 6 | + | |
| 7 | +use Jet_Form_Builder\Classes\Repository\Repository_Item_Dynamic_Id; | |
| 8 | + | |
| 9 | +class Collection implements \Iterator, \Countable, \ArrayAccess { | |
| 10 | + | |
| 11 | + protected $position = 0; | |
| 12 | + protected $items; | |
| 13 | + protected $groups = array(); | |
| 14 | + | |
| 15 | + public function __construct( array $items = array() ) { | |
| 16 | + $this->items = $items; | |
| 17 | + $this->group_items(); | |
| 18 | + } | |
| 19 | + | |
| 20 | + public function intersect( $list_of_states ): bool { | |
| 21 | + foreach ( $list_of_states as $state ) { | |
| 22 | + if ( $this->in_array( $state ) ) { | |
| 23 | + return true; | |
| 24 | + } | |
| 25 | + } | |
| 26 | + | |
| 27 | + return false; | |
| 28 | + } | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * @param $state | |
| 32 | + * | |
| 33 | + * @return bool | |
| 34 | + */ | |
| 35 | + public function in_array( $state ): bool { | |
| 36 | + if ( is_object( $state ) ) { | |
| 37 | + if ( $state instanceof Repository_Item_Dynamic_Id ) { | |
| 38 | + return $this->in_array_by_dynamic( $state ); | |
| 39 | + } | |
| 40 | + $state = get_class( $state ); | |
| 41 | + } | |
| 42 | + | |
| 43 | + if ( class_exists( $state ) ) { | |
| 44 | + return $this->in_array_by_class( $state ); | |
| 45 | + } | |
| 46 | + | |
| 47 | + return array_key_exists( $state, $this->groups ); | |
| 48 | + } | |
| 49 | + | |
| 50 | + protected function in_array_by_class( $state_class ): bool { | |
| 51 | + foreach ( $this as $state ) { | |
| 52 | + if ( is_a( $state, $state_class ) ) { | |
| 53 | + return true; | |
| 54 | + } | |
| 55 | + } | |
| 56 | + | |
| 57 | + return false; | |
| 58 | + } | |
| 59 | + | |
| 60 | + protected function in_array_by_dynamic( Repository_Item_Dynamic_Id $object ): bool { | |
| 61 | + foreach ( $this as $state ) { | |
| 62 | + if ( ! is_a( $state, Repository_Item_Dynamic_Id::class ) ) { | |
| 63 | + continue; | |
| 64 | + } | |
| 65 | + if ( $state->get_dynamic_id() === $object->get_dynamic_id() ) { | |
| 66 | + return true; | |
| 67 | + } | |
| 68 | + } | |
| 69 | + | |
| 70 | + return false; | |
| 71 | + } | |
| 72 | + | |
| 73 | + public function add( $item ): Collection { | |
| 74 | + $this->items[] = $item; | |
| 75 | + | |
| 76 | + if ( ! is_a( $item, Collection_Item_Interface::class ) ) { | |
| 77 | + return $this; | |
| 78 | + } | |
| 79 | + | |
| 80 | + return $this->add_to_group( $item ); | |
| 81 | + } | |
| 82 | + | |
| 83 | + public function replace( $item ): Collection { | |
| 84 | + if ( ! is_a( $item, Collection_Item_Interface::class ) ) { | |
| 85 | + return $this; | |
| 86 | + } | |
| 87 | + $this->items[] = $item; | |
| 88 | + $this->groups[ $item->get_id() ] = array(); | |
| 89 | + | |
| 90 | + return $this->add_to_group( $item ); | |
| 91 | + } | |
| 92 | + | |
| 93 | + public function delete( $position ): Collection { | |
| 94 | + /** @var Collection_Item_Interface $item */ | |
| 95 | + $item = $this->items[ $position ]; | |
| 96 | + | |
| 97 | + unset( $this->groups[ $item->get_id() ] ); | |
| 98 | + unset( $this->items[ $position ] ); | |
| 99 | + | |
| 100 | + return $this; | |
| 101 | + } | |
| 102 | + | |
| 103 | + public function has_by_id( string $id ): bool { | |
| 104 | + return $this->get_by_id( $id )->valid(); | |
| 105 | + } | |
| 106 | + | |
| 107 | + public function get_by_ids( array $ids ): \Generator { | |
| 108 | + foreach ( $ids as $id ) { | |
| 109 | + yield from $this->get_by_id( $id ); | |
| 110 | + } | |
| 111 | + } | |
| 112 | + | |
| 113 | + public function get_by_id( string $id ): \Generator { | |
| 114 | + $group = $this->groups[ $id ] ?? array(); | |
| 115 | + | |
| 116 | + foreach ( $group as $property ) { | |
| 117 | + yield $property; | |
| 118 | + } | |
| 119 | + } | |
| 120 | + | |
| 121 | + public function group_items() { | |
| 122 | + /** @var Collection_Item_Interface $item */ | |
| 123 | + foreach ( $this->items as $item ) { | |
| 124 | + if ( ! is_a( $item, Collection_Item_Interface::class ) ) { | |
| 125 | + continue; | |
| 126 | + } | |
| 127 | + $this->add_to_group( $item ); | |
| 128 | + } | |
| 129 | + } | |
| 130 | + | |
| 131 | + protected function add_to_group( $item ): Collection { | |
| 132 | + if ( ! isset( $this->groups[ $item->get_id() ] ) ) { | |
| 133 | + $this->groups[ $item->get_id() ] = array(); | |
| 134 | + } | |
| 135 | + $this->groups[ $item->get_id() ][] = $item; | |
| 136 | + | |
| 137 | + return $this; | |
| 138 | + } | |
| 139 | + | |
| 140 | + public function all(): array { | |
| 141 | + return $this->items; | |
| 142 | + } | |
| 143 | + | |
| 144 | + public function remove( ...$positions ) { | |
| 145 | + if ( is_array( $positions[0] ) ) { | |
| 146 | + $positions = $positions[0]; | |
| 147 | + } | |
| 148 | + foreach ( $positions as $position ) { | |
| 149 | + $this->delete( $position ); | |
| 150 | + } | |
| 151 | + $this->items = array_values( $this->items ); | |
| 152 | + } | |
| 153 | + | |
| 154 | + /* | |
| 155 | + * \Iterator | |
| 156 | + */ | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * @return mixed | |
| 160 | + */ | |
| 161 | + #[\ReturnTypeWillChange] | |
| 162 | + public function current() { | |
| 163 | + return $this->items[ $this->position ]; | |
| 164 | + } | |
| 165 | + | |
| 166 | + #[\ReturnTypeWillChange] | |
| 167 | + public function next() { | |
| 168 | + ++ $this->position; | |
| 169 | + } | |
| 170 | + | |
| 171 | + /** | |
| 172 | + * @return bool|float|int|string|null | |
| 173 | + */ | |
| 174 | + #[\ReturnTypeWillChange] | |
| 175 | + public function key() { | |
| 176 | + return $this->position; | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 180 | + * @return bool | |
| 181 | + */ | |
| 182 | + public function valid(): bool { | |
| 183 | + return isset( $this->items[ $this->position ] ); | |
| 184 | + } | |
| 185 | + | |
| 186 | + #[\ReturnTypeWillChange] | |
| 187 | + public function rewind() { | |
| 188 | + $this->position = 0; | |
| 189 | + } | |
| 190 | + | |
| 191 | + /* | |
| 192 | + * \ArrayAccess | |
| 193 | + */ | |
| 194 | + | |
| 195 | + /** | |
| 196 | + * @param mixed $offset | |
| 197 | + * | |
| 198 | + * @return bool | |
| 199 | + */ | |
| 200 | + public function offsetExists( $offset ): bool { | |
| 201 | + return isset( $this->items[ $offset ] ); | |
| 202 | + } | |
| 203 | + | |
| 204 | + /** | |
| 205 | + * @param mixed $offset | |
| 206 | + * | |
| 207 | + * @return mixed | |
| 208 | + */ | |
| 209 | + #[\ReturnTypeWillChange] | |
| 210 | + public function offsetGet( $offset ) { | |
| 211 | + return $this->items[ $offset ] ?? null; | |
| 212 | + } | |
| 213 | + | |
| 214 | + /** | |
| 215 | + * @param mixed $offset | |
| 216 | + * @param mixed $value | |
| 217 | + */ | |
| 218 | + #[\ReturnTypeWillChange] | |
| 219 | + public function offsetSet( $offset, $value ) { | |
| 220 | + if ( is_null( $offset ) ) { | |
| 221 | + $this->items[] = $value; | |
| 222 | + } else { | |
| 223 | + $this->items[ $offset ] = $value; | |
| 224 | + } | |
| 225 | + } | |
| 226 | + | |
| 227 | + /** | |
| 228 | + * @param mixed $offset | |
| 229 | + */ | |
| 230 | + #[\ReturnTypeWillChange] | |
| 231 | + public function offsetUnset( $offset ) { | |
| 232 | + $this->delete( $offset ); | |
| 233 | + $this->items = array_values( $this->items ); | |
| 234 | + } | |
| 235 | + | |
| 236 | + /* | |
| 237 | + * \Countable | |
| 238 | + */ | |
| 239 | + | |
| 240 | + public function count(): int { | |
| 241 | + return count( $this->items ); | |
| 242 | + } | |
| 243 | + | |
| 244 | +} | |