Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/RunOpenCode/Component/Dataset/src/Collector/ArrayCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ public function __construct(
$this->value = iterable_to_array($this->source);
}

/**
* @return list<TKey>
*/
public function keys(): array
{
return \array_keys($this->value);
}

/**
* @return list<TValue>
*/
public function values(): array
{
return \array_values($this->value);
}

/**
* {@inheritdoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ final class IndexedCollector implements CollectorInterface, \ArrayAccess, \Itera
/**
* Index of values with keys of scalar type.
*
* @var array<TKey, list<TValue>>
* @var array<scalar, list<TValue>>
*/
private array $scalarIndex = [];

/**
* Index of values with keys of object type.
*
* @var \SplObjectStorage<TKey&object, list<TValue>>
* @var \SplObjectStorage<object, list<TValue>>
*/
private \SplObjectStorage $objectIndex;

Expand All @@ -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;
}

Expand Down Expand Up @@ -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.'),
};
Expand All @@ -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.'),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down