3 declare(strict_types=1);
5 namespace CuyZ\Valinor\Definition\Repository\Cache\Compiler;
7 use CuyZ\Valinor\Cache\Compiled\CacheCompiler;
8 use CuyZ\Valinor\Definition\ClassDefinition;
9 use CuyZ\Valinor\Definition\MethodDefinition;
10 use CuyZ\Valinor\Definition\PropertyDefinition;
12 use function array_map;
15 use function iterator_to_array;
18 final class ClassDefinitionCompiler implements CacheCompiler
20 private TypeCompiler $typeCompiler;
22 private AttributesCompiler $attributesCompiler;
24 private MethodDefinitionCompiler $methodCompiler;
26 private PropertyDefinitionCompiler $propertyCompiler;
28 public function __construct()
30 $this->typeCompiler = new TypeCompiler();
31 $this->attributesCompiler = new AttributesCompiler();
33 $this->methodCompiler = new MethodDefinitionCompiler($this->typeCompiler, $this->attributesCompiler);
34 $this->propertyCompiler = new PropertyDefinitionCompiler($this->typeCompiler, $this->attributesCompiler);
37 public function compile(mixed $value): string
39 assert($value instanceof ClassDefinition);
41 $type = $this->typeCompiler->compile($value->type());
43 $properties = array_map(
44 fn (PropertyDefinition $property) => $this->propertyCompiler->compile($property),
45 iterator_to_array($value->properties())
48 $properties = implode(', ', $properties);
51 fn (MethodDefinition $method) => $this->methodCompiler->compile($method),
52 iterator_to_array($value->methods())
55 $methods = implode(', ', $methods);
56 $attributes = $this->attributesCompiler->compile($value->attributes());
58 $isFinal = var_export($value->isFinal(), true);
59 $isAbstract = var_export($value->isAbstract(), true);
62 new \CuyZ\Valinor\Definition\ClassDefinition(
65 new \CuyZ\Valinor\Definition\Properties($properties),
66 new \CuyZ\Valinor\Definition\Methods($methods),