e83333003961f9f023bc0deb2fa021cee182b312
[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
19 public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
20 {
21 $node = $this->delegate->build($shell, $rootBuilder);
22
23 if (! $node->isValid()) {
24 return $node;
25 }
26
27 $value = $node->value();
28
29 foreach ($this->functions as $function) {
30 $parameters = $function->definition()->parameters();
31
32 if (count($parameters) === 0) {
33 continue;
34 }
35
36 $firstParameterType = $parameters->at(0)->type();
37
38 if (! $firstParameterType->accepts($value)) {
39 continue;
40 }
41
42 $value = ($function->callback())($value);
43 $node = $node->withValue($value);
44 }
45
46 return $node;
47 }
48 }