3 declare(strict_types=1);
5 namespace CuyZ\Valinor\Mapper\Tree\Builder;
7 use CuyZ\Valinor\Definition\FunctionsContainer;
8 use CuyZ\Valinor\Mapper\Tree\Shell;
11 final class ValueAlteringNodeBuilder implements NodeBuilder
13 public function __construct(
14 private NodeBuilder $delegate,
15 private FunctionsContainer $functions
19 public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
21 $node = $this->delegate->build($shell, $rootBuilder);
23 if (! $node->isValid()) {
27 $value = $node->value();
29 foreach ($this->functions as $function) {
30 $parameters = $function->definition()->parameters();
32 if (count($parameters) === 0) {
36 $firstParameterType = $parameters->at(0)->type();
38 if (! $firstParameterType->accepts($value)) {
42 $value = ($function->callback())($value);
43 $node = $node->withValue($value);