Skip to content

Commit

Permalink
builder interface reversed, renamed
Browse files Browse the repository at this point in the history
  • Loading branch information
dakujem committed Jan 18, 2024
1 parent e777fe1 commit e5b9a35
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
11 changes: 5 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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`.
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions src/Simple/NodeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
*
* @author Andrej Rypak <xrypak@gmail.com>
*/
class NodeBuilder
final class NodeBuilder
{
/** @var callable */
/**
* Node factory,
* signature `fn(mixed $data): MovableNodeContract`.
* @var callable
*/
private $factory;

public function __construct(
Expand Down
31 changes: 25 additions & 6 deletions src/Simple/TreeBuilder.php → src/Simple/TreeWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,36 @@
*
* @author Andrej Rypak <xrypak@gmail.com>
*/
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,
);
}

Expand Down

0 comments on commit e5b9a35

Please sign in to comment.