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