-
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.
unifying and reversing the builder interface
constructors now accept
- Loading branch information
Showing
6 changed files
with
201 additions
and
83 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 |
---|---|---|
|
@@ -4,9 +4,9 @@ | |
|
||
namespace Dakujem\Oliva\MaterializedPath; | ||
|
||
use Dakujem\Oliva\MaterializedPath\Support\AlmostThere; | ||
use Dakujem\Oliva\MaterializedPath\Support\Register; | ||
use Dakujem\Oliva\MaterializedPath\Support\ShadowNode; | ||
use Dakujem\Oliva\MaterializedPath\Support\AlmostThere; | ||
use Dakujem\Oliva\MovableNodeContract; | ||
use Dakujem\Oliva\TreeNodeContract; | ||
use LogicException; | ||
|
@@ -25,26 +25,48 @@ | |
* | ||
* Fixed path variant example: | ||
* ``` | ||
* $root = (new TreeBuilder())->build( | ||
* $myItemCollection, | ||
* $builder = new TreeBuilder( | ||
* fn(MyItem $item) => new Node($item), | ||
* TreeBuilder::fixed(3, fn(MyItem $item) => $item->path), | ||
* ); | ||
* $root = $builder->build( $myItemCollection ); | ||
* ``` | ||
* | ||
* Delimited path variant example: | ||
* ``` | ||
* $root = (new TreeBuilder())->build( | ||
* $myItemCollection, | ||
* $builder = new TreeBuilder( | ||
* fn(MyItem $item) => new Node($item), | ||
* TreeBuilder::delimited('.', fn(MyItem $item) => $item->path), | ||
* ); | ||
* $root = $builder->build( $myItemCollection ); | ||
* ``` | ||
* | ||
* @author Andrej Rypak <[email protected]> | ||
*/ | ||
final class TreeBuilder | ||
{ | ||
/** | ||
* Node factory, | ||
* signature `fn(mixed $data): MovableNodeContract`. | ||
* @var callable | ||
*/ | ||
private $node; | ||
|
||
/** | ||
* Extractor of the node vector, | ||
* signature `fn(mixed $data, mixed $inputIndex, TreeNodeContract $node): array`. | ||
* @var callable | ||
*/ | ||
private $vector; | ||
|
||
public function __construct( | ||
callable $node, | ||
callable $vector, | ||
) { | ||
$this->node = $node; | ||
$this->vector = $vector; | ||
} | ||
|
||
public static function fixed(int $levelWidth, callable $accessor): callable | ||
{ | ||
return function (mixed $data) use ($levelWidth, $accessor): array { | ||
|
@@ -82,27 +104,21 @@ public static function delimited(string $delimiter, callable $accessor): callabl | |
}; | ||
} | ||
|
||
public function build( | ||
iterable $input, | ||
callable $node, | ||
callable $vector, | ||
): TreeNodeContract { | ||
$root = $this->processInput($input, $node, $vector)->root(); | ||
public function build(iterable $input): TreeNodeContract | ||
{ | ||
$root = $this->processInput($input)->root(); | ||
if (null === $root) { | ||
throw new RuntimeException('Corrupted input, no tree created.'); | ||
} | ||
return $root; | ||
} | ||
|
||
public function processInput( | ||
iterable $input, | ||
callable $node, | ||
callable $vector, | ||
): AlmostThere { | ||
public function processInput(iterable $input): AlmostThere | ||
{ | ||
$shadowRoot = $this->buildShadowTree( | ||
$input, | ||
$node, | ||
$vector, | ||
$this->node, | ||
$this->vector, | ||
); | ||
|
||
// The actual tree nodes are not yet connected. | ||
|
Oops, something went wrong.