Skip to content
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

Implement iterator interface for node collection #5

Merged
merged 4 commits into from
Jun 7, 2024
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
26 changes: 26 additions & 0 deletions src/NodeFilter/NodeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class NodeCollection implements NodeFilter
use Traits\EnumeratesValues;

private DOMNodeList $nodes;
private int $iteratorPosition = 0;

/**
* @throws Exception When node could not be imported into a new document.
Expand Down Expand Up @@ -200,4 +201,29 @@ public function xPath(string $selector): NodeFilter
{
return $this->internalXpath($this->nodes, $selector);
}

public function current(): NodeFilter
{
return $this->nth($this->iteratorPosition);
}

public function next(): void
{
$this->iteratorPosition++;
}

public function key(): int
{
return $this->iteratorPosition;
}

public function valid(): bool
{
return $this->current()->exists();
}

public function rewind(): void
{
$this->iteratorPosition = 0;
}
}
3 changes: 2 additions & 1 deletion src/NodeFilter/NodeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Closure;

interface NodeFilter
/** @extends \Iterator<int, NodeFilter> */
interface NodeFilter extends \Iterator
{
/**
* Get the value for the requested attribute of the first node in the current collection.
Expand Down
23 changes: 23 additions & 0 deletions src/NodeFilter/NullNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,27 @@ public function xPath(string $selector): NodeFilter
{
return $this;
}

public function current(): NodeFilter
{
return $this;
}

public function next(): void
{
}

public function key(): int
{
return 0;
}

public function valid(): bool
{
return false;
}

public function rewind(): void
{
}
}
11 changes: 11 additions & 0 deletions tests/NodeFilter/NodeCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,15 @@ private function createDOMNodes(): DOMNodeList

return $doc->childNodes;
}

/** @test */
public function it_should_iterate_zero_times(): void
{
$text = '';
foreach ($this->node->querySelector('li') as $t) {
$text .= $t->text();
}

$this->assertEquals('onetwothreefour', $text);
}
}
11 changes: 11 additions & 0 deletions tests/NodeFilter/NullNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ protected function setUp(): void
$this->node = new NullNode();
}

/** @test */
public function it_should_iterate_zero_times(): void
{
$count = 0;
foreach ($this->node as $n) {
$count += $n->count() + 1;
}

$this->assertEquals(0, $count);
}

/** @test */
public function it_should_return_empty_array_on_each(): void
{
Expand Down
Loading