3 declare(strict_types=1);
5 namespace CuyZ\Valinor\Mapper\Tree\Builder;
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;
16 final class NativeClassNodeBuilder implements NodeBuilder
18 public function __construct(
19 private ClassDefinitionRepository $classDefinitionRepository,
20 private ObjectBuilderFactory $objectBuilderFactory,
21 private ObjectNodeBuilder $objectNodeBuilder,
22 private bool $enableFlexibleCasting,
25 public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
27 $type = $shell->type();
29 // @infection-ignore-all
30 assert($type instanceof ClassType);
32 if ($this->enableFlexibleCasting && $shell->value() === null) {
33 $shell = $shell->withValue([]);
36 $class = $this->classDefinitionRepository->for($type);
37 $objectBuilder = new FilteredObjectBuilder($shell->value(), ...$this->objectBuilderFactory->for($class));
39 return $this->objectNodeBuilder->build($objectBuilder, $shell, $rootBuilder);