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
18 public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
20 $node = $this->delegate->build($shell, $rootBuilder);
22 if (! $node->isValid()) {
26 $value = $node->value();
28 foreach ($this->functions as $function) {
29 $parameters = $function->definition()->parameters();
31 if (count($parameters) === 0) {
35 $firstParameterType = $parameters->at(0)->type();
37 if (! $firstParameterType->accepts($value)) {
41 $value = ($function->callback())($value);
42 $node = $node->withValue($value);