From 3aae539493e4b012cda833a697bf4c61c7c5a02b Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Mon, 3 May 2021 10:40:55 +0200 Subject: [PATCH] Add support for items array type detection --- src/Generator.php | 8 +++++-- src/Generator/Schema.php | 45 ++++++++++++++++++++++------------------ 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/Generator.php b/src/Generator.php index 1e5fb96..dc9a55c 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -23,6 +23,10 @@ public function __construct(string $specUrl) public function generate(string $namespace, string $destinationPath) { $codePrinter = new Standard(); + $schemaClassNameMap = []; + foreach ($this->spec->components->schemas as $name => $schema) { + $schemaClassNameMap[spl_object_hash($schema)] = str_replace(['{', '}'], ['Cb', 'Rcb'], (new Convert($name))->toPascal()); + } foreach ($this->spec->components->schemas as $name => $schema) { $schemaClassName = str_replace(['{', '}'], ['Cb', 'Rcb'], (new Convert($name))->toPascal()); @mkdir(dirname($destinationPath . '/Schema/' . $schemaClassName), 0777, true); @@ -31,14 +35,14 @@ public function generate(string $namespace, string $destinationPath) $name, $namespace . str_replace('/', '\\', dirname('Schema/' . $schemaClassName)), strrev(explode('/', strrev($schemaClassName))[0]), - $schema + $schema, + $schemaClassNameMap ), ]) . PHP_EOL); } foreach ($this->spec->paths as $path => $pathItem) { $pathClassName = str_replace(['{', '}'], ['Cb', 'Rcb'], (new Convert($path))->toPascal()); - echo $pathClassName, PHP_EOL; @mkdir(dirname($destinationPath . '/Path/' . $pathClassName), 0777, true); file_put_contents($destinationPath . '/Path/' . $pathClassName . '.php', $codePrinter->prettyPrintFile([ Path::generate( diff --git a/src/Generator/Schema.php b/src/Generator/Schema.php index dae975a..d76cb44 100644 --- a/src/Generator/Schema.php +++ b/src/Generator/Schema.php @@ -16,10 +16,10 @@ final class Schema * @param string $name * @param string $namespace * @param string $className - * @param OpenAPiSchema $operation + * @param OpenAPiSchema $schema * @return iterable */ - public static function generate(string $name, string $namespace, string $className, OpenAPiSchema $operation): Node + public static function generate(string $name, string $namespace, string $className, OpenAPiSchema $schema, array $schemaClassNameMap): Node { $factory = new BuilderFactory(); $stmt = $factory->namespace($namespace); @@ -30,19 +30,30 @@ public static function generate(string $name, string $namespace, string $classNa new Node\Const_( 'SCHEMA_TITLE', new Node\Scalar\String_( - $operation->title ?? $name + $schema->title ?? $name ) ), ], Class_::MODIFIER_PUBLIC ) )->addStmt( + new Node\Stmt\ClassConst( + [ + new Node\Const_( + 'SPL_HASH', + new Node\Scalar\String_( + spl_object_hash($schema) + ) + ), + ], + Class_::MODIFIER_PUBLIC + ))->addStmt( new Node\Stmt\ClassConst( [ new Node\Const_( 'SCHEMA_DESCRIPTION', new Node\Scalar\String_( - $operation->description ?? '' + $schema->description ?? '' ) ), ], @@ -50,10 +61,11 @@ public static function generate(string $name, string $namespace, string $classNa ) ); - foreach ($operation->properties as $propertyName => $property) { + foreach ($schema->properties as $propertyName => $property) { $propertyStmt = $factory->property($propertyName)->makePrivate(); + $docBlock = []; if (strlen($property->description) > 0) { - $propertyStmt->setDocComment('/**' . $property->description . '**/'); + $docBlock[] = $property->description; } $method = $factory->method($propertyName)->makePublic()/*->setReturnType('string')*/->addStmt( new Node\Stmt\Return_( @@ -64,6 +76,9 @@ public static function generate(string $name, string $namespace, string $classNa ) ); if (is_string($property->type)) { + if ($property->type === 'array' && array_key_exists(spl_object_hash($property->items), $schemaClassNameMap)) { + $docBlock[] = '@var array<' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>'; + } $propertyStmt->setType(str_replace([ 'integer', 'any', @@ -79,22 +94,12 @@ public static function generate(string $name, string $namespace, string $classNa '', ], $property->type)); } - $class->addStmt($propertyStmt)->addStmt($method); - $param = (new Param( - $propertyName - ))/*->setType( - str_replace([ - 'integer', - 'any', - ], [ - 'int', - '', - ], $property->type) - )*/; - if ($property->default !== null) { - $param->setDefault($property->default); + if (count($docBlock) > 0) { + $propertyStmt->setDocComment('/**' . PHP_EOL . ' * ' . implode(PHP_EOL . ' * ', $docBlock) . PHP_EOL .' */'); } + + $class->addStmt($propertyStmt)->addStmt($method); } return $stmt->addStmt($class)->getNode();