Skip to content

Commit

Permalink
misc: check value acceptance in separate node builder
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Feb 19, 2022
1 parent 06e9ded commit 30d4479
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Library/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use CuyZ\Valinor\Mapper\Object\Factory\ObjectBuilderFactory;
use CuyZ\Valinor\Mapper\Tree\Builder\ArrayNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\CasterNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\CasterProxyNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\ClassNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\EnumNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\ErrorCatcherNodeBuilder;
Expand Down Expand Up @@ -128,6 +129,7 @@ public function __construct(Settings $settings)
)
));

$builder = new CasterProxyNodeBuilder($builder);
$builder = new VisitorNodeBuilder($builder, $settings->nodeVisitors);
$builder = new ValueAlteringNodeBuilder($builder, $settings->valueModifier);
$builder = new ShellVisitorNodeBuilder($builder, $this->get(ShellVisitor::class));
Expand Down
5 changes: 0 additions & 5 deletions src/Mapper/Tree/Builder/CasterNodeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ public function __construct(array $builders)
public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
{
$type = $shell->type();
$value = $shell->value();

if ($type->accepts($value)) {
return Node::leaf($shell, $value);
}

foreach ($this->builders as $allowed => $builder) {
if ($type instanceof $allowed) {
Expand Down
30 changes: 30 additions & 0 deletions src/Mapper/Tree/Builder/CasterProxyNodeBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace CuyZ\Valinor\Mapper\Tree\Builder;

use CuyZ\Valinor\Mapper\Tree\Node;
use CuyZ\Valinor\Mapper\Tree\Shell;

/** @internal */
final class CasterProxyNodeBuilder implements NodeBuilder
{
private NodeBuilder $delegate;

public function __construct(NodeBuilder $delegate)
{
$this->delegate = $delegate;
}

public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node
{
$value = $shell->value();

if ($shell->type()->accepts($value)) {
return Node::leaf($shell, $value);
}

return $this->delegate->build($shell, $rootBuilder);
}
}

0 comments on commit 30d4479

Please sign in to comment.