be0a590fdd3e24c39d960cf97c25d4bec85241d9
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 declare(strict_types=1);
4
5 namespace CuyZ\Valinor\Definition\Repository\Cache\Compiler;
6
7 use CuyZ\Valinor\Definition\ParameterDefinition;
8
9 /** @internal */
10 final class ParameterDefinitionCompiler
11 {
12 public function __construct(
13 private TypeCompiler $typeCompiler,
14 private AttributesCompiler $attributesCompiler
15 ) {}
16
17 public function compile(ParameterDefinition $parameter): string
18 {
19 $isOptional = var_export($parameter->isOptional(), true);
20 $isVariadic = var_export($parameter->isVariadic(), true);
21 $defaultValue = $this->defaultValue($parameter);
22 $type = $this->typeCompiler->compile($parameter->type());
23 $attributes = $this->attributesCompiler->compile($parameter->attributes());
24
25 return <<<PHP
26 new \CuyZ\Valinor\Definition\ParameterDefinition(
27 '{$parameter->name()}',
28 '{$parameter->signature()}',
29 $type,
30 $isOptional,
31 $isVariadic,
32 $defaultValue,
33 $attributes
34 )
35 PHP;
36 }
37
38 private function defaultValue(ParameterDefinition $parameter): string
39 {
40 $defaultValue = $parameter->defaultValue();
41
42 return is_object($defaultValue)
43 ? 'unserialize(' . var_export(serialize($defaultValue), true) . ')'
44 : var_export($defaultValue, true);
45 }
46 }