|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace RunOpenCode\Component\Dataset\Collector; |
| 6 | + |
| 7 | +use RunOpenCode\Component\Dataset\Contract\CollectorInterface; |
| 8 | +use RunOpenCode\Component\Dataset\Contract\StreamInterface; |
| 9 | +use RunOpenCode\Component\Dataset\Exception\LogicException; |
| 10 | +use RunOpenCode\Component\Dataset\Exception\OutOfBoundsException; |
| 11 | +use RunOpenCode\Component\Dataset\Exception\UnsupportedException; |
| 12 | + |
| 13 | +/** |
| 14 | + * Collects items into an iterable, indexing values by their keys. |
| 15 | + * |
| 16 | + * The collector assumes that keys are not unique; therefore, accessing |
| 17 | + * a value by key returns a list of values. |
| 18 | + * |
| 19 | + * Currently, the allowed key types are scalar values |
| 20 | + * (int, float, string, bool, null) and objects. |
| 21 | + * |
| 22 | + * @template TKey |
| 23 | + * @template TValue |
| 24 | + * |
| 25 | + * @implements CollectorInterface<iterable<TKey,TValue>> |
| 26 | + * @implements \IteratorAggregate<TKey, TValue> |
| 27 | + * @implements \ArrayAccess<TKey, list<TValue>> |
| 28 | + */ |
| 29 | +final class IndexedCollector implements CollectorInterface, \ArrayAccess, \IteratorAggregate, \Countable |
| 30 | +{ |
| 31 | + /** |
| 32 | + * {@inheritdoc} |
| 33 | + */ |
| 34 | + public mixed $value { |
| 35 | + get => $this->getIterator(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * {@inheritdoc} |
| 40 | + */ |
| 41 | + public array $aggregated { |
| 42 | + get => $this->source instanceof StreamInterface ? $this->source->aggregated : []; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * {@inheritdoc} |
| 47 | + */ |
| 48 | + public bool $closed { |
| 49 | + get => false; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Index of values with keys of scalar type. |
| 54 | + * |
| 55 | + * @var array<TKey, list<TValue>> |
| 56 | + */ |
| 57 | + private array $scalarIndex = []; |
| 58 | + |
| 59 | + /** |
| 60 | + * Index of values with keys of object type. |
| 61 | + * |
| 62 | + * @var \SplObjectStorage<TKey&object, list<TValue>> |
| 63 | + */ |
| 64 | + private \SplObjectStorage $objectIndex; |
| 65 | + |
| 66 | + /** |
| 67 | + * Collected values from stream. |
| 68 | + * |
| 69 | + * @var array<array{TKey, TValue}> |
| 70 | + */ |
| 71 | + private array $collected = []; |
| 72 | + |
| 73 | + /** |
| 74 | + * @param iterable<TKey, TValue> $source Stream source to collect. |
| 75 | + */ |
| 76 | + public function __construct( |
| 77 | + private readonly iterable $source, |
| 78 | + ) { |
| 79 | + $this->objectIndex = new \SplObjectStorage(); |
| 80 | + |
| 81 | + foreach ($this->source as $key => $value) { |
| 82 | + $this->collected[] = [$key, $value]; |
| 83 | + |
| 84 | + if (\is_string($key) || \is_int($key)) { |
| 85 | + $this->scalarIndex[$key] = $this->scalarIndex[$key] ?? []; |
| 86 | + $this->scalarIndex[$key][] = $value; |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + if (\is_object($key)) { |
| 91 | + $current = $this->objectIndex->contains($key) ? $this->objectIndex[$key] : []; |
| 92 | + |
| 93 | + $current[] = $value; |
| 94 | + $this->objectIndex[$key] = $current; |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + throw new UnsupportedException('Only object, string and integer keys are supported.'); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * {@inheritdoc} |
| 104 | + */ |
| 105 | + public function getIterator(): \Traversable |
| 106 | + { |
| 107 | + foreach ($this->collected as [$key, $value]) { |
| 108 | + yield $key => $value; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * {@inheritdoc} |
| 114 | + */ |
| 115 | + public function offsetExists(mixed $offset): bool |
| 116 | + { |
| 117 | + return match (true) { |
| 118 | + \is_string($offset) || \is_int($offset) => \array_key_exists($offset, $this->scalarIndex), |
| 119 | + \is_object($offset) => $this->objectIndex->contains($offset), |
| 120 | + default => throw new UnsupportedException('Only object, string and integer keys are supported.'), |
| 121 | + }; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Get values for given key. |
| 126 | + * |
| 127 | + * @param TKey $offset |
| 128 | + * |
| 129 | + * @return list<TValue> |
| 130 | + */ |
| 131 | + public function offsetGet(mixed $offset): mixed |
| 132 | + { |
| 133 | + if (!$this->offsetExists($offset)) { |
| 134 | + throw new OutOfBoundsException($offset, $this->value); |
| 135 | + } |
| 136 | + |
| 137 | + return match (true) { |
| 138 | + \is_string($offset) || \is_int($offset) => $this->scalarIndex[$offset], |
| 139 | + \is_object($offset) => $this->objectIndex[$offset], |
| 140 | + default => throw new UnsupportedException('Only object, string and integer keys are supported.'), |
| 141 | + }; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * {@inheritdoc} |
| 146 | + */ |
| 147 | + public function offsetSet(mixed $offset, mixed $value): void |
| 148 | + { |
| 149 | + throw new LogicException(\sprintf( |
| 150 | + 'Cannot set value for key "%s". Collector "%s" is read-only.', |
| 151 | + \var_export($offset, true), |
| 152 | + self::class, |
| 153 | + )); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * {@inheritdoc} |
| 158 | + */ |
| 159 | + public function offsetUnset(mixed $offset): void |
| 160 | + { |
| 161 | + throw new LogicException(\sprintf( |
| 162 | + 'Cannot unset value for key "%s". Collector "%s" is read-only.', |
| 163 | + \var_export($offset, true), |
| 164 | + self::class, |
| 165 | + )); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * {@inheritdoc} |
| 170 | + */ |
| 171 | + public function count(): int |
| 172 | + { |
| 173 | + return \count($this->collected); |
| 174 | + } |
| 175 | +} |
0 commit comments