Smoren /
sequence-php
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace Smoren\Sequence\Structs; |
||||
| 6 | |||||
| 7 | use Smoren\Sequence\Exceptions\OutOfRangeException; |
||||
| 8 | use Smoren\Sequence\Interfaces\IndexedArrayInterface; |
||||
| 9 | use Smoren\Sequence\Iterators\IndexedArrayIterator; |
||||
| 10 | |||||
| 11 | /** |
||||
| 12 | * Implementation of Indexed Array. |
||||
| 13 | * |
||||
| 14 | * @template T |
||||
| 15 | * @implements IndexedArrayInterface<T> |
||||
| 16 | */ |
||||
| 17 | class IndexedArray implements IndexedArrayInterface |
||||
| 18 | { |
||||
| 19 | /** |
||||
| 20 | * @var array<T> data storage |
||||
| 21 | */ |
||||
| 22 | protected array $source; |
||||
| 23 | |||||
| 24 | /** |
||||
| 25 | * IndexedArray constructor. |
||||
| 26 | * |
||||
| 27 | * @param array<T> $source default data storage |
||||
| 28 | */ |
||||
| 29 | 18 | public function __construct(array $source = []) |
|||
| 30 | { |
||||
| 31 | 18 | $this->source = array_values($source); |
|||
| 32 | } |
||||
| 33 | |||||
| 34 | /** |
||||
| 35 | * {@inheritDoc} |
||||
| 36 | */ |
||||
| 37 | 3 | public function getRange(): Range |
|||
| 38 | { |
||||
| 39 | 3 | return new Range(0, count($this->source), 1); |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
0 of type integer is incompatible with the type Smoren\Sequence\Structs\T expected by parameter $start of Smoren\Sequence\Structs\Range::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 40 | } |
||||
| 41 | |||||
| 42 | /** |
||||
| 43 | * {@inheritDoc} |
||||
| 44 | */ |
||||
| 45 | 3 | public function getIterator(): IndexedArrayIterator |
|||
| 46 | { |
||||
| 47 | 3 | return new IndexedArrayIterator($this); |
|||
| 48 | } |
||||
| 49 | |||||
| 50 | /** |
||||
| 51 | * {@inheritDoc} |
||||
| 52 | */ |
||||
| 53 | 3 | public function offsetExists($offset): bool |
|||
| 54 | { |
||||
| 55 | 3 | return $this->getRange()->offsetExists($offset); |
|||
| 56 | } |
||||
| 57 | |||||
| 58 | /** |
||||
| 59 | * {@inheritDoc} |
||||
| 60 | */ |
||||
| 61 | 3 | public function offsetGet($offset) |
|||
| 62 | { |
||||
| 63 | 3 | $index = $this->getRange()->offsetGet($offset); |
|||
| 64 | 3 | return $this->source[$index]; |
|||
| 65 | } |
||||
| 66 | |||||
| 67 | /** |
||||
| 68 | * {@inheritDoc} |
||||
| 69 | */ |
||||
| 70 | 13 | public function offsetSet($offset, $value): void |
|||
| 71 | { |
||||
| 72 | 13 | if ($offset === null) { |
|||
| 73 | 12 | $this->source[] = $value; |
|||
| 74 | } else { |
||||
| 75 | 1 | $range = $this->getRange(); |
|||
| 76 | 1 | if (isset($range[$offset])) { |
|||
| 77 | 1 | $index = $range[$offset]; |
|||
| 78 | 1 | $this->source[$index] = $value; |
|||
| 79 | } else { |
||||
| 80 | 1 | throw new OutOfRangeException(); |
|||
| 81 | } |
||||
| 82 | } |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | /** |
||||
| 86 | * {@inheritDoc} |
||||
| 87 | */ |
||||
| 88 | 1 | public function offsetUnset($offset): void |
|||
| 89 | { |
||||
| 90 | 1 | $range = $this->getRange(); |
|||
| 91 | 1 | if (isset($range[$offset])) { |
|||
| 92 | 1 | $index = $range[$offset]; |
|||
| 93 | 1 | unset($this->source[$index]); |
|||
| 94 | 1 | $this->source = array_values($this->source); |
|||
| 95 | } else { |
||||
| 96 | 1 | throw new OutOfRangeException(); |
|||
| 97 | } |
||||
| 98 | } |
||||
| 99 | |||||
| 100 | /** |
||||
| 101 | * {@inheritDoc} |
||||
| 102 | */ |
||||
| 103 | 3 | public function count(): int |
|||
| 104 | { |
||||
| 105 | 3 | return count($this->source); |
|||
| 106 | } |
||||
| 107 | |||||
| 108 | /** |
||||
| 109 | * {@inheritDoc} |
||||
| 110 | */ |
||||
| 111 | 16 | public function toArray(): array |
|||
| 112 | { |
||||
| 113 | 16 | return $this->source; |
|||
| 114 | } |
||||
| 115 | } |
||||
| 116 |