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