Skip to content

Commit

Permalink
filter iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
dakujem committed Jan 14, 2024
1 parent 439af53 commit 7f31d1f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Iterator/Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Dakujem\Oliva\Iterator;

use CallbackFilterIterator;

/**
* Filter tree iterator.
* To be used with tree traversal iterators or for filtering input data.
*
* Wraps the native callback filter iterator with improved interface (supporting arrays).
*
* @author Andrej Rypak <[email protected]>
*/
final class Filter extends CallbackFilterIterator
{
/**
* @param callable $accept Callable predicate returning `true`/truthy for accepted nodes; signature: `fn(TreeNodeContract|mixed):bool`
*/
public function __construct(
iterable $input,
callable $accept,
) {
parent::__construct(
Data::iterator($input),
$accept,
);
}
}
3 changes: 3 additions & 0 deletions tests/mptree.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ foreach($it as $key => $node){

//xdebug_break();

new Filter($it, Data::omitNull());
new Filter($it, Data::omitRoot());

$item = $tree->root()?->data();


Expand Down

0 comments on commit 7f31d1f

Please sign in to comment.