66a6630dac91704fe0849f26a121754c6516991a
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 declare(strict_types=1);
4
5 namespace CuyZ\Valinor\Mapper\Tree\Builder;
6
7 use CuyZ\Valinor\Definition\FunctionsContainer;
8 use CuyZ\Valinor\Mapper\Tree\Shell;
9
10 /** @internal */
11 final class ValueAlteringNodeBuilder implements NodeBuilder
12 {
13 public function __construct(
14 private NodeBuilder $delegate,
15 private FunctionsContainer $functions
16 ) {}
17
18 public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
19 {
20 $node = $this->delegate->build($shell, $rootBuilder);
21
22 if (! $node->isValid()) {
23 return $node;
24 }
25
26 $value = $node->value();
27
28 foreach ($this->functions as $function) {
29 $parameters = $function->definition()->parameters();
30
31 if (count($parameters) === 0) {
32 continue;
33 }
34
35 $firstParameterType = $parameters->at(0)->type();
36
37 if (! $firstParameterType->accepts($value)) {
38 continue;
39 }
40
41 $value = ($function->callback())($value);
42 $node = $node->withValue($value);
43 }
44
45 return $node;
46 }
47 }