-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
45 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,37 +2,28 @@ | |
|
||
declare(strict_types=1); | ||
|
||
namespace Dakujem\Oliva\Iterator; | ||
namespace Dakujem\Oliva; | ||
|
||
use ArrayIterator; | ||
use Dakujem\Oliva\DataNodeContract; | ||
use Dakujem\Oliva\TreeNodeContract; | ||
use Generator; | ||
use Iterator; | ||
use IteratorIterator; | ||
|
||
/** | ||
* Iterable data collection helper functions. | ||
* A static tool to manage iterable data collections and iterators. | ||
* | ||
* Contains: | ||
* - methods that produce or adapt iterable data and iterators | ||
* - methods that produce filtering callables to be used with the Filter iterator | ||
* | ||
* @author Andrej Rypak <[email protected]> | ||
*/ | ||
final class Data | ||
final class Seed | ||
{ | ||
/** | ||
* 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; | ||
} | ||
|
||
/** | ||
* Create a merged iterable. | ||
* Can be used to prepend or append data from multiple source collections. | ||
* The data is not actually merged, but a generator is used. | ||
* The data is not actually merged, but a generator is produced. | ||
*/ | ||
public static function merged(iterable ...$input): Generator | ||
{ | ||
|
@@ -42,13 +33,25 @@ public static function merged(iterable ...$input): Generator | |
} | ||
|
||
/** | ||
* Create a callable that omits the root node. | ||
* To be used with `Filter` iterator as the predicate: | ||
* @see Filter | ||
* Returns the first element of an iterable collection. | ||
*/ | ||
public static function omitRoot(): callable | ||
public static function first(iterable $input): mixed | ||
{ | ||
return fn(TreeNodeContract $node): bool => !$node->isRoot(); | ||
foreach ($input as $item) { | ||
return $item; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
|
||
/** | ||
|
@@ -62,22 +65,21 @@ public static function omitNull(): callable | |
} | ||
|
||
/** | ||
* Returns an iterator. | ||
* Useful where en iterator is required. | ||
* Create a filtering callable that omits the root node. | ||
* To be used with `Filter` iterator as the predicate: | ||
* @see Filter | ||
*/ | ||
public static function iterator(iterable $input): Iterator | ||
public static function omitRoot(): callable | ||
{ | ||
return is_array($input) ? new ArrayIterator($input) : new IteratorIterator($input); | ||
return fn(TreeNodeContract $node): bool => !$node->isRoot(); | ||
} | ||
|
||
/** | ||
* Returns the first element of an iterable collection. | ||
* Accepts any iterable and returns an iterator. | ||
* Useful where en iterator is required, but any iterable or array is provided. | ||
*/ | ||
public static function first(iterable $input): mixed | ||
public static function iterator(iterable $input): Iterator | ||
{ | ||
foreach ($input as $item) { | ||
return $item; | ||
} | ||
return null; | ||
return is_array($input) ? new ArrayIterator($input) : new IteratorIterator($input); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters