array-continue-exception.php
3 years ago
array-convert-once.php
3 years ago
array-tools.php
3 years ago
arrayable-once.php
3 years ago
arrayable.php
3 years ago
collection-item-interface.php
3 years ago
collection.php
3 years ago
collection.php
244 lines
| 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 | } |