|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Typhoon\TypeSpec; |
| 6 | + |
| 7 | +final class ComplexType extends Type |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @param non-empty-string $name |
| 11 | + * @param list<Template> $templates |
| 12 | + * @param non-empty-list<Parameter> $parameters |
| 13 | + * @param non-empty-string $TType |
| 14 | + * @param list<class-string> $imports |
| 15 | + */ |
| 16 | + public function __construct( |
| 17 | + string $name, |
| 18 | + private string $TType, |
| 19 | + private array $parameters, |
| 20 | + private array $templates = [], |
| 21 | + private array $imports = [], |
| 22 | + ) { |
| 23 | + parent::__construct($name); |
| 24 | + } |
| 25 | + |
| 26 | + public function typeCode(): string |
| 27 | + { |
| 28 | + $imports = implode( |
| 29 | + '', |
| 30 | + array_map( |
| 31 | + static fn (string $class): string => "\nuse {$class};", |
| 32 | + $this->imports, |
| 33 | + ) |
| 34 | + ); |
| 35 | + $imports = $imports === '' ? '' : $imports."\n"; |
| 36 | + $templates = implode( |
| 37 | + '', |
| 38 | + array_map( |
| 39 | + static fn(Template $template): string => $template->code(), |
| 40 | + $this->templates, |
| 41 | + ), |
| 42 | + ); |
| 43 | + $properties = implode( |
| 44 | + "\n", |
| 45 | + array_map( |
| 46 | + static fn(Parameter $parameter): string => $parameter->propertyCode(), |
| 47 | + $this->parameters, |
| 48 | + ), |
| 49 | + ); |
| 50 | + $phpDocParams = implode( |
| 51 | + '', |
| 52 | + array_filter( |
| 53 | + array_map( |
| 54 | + static fn(Parameter $parameter): ?string => $parameter->paramPhpDocCode(), |
| 55 | + $this->parameters, |
| 56 | + ), |
| 57 | + ), |
| 58 | + ); |
| 59 | + $params = implode( |
| 60 | + "\n", |
| 61 | + array_map( |
| 62 | + static fn(Parameter $parameter): string => $parameter->paramCode(), |
| 63 | + $this->parameters, |
| 64 | + ), |
| 65 | + ); |
| 66 | + $assignments = implode( |
| 67 | + "\n", |
| 68 | + array_map( |
| 69 | + static fn(Parameter $parameter): string => $parameter->propertyAssignCode(), |
| 70 | + $this->parameters, |
| 71 | + ), |
| 72 | + ); |
| 73 | + |
| 74 | + return <<<PHP |
| 75 | + <?php |
| 76 | + |
| 77 | + declare(strict_types=1); |
| 78 | + |
| 79 | + namespace Typhoon\\Type; |
| 80 | + {$imports} |
| 81 | + /** |
| 82 | + * This code is generated, do not modify it directly. |
| 83 | + * |
| 84 | + * @api{$templates} |
| 85 | + * @implements Type<{$this->TType}> |
| 86 | + */ |
| 87 | + final class {$this->shortClassName()} implements Type |
| 88 | + { |
| 89 | + {$properties} |
| 90 | + /** |
| 91 | + * @internal |
| 92 | + * @psalm-internal Typhoon\\Type{$phpDocParams} |
| 93 | + */ |
| 94 | + public function __construct( |
| 95 | + {$params} |
| 96 | + ) { |
| 97 | + {$assignments} |
| 98 | + } |
| 99 | +
|
| 100 | + public function accept(TypeVisitor \$visitor): mixed |
| 101 | + { |
| 102 | + return \$visitor->{$this->name}(\$this); |
| 103 | + } |
| 104 | + } |
| 105 | +
|
| 106 | + PHP; |
| 107 | + } |
| 108 | +} |
0 commit comments