From d7db51cf9a20a5d662f4729bce1283c0b46164dc Mon Sep 17 00:00:00 2001 From: Stefan Veljancic Date: Wed, 24 Dec 2025 13:53:46 +0100 Subject: [PATCH] Added support for fetching keys and values from array collector --- .../Dataset/src/Collector/ArrayCollector.php | 16 +++++++++++ .../src/Collector/IndexedCollector.php | 14 +++++----- .../tests/Collector/ArrayCollectorTest.php | 28 +++++++++++++++++++ 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/RunOpenCode/Component/Dataset/src/Collector/ArrayCollector.php b/src/RunOpenCode/Component/Dataset/src/Collector/ArrayCollector.php index b38d660..32aea76 100644 --- a/src/RunOpenCode/Component/Dataset/src/Collector/ArrayCollector.php +++ b/src/RunOpenCode/Component/Dataset/src/Collector/ArrayCollector.php @@ -51,6 +51,22 @@ public function __construct( $this->value = iterable_to_array($this->source); } + /** + * @return list + */ + public function keys(): array + { + return \array_keys($this->value); + } + + /** + * @return list + */ + public function values(): array + { + return \array_values($this->value); + } + /** * {@inheritdoc} * diff --git a/src/RunOpenCode/Component/Dataset/src/Collector/IndexedCollector.php b/src/RunOpenCode/Component/Dataset/src/Collector/IndexedCollector.php index e1605cc..011be28 100644 --- a/src/RunOpenCode/Component/Dataset/src/Collector/IndexedCollector.php +++ b/src/RunOpenCode/Component/Dataset/src/Collector/IndexedCollector.php @@ -52,14 +52,14 @@ final class IndexedCollector implements CollectorInterface, \ArrayAccess, \Itera /** * Index of values with keys of scalar type. * - * @var array> + * @var array> */ private array $scalarIndex = []; /** * Index of values with keys of object type. * - * @var \SplObjectStorage> + * @var \SplObjectStorage> */ private \SplObjectStorage $objectIndex; @@ -81,9 +81,9 @@ public function __construct( foreach ($this->source as $key => $value) { $this->collected[] = [$key, $value]; - if (\is_string($key) || \is_int($key)) { - $this->scalarIndex[$key] = $this->scalarIndex[$key] ?? []; - $this->scalarIndex[$key][] = $value; + if (\is_scalar($key)) { + $this->scalarIndex[$key] = $this->scalarIndex[$key] ?? []; // @phpstan-ignore-line + $this->scalarIndex[$key][] = $value; // @phpstan-ignore-line continue; } @@ -115,7 +115,7 @@ public function getIterator(): \Traversable public function offsetExists(mixed $offset): bool { return match (true) { - \is_string($offset) || \is_int($offset) => \array_key_exists($offset, $this->scalarIndex), + \is_scalar($offset) => \array_key_exists($offset, $this->scalarIndex), // @phpstan-ignore-line \is_object($offset) => $this->objectIndex->contains($offset), default => throw new UnsupportedException('Only object, string and integer keys are supported.'), }; @@ -135,7 +135,7 @@ public function offsetGet(mixed $offset): mixed } return match (true) { - \is_string($offset) || \is_int($offset) => $this->scalarIndex[$offset], + \is_scalar($offset) => $this->scalarIndex[$offset], // @phpstan-ignore-line \is_object($offset) => $this->objectIndex[$offset], default => throw new UnsupportedException('Only object, string and integer keys are supported.'), }; diff --git a/src/RunOpenCode/Component/Dataset/tests/Collector/ArrayCollectorTest.php b/src/RunOpenCode/Component/Dataset/tests/Collector/ArrayCollectorTest.php index c0a6653..420ef54 100644 --- a/src/RunOpenCode/Component/Dataset/tests/Collector/ArrayCollectorTest.php +++ b/src/RunOpenCode/Component/Dataset/tests/Collector/ArrayCollectorTest.php @@ -49,6 +49,34 @@ public function array_access(): void $this->assertArrayHasKey('b', $collector); } + #[Test] + public function keys(): void + { + $dataset = [ + 'a' => 1, + 'b' => 2, + 'c' => 3, + ]; + + $collector = collect($dataset, ArrayCollector::class); + + $this->assertSame(['a', 'b', 'c'], $collector->keys()); + } + + #[Test] + public function values(): void + { + $dataset = [ + 'a' => 1, + 'b' => 2, + 'c' => 3, + ]; + + $collector = collect($dataset, ArrayCollector::class); + + $this->assertSame([1, 2, 3], $collector->values()); + } + #[Test] public function counts(): void {