Skip to content

Commit

Permalink
Implement iterator interface for node collection (#5)
Browse files Browse the repository at this point in the history
* Implement ArrayAccess

* Uhm actually, an iterator is what you are looking for

* Test iterating node collections
  • Loading branch information
jerodev authored Jun 7, 2024
1 parent 7551a9a commit 4575e42
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
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

0 comments on commit 4575e42

Please sign in to comment.