* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class TypeScript extends CodeGeneratorAbstract { /** * @inheritDoc */ public function getFileName(string $file): string { return $file . '.ts'; } protected function newTypeGenerator(array $mapping): GeneratorInterface { return new Type\TypeScript($mapping); } protected function writeStruct( string $name, array $properties, ?string $extends, ?array $generics, StructType $origin, ): string { $code = 'export interface ' . $name; if (!empty($generics)) { $code.= '<' . implode(', ', $generics) . '>'; } if (!empty($extends)) { $code.= ' extends ' . $extends; } $code.= ' {' . "\n"; foreach ($properties as $name => $property) { /** @var Code\Property $property */ $code.= $this->indent . $name . ($property->isRequired() ? '' : '?') . ': ' . $property->getType() . "\n"; } $code.= '}' . "\n"; return $code; } protected function writeMap(string $name, string $type, MapType $origin): string { return 'export type ' . $name . ' = ' . $type . ';' . "\n"; } protected function writeArray(string $name, string $type, ArrayType $origin): string { return 'export type ' . $name . ' = ' . $type . ';' . "\n"; } protected function writeUnion(string $name, string $type, UnionType $origin): string { return 'export type ' . $name . ' = ' . $type . ';' . "\n"; } protected function writeIntersection(string $name, string $type, IntersectionType $origin): string { return 'export type ' . $name . ' = ' . $type . ';' . "\n"; } protected function writeReference(string $name, string $type, ReferenceType $origin): string { return 'export type ' . $name . ' = ' . $type . ';' . "\n"; } protected function writeHeader(TypeAbstract $origin): string { $code = ''; $imports = $this->getImports($origin); if (!empty($imports)) { $code.= "\n"; $code.= implode("\n", $imports); $code.= "\n"; } $code.= "\n"; $comment = $origin->getDescription(); if (!empty($comment)) { $code.= '/**' . "\n"; $code.= ' * ' . $comment . "\n"; $code.= ' */' . "\n"; } return $code; } private function getImports(TypeInterface $origin): array { $refs = []; TypeUtil::walk($origin, function(TypeInterface $type) use (&$refs){ if ($type instanceof ReferenceType) { $refs[$type->getRef()] = $type->getRef(); if ($type->getTemplate()) { foreach ($type->getTemplate() as $ref) { $refs[$ref] = $ref; } } } elseif ($type instanceof StructType && $type->getExtends()) { $refs[$type->getExtends()] = $type->getExtends(); } }); $imports = []; foreach ($refs as $ref) { [$ns, $name] = TypeUtil::split($ref); $imports[] = 'import {' . $name . '} from "./' . $name . '";'; } return $imports; } } __halt_compiler();----SIGNATURE:----QTsOt/yWXgwmkBtGooKtN6g2OmGHDPsiab8IC9mo50u8U5Ymn2Jd/WYChYXgAlJHHWnBaNqxOtkO6Grawq9260Xdvj+AD9+8PZv7rR1Mh1u+OdfpoHliOX2tEaREx6QevP1SUxO5la3MqDS1dhdgYXX9wP6VIzjfCFftyabbRRoHKo1gSVzQjfwym6Kl6w/4VlHPmGaj7GJy7DbJcJkKSbYZzim8cMnEMIbWcqNM6QmuIyhv9eYagTIB737cwkmcpYPxoDgxt+GKT1E48x5948eyy/QeblRiWpirL/7waTjYGbui8QaNvI9gozGnE6zl6YRynN6DbBzuRAU1mcqp1xd4HcfXM6ICOdGXgFglK9EThOgUP2y2pHgQSVLCIHmkZnoyhct0v7KyrUmaKKOTILcgpMcxIYx98hI4QykYJvbU8XmpmaOz6xAKdTKJ0sugyvdJxXr2rZPoHXDZDkWtUKunTt2U7/u44Ztoy+hKZ34b/FYSkRYRbL2Z6LAZgXCDIJLM3wji9yfr6JiFm58UOW+TcXFaKOcLQKOj0YqfvaPoje7KhqGoKWgy1AP55x5FZbYm7iHRLRsBgpDO8P/VwlBkMQQ9OCmQKfDDF5A6E8OoFcshI1K5g0OM9DEUAPB3xeB/H2U/RqJ2cCujEgwV9ET9hcv98kb2AUa9MgVvF/Q=----ATTACHMENT:----MjgxNjQzMDg0NjM3ODUxMiAxMjQzNDA5MjAyMDUxNzA0IDQxODI0NjI5ODg1OTA5MTI=