Skip to content

PHPLIB-609: Allow CachingIterator to handle Iterators with non-unique keys #896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 24, 2022
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
18 changes: 14 additions & 4 deletions src/Model/CachingIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

use function count;
use function current;
use function key;
use function next;
use function reset;

Expand All @@ -40,6 +39,9 @@
*/
class CachingIterator implements Countable, Iterator
{
private const FIELD_KEY = 0;
private const FIELD_VALUE = 1;

/** @var array */
private $items = [];

Expand Down Expand Up @@ -87,7 +89,9 @@ public function count()
#[ReturnTypeWillChange]
public function current()
{
return current($this->items);
$currentItem = current($this->items);

return $currentItem !== false ? $currentItem[self::FIELD_VALUE] : false;
}

/**
Expand All @@ -97,7 +101,9 @@ public function current()
#[ReturnTypeWillChange]
public function key()
{
return key($this->items);
$currentItem = current($this->items);

return $currentItem !== false ? $currentItem[self::FIELD_KEY] : null;
}

/**
Expand Down Expand Up @@ -165,6 +171,10 @@ private function storeCurrentItem()
return;
}

$this->items[$this->iterator->key()] = $this->iterator->current();
// Storing a new item in the internal cache
$this->items[] = [
self::FIELD_KEY => $this->iterator->key(),
self::FIELD_VALUE => $this->iterator->current(),
];
}
}
31 changes: 31 additions & 0 deletions tests/Model/CachingIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,26 @@ public function rewind(): void
$this->assertCount(1, $iterator);
}

public function testCountNonUniqueKeys(): void
{
$iterator = new CachingIterator($this->getNonUniqueTraversable([1, 2, 3]));
$this->assertCount(3, $iterator);
}

public function testIterationNonUniqueKeys(): void
{
$iterator = new CachingIterator($this->getNonUniqueTraversable([1, 2, 3]));

$expectedItem = 1;

foreach ($iterator as $key => $item) {
$this->assertSame(0, $key);
$this->assertSame($expectedItem++, $item);
}

$this->assertFalse($iterator->valid());
}

private function getTraversable($items)
{
foreach ($items as $item) {
Expand All @@ -162,4 +182,15 @@ private function getTraversable($items)
}
}
}

private function getNonUniqueTraversable($items)
{
foreach ($items as $item) {
if ($item instanceof Exception) {
throw $item;
} else {
yield 0 => $item;
}
}
}
}