f129b3d31384138f080b41d8484f35c73b50f768
[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\Repository\ClassDefinitionRepository;
8 use CuyZ\Valinor\Mapper\Object\Factory\ObjectBuilderFactory;
9 use CuyZ\Valinor\Mapper\Object\FilteredObjectBuilder;
10 use CuyZ\Valinor\Mapper\Tree\Shell;
11 use CuyZ\Valinor\Type\ClassType;
12
13 use function assert;
14
15 /** @internal */
16 final class NativeClassNodeBuilder implements NodeBuilder
17 {
18 public function __construct(
19 private ClassDefinitionRepository $classDefinitionRepository,
20 private ObjectBuilderFactory $objectBuilderFactory,
21 private ObjectNodeBuilder $objectNodeBuilder,
22 private bool $enableFlexibleCasting,
23 ) {}
24
25 public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
26 {
27 $type = $shell->type();
28
29 // @infection-ignore-all
30 assert($type instanceof ClassType);
31
32 if ($this->enableFlexibleCasting && $shell->value() === null) {
33 $shell = $shell->withValue([]);
34 }
35
36 $class = $this->classDefinitionRepository->for($type);
37 $objectBuilder = new FilteredObjectBuilder($shell->value(), ...$this->objectBuilderFactory->for($class));
38
39 return $this->objectNodeBuilder->build($objectBuilder, $shell, $rootBuilder);
40 }
41 }