Skip to content

Commit

Permalink
Add NodeFilter::filter() method (#1)
Browse files Browse the repository at this point in the history
* Add NodeFilter::filter() method

* Test NodeFilter::filter()

* phpstan
  • Loading branch information
jerodev authored Apr 7, 2021
1 parent e9a7311 commit bba21cc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ parameters:

-
message: "#^Right side of && is always true\\.$#"
count: 2
count: 3
path: src/NodeFilter/NodeCollection.php

13 changes: 13 additions & 0 deletions src/NodeFilter/NodeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ public function exists(?string $selector = null): bool
return $this->querySelector($selector)->exists();
}

public function filter(Closure $closure): NodeFilter
{
$newDoc = new DOMDocument();

foreach ($this->nodes as $node) {
if ($closure(new NodeCollection($node)) && ($newNode = $newDoc->importNode($node, true))) {
$newDoc->appendChild($newNode);
}
}

return new NodeCollection($newDoc->childNodes);
}

public function first(?string $selector = null): NodeFilter
{
if (\is_null($selector)) {
Expand Down
9 changes: 9 additions & 0 deletions src/NodeFilter/NodeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ public function each($selector = null, ?Closure $closure = null, ?int $max = nul
*/
public function exists(?string $selector = null): bool;

/**
* Filter a list of nodes using a closure that returns a boolean.
* The closure gets passed a node filter to test upon.
*
* @param Closure $closure
* @return NodeFilter
*/
public function filter(Closure $closure): NodeFilter;

/**
* Create a new collection of nodes only containing the first node of the current collection.
*
Expand Down
5 changes: 5 additions & 0 deletions src/NodeFilter/NullNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function exists(?string $selector = null): bool
return false;
}

public function filter(Closure $closure): NodeFilter
{
return $this;
}

public function first(?string $selector = null): NodeFilter
{
return $this;
Expand Down
10 changes: 10 additions & 0 deletions tests/NodeFilter/NodeFilterChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DOMNodeList;
use Generator;
use Jerodev\Diggy\NodeFilter\NodeCollection;
use Jerodev\Diggy\NodeFilter\NodeFilter;
use PHPUnit\Framework\TestCase;

final class NodeFilterChainTest extends TestCase
Expand Down Expand Up @@ -95,6 +96,15 @@ public function functionProvider(): Generator
['is', ['p']],
]
];

yield [
'two',
[
['querySelector', ['ul li']],
['filter', [static fn (NodeFilter $node) => $node->text() === 'two']],
['text']
]
];
}

private function createDOMNodes(): DOMNodeList
Expand Down

0 comments on commit bba21cc

Please sign in to comment.