From e5b9a351ae41136254235e6eef6efa837f915f66 Mon Sep 17 00:00:00 2001 From: Andrej Rypo Date: Thu, 18 Jan 2024 12:20:31 +0100 Subject: [PATCH] builder interface reversed, renamed --- readme.md | 11 +++---- src/Simple/NodeBuilder.php | 8 +++-- .../{TreeBuilder.php => TreeWrapper.php} | 31 +++++++++++++++---- 3 files changed, 36 insertions(+), 14 deletions(-) rename src/Simple/{TreeBuilder.php => TreeWrapper.php} (71%) diff --git a/readme.md b/readme.md index fca95a5..84db4bb 100644 --- a/readme.md +++ b/readme.md @@ -217,7 +217,7 @@ In case the data is already structured as tree data, a simple wrapper may be use ```php use Any\Item; -use Dakujem\Oliva\Simple\TreeBuilder; +use Dakujem\Oliva\Simple\TreeWrapper; use Dakujem\Oliva\Node; // $json = (new External\ApiConnector())->call('getJsonData'); @@ -241,15 +241,14 @@ $rawData = [ ], ]; -$builder = new TreeBuilder(); -$root = $builder->build( - input: $rawData, +$builder = new TreeWrapper( node: function(array $item) { // How to create a node. unset($item['children']); return new Node($item); }, children: fn(array $item):array => $item['children'] ?? [], // How to extract children. ); +$root = $builder->wrap($rawData); ``` Above, `children` expects an extractor with signature `fn(mixed $data, TreeNodeContract $node): ?iterable`. @@ -476,7 +475,7 @@ One of the solutions is to prepend an empty data element and then ignore it duri Observe using `Seed` helper class: ```php -use Dakujem\Oliva\MaterializedPath; +use Dakujem\Oliva\MaterializedPath; use Dakujem\Oliva\Seed; $source = Sql::getMeTheCommentsFor($article); @@ -498,7 +497,7 @@ foreach(Seed::omitNull($root) as $node) { // the node with `null` data is omitt We could also use `Seed::merged` to prepend an item with fabricated root data, but then `Seed::omitRoot` must be used omit the root instead: ```php -use Dakujem\Oliva\MaterializedPath; +use Dakujem\Oliva\MaterializedPath; use Dakujem\Oliva\Seed; $source = Sql::getMeTheCommentsFor($article); diff --git a/src/Simple/NodeBuilder.php b/src/Simple/NodeBuilder.php index 8442091..68f2f7c 100644 --- a/src/Simple/NodeBuilder.php +++ b/src/Simple/NodeBuilder.php @@ -13,9 +13,13 @@ * * @author Andrej Rypak */ -class NodeBuilder +final class NodeBuilder { - /** @var callable */ + /** + * Node factory, + * signature `fn(mixed $data): MovableNodeContract`. + * @var callable + */ private $factory; public function __construct( diff --git a/src/Simple/TreeBuilder.php b/src/Simple/TreeWrapper.php similarity index 71% rename from src/Simple/TreeBuilder.php rename to src/Simple/TreeWrapper.php index c0c1d52..7684a80 100644 --- a/src/Simple/TreeBuilder.php +++ b/src/Simple/TreeWrapper.php @@ -14,17 +14,36 @@ * * @author Andrej Rypak */ -class TreeBuilder +final class TreeWrapper { - public function build( - mixed $data, + /** + * Node factory, + * signature `fn(mixed $data): MovableNodeContract`. + * @var callable + */ + private $factory; + + /** + * Extractor of children iterable, + * signature `fn(mixed $data, TreeNodeContract $node): ?iterable`. + * @var callable + */ + private $childrenExtractor; + + public function __construct( callable $node, callable $children, - ): TreeNodeContract { + ) { + $this->factory = $node; + $this->childrenExtractor = $children; + } + + public function wrap(mixed $data): TreeNodeContract + { return $this->wrapNode( data: $data, - nodeFactory: $node, - childrenExtractor: $children, + nodeFactory: $this->factory, + childrenExtractor: $this->childrenExtractor, ); }