Skip to content

Commit

Permalink
Seed: first --> firstOf
Browse files Browse the repository at this point in the history
  • Loading branch information
dakujem committed Jan 19, 2024
1 parent 0e997f7 commit e685b6e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ foreach ($root as $node) {
```

Finally, the filter iterator `Iterator\Filter` may be used for filtering either the input data or tree nodes.

```php
use Dakujem\Oliva\Iterator\Filter;
use Dakujem\Oliva\Node;
Expand All @@ -392,7 +393,7 @@ foreach($filter as $node){

// Find the first node that matches a criterion (data with ID = 42).
$filter = new Filter($root, fn(Node $node): bool => $node->data()?->id === 42);
$node = Seed::first(new Filter(
$node = Seed::firstOf(new Filter(
input: $root,
accept: fn(Node $node): bool => $node->data()?->id === 42),
);
Expand Down
22 changes: 15 additions & 7 deletions src/Seed.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,21 @@ public static function merged(iterable ...$input): Generator
}
}

/**
* Prepend `null` value at the beginning of the data collection.
* Use when missing root.
* Remember, the data accessor and the node factory must be aware that a null may be passed to them.
*/
public static function nullFirst(iterable $input): Generator
{
yield null;
yield from $input;
}

/**
* Returns the first element of an iterable collection.
*/
public static function first(iterable $input): mixed
public static function firstOf(iterable $input): mixed
{
foreach ($input as $item) {
return $item;
Expand All @@ -45,14 +56,11 @@ public static function first(iterable $input): mixed
}

/**
* Prepend `null` value at the beginning of the data collection.
* Use when missing root.
* Remember, the data accessor and the node factory must be aware that a null may be passed to them.
* @deprecated Name changed.
*/
public static function nullFirst(iterable $input): Generator
public static function first(iterable $input): mixed
{
yield null;
yield from $input;
return self::firstOf($input);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/mptree.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Item
Assert::type(AlmostThere::class, $almost);
Assert::type(Node::class, $almost->root());
Assert::null($almost->root()?->data());
Assert::type(Item::class, Seed::first($almost->root()?->children())?->data());
Assert::type(Item::class, Seed::firstOf($almost->root()?->children())?->data());

Assert::same([
'>' => 'root',
Expand Down

0 comments on commit e685b6e

Please sign in to comment.