|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Abstract collection. |
| 4 | + * |
| 5 | + * @since 3.0.0 |
| 6 | + * |
| 7 | + * @package StellarWP\Schema\V3\Collections |
| 8 | + */ |
| 9 | + |
| 10 | +declare( strict_types=1 ); |
| 11 | + |
| 12 | +namespace StellarWP\Schema\V3\Collections; |
| 13 | + |
| 14 | +use ArrayAccess; |
| 15 | +use Countable; |
| 16 | +use Iterator; |
| 17 | +use JsonSerializable; |
| 18 | +use ReturnTypeWillChange; |
| 19 | + |
| 20 | +/** |
| 21 | + * Abstract collection. |
| 22 | + * |
| 23 | + * @since 3.0.0 |
| 24 | + * |
| 25 | + * @package StellarWP\Schema\V3\Collections |
| 26 | + */ |
| 27 | +abstract class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable { |
| 28 | + /** |
| 29 | + * Collection of items. |
| 30 | + * |
| 31 | + * @since 3.0.0 |
| 32 | + * |
| 33 | + * @var array |
| 34 | + */ |
| 35 | + protected array $resources = []; |
| 36 | + |
| 37 | + /** |
| 38 | + * Constructor. |
| 39 | + * |
| 40 | + * @since 3.0.0 |
| 41 | + * |
| 42 | + * @param array $resources An array of items. |
| 43 | + */ |
| 44 | + public function __construct( array $resources = [] ) { |
| 45 | + foreach ( $resources as $offset => $value ) { |
| 46 | + $this->set( (string) $offset, $value ); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Sets a value in the collection. |
| 52 | + * |
| 53 | + * @since 3.0.0 |
| 54 | + * |
| 55 | + * @param string $offset The offset to set. |
| 56 | + * @param mixed $value The value to set. |
| 57 | + */ |
| 58 | + protected function set( string $offset, $value ): void { |
| 59 | + $this->resources[ $offset ] = $value; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @inheritDoc |
| 64 | + */ |
| 65 | + #[ReturnTypeWillChange] |
| 66 | + public function current() { |
| 67 | + return current( $this->resources ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @inheritDoc |
| 72 | + */ |
| 73 | + public function key(): ?string { |
| 74 | + return (string) key( $this->resources ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @inheritDoc |
| 79 | + */ |
| 80 | + public function next(): void { |
| 81 | + next( $this->resources ); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @inheritDoc |
| 86 | + * |
| 87 | + * @param TKey $offset The offset to check. |
| 88 | + * |
| 89 | + * @return bool |
| 90 | + */ |
| 91 | + public function offsetExists( $offset ): bool { |
| 92 | + return array_key_exists( $offset, $this->resources ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @inheritDoc |
| 97 | + * |
| 98 | + * @param TKey $offset The offset to get. |
| 99 | + * |
| 100 | + * @return mixed|null |
| 101 | + */ |
| 102 | + #[ReturnTypeWillChange] |
| 103 | + public function offsetGet( $offset ) { |
| 104 | + return $this->resources[ $offset ] ?? null; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @inheritDoc |
| 109 | + * |
| 110 | + * @param TKey $offset The offset to set. |
| 111 | + * @param mixed $value The value to set. |
| 112 | + */ |
| 113 | + public function offsetSet( $offset, $value ): void { |
| 114 | + if ( ! $offset ) { |
| 115 | + $offset = (string) count( $this->resources ); |
| 116 | + } |
| 117 | + $this->set( $offset, $value ); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @inheritDoc |
| 122 | + * |
| 123 | + * @param TKey $offset The offset to unset. |
| 124 | + */ |
| 125 | + public function offsetUnset( $offset ): void { |
| 126 | + unset( $this->resources[ $offset ] ); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @inheritDoc |
| 131 | + */ |
| 132 | + public function rewind(): void { |
| 133 | + reset( $this->resources ); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @inheritDoc |
| 138 | + */ |
| 139 | + public function valid(): bool { |
| 140 | + return key( $this->resources ) !== null; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @inheritDoc |
| 145 | + */ |
| 146 | + public function count(): int { |
| 147 | + return count( $this->resources ); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Returns the collection as an array. |
| 152 | + * |
| 153 | + * @since 3.0.0 |
| 154 | + * |
| 155 | + * @return array |
| 156 | + */ |
| 157 | + public function jsonSerialize(): array { |
| 158 | + return $this->resources; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Maps the collection to an array. |
| 163 | + * |
| 164 | + * @since 3.0.0 |
| 165 | + * |
| 166 | + * @param callable $callback The callback to map the collection to an array. |
| 167 | + * |
| 168 | + * @return array |
| 169 | + */ |
| 170 | + public function map( callable $callback ): array { |
| 171 | + return array_map( $callback, $this->resources ); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Filters the collection. |
| 176 | + * |
| 177 | + * @since 3.0.0 |
| 178 | + * |
| 179 | + * @param callable $callback The callback to filter the collection. |
| 180 | + * |
| 181 | + * @return Collection |
| 182 | + */ |
| 183 | + public function filter( callable $callback ): Collection { |
| 184 | + return new static( array_filter( $this->resources, $callback ) ); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Gets a resource from the collection. |
| 189 | + * |
| 190 | + * @since 3.0.0 |
| 191 | + * |
| 192 | + * @param TKey $key The key to get. |
| 193 | + * |
| 194 | + * @return ?mixed |
| 195 | + */ |
| 196 | + #[ReturnTypeWillChange] |
| 197 | + public function get( string $key ) { |
| 198 | + return $this->offsetGet( $key ); |
| 199 | + } |
| 200 | +} |
0 commit comments