diff --git a/src/Generator.php b/src/Generator.php index 2c0386e..1e5fb96 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -4,6 +4,7 @@ use ApiClients\Tools\OpenApiClientGenerator\Generator\Operation; use ApiClients\Tools\OpenApiClientGenerator\Generator\Path; +use ApiClients\Tools\OpenApiClientGenerator\Generator\Schema; use cebe\openapi\Reader; use cebe\openapi\spec\OpenApi; use Jawira\CaseConverter\Convert; @@ -22,9 +23,23 @@ public function __construct(string $specUrl) public function generate(string $namespace, string $destinationPath) { $codePrinter = new Standard(); + foreach ($this->spec->components->schemas as $name => $schema) { + $schemaClassName = str_replace(['{', '}'], ['Cb', 'Rcb'], (new Convert($name))->toPascal()); + @mkdir(dirname($destinationPath . '/Schema/' . $schemaClassName), 0777, true); + file_put_contents($destinationPath . '/Schema/' . $schemaClassName . '.php', $codePrinter->prettyPrintFile([ + Schema::generate( + $name, + $namespace . str_replace('/', '\\', dirname('Schema/' . $schemaClassName)), + strrev(explode('/', strrev($schemaClassName))[0]), + $schema + ), + ]) . PHP_EOL); + } + foreach ($this->spec->paths as $path => $pathItem) { - $pathClassName = str_replace(['{', '}'], ['Cb', 'Rcb'], (new Convert($path))->toPascal()) . 'Path'; - @mkdir(dirname($destinationPath . '/Path' . $pathClassName), 0777, true); + $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( $path, @@ -35,7 +50,7 @@ public function generate(string $namespace, string $destinationPath) ), ]) . PHP_EOL); foreach ($pathItem->getOperations() as $method => $operation) { - $operationClassName = (new Convert($operation->operationId))->fromTrain()->toPascal() . 'Operation'; + $operationClassName = (new Convert($operation->operationId))->fromTrain()->toPascal(); $operations[$method] = $operationClassName; @mkdir(dirname($destinationPath . '/Operation/' . $operationClassName), 0777, true); diff --git a/src/Generator/Operation.php b/src/Generator/Operation.php index a5b81ec..718bfe7 100644 --- a/src/Generator/Operation.php +++ b/src/Generator/Operation.php @@ -45,30 +45,32 @@ public static function generate(string $path, string $method, string $namespace, $requestReplaces = []; $query = []; foreach ($operation->parameters as $parameter) { - $class->addStmt( - $factory-> - property($parameter->name)-> - setDocComment('/**' . (string)$parameter->description . '**/')-> - setType(str_replace([ + $paramterStmt = $factory-> + property($parameter->name)-> + setDocComment('/**' . (string)$parameter->description . '**/'); + if ($parameter->schema->type !== null) { + $paramterStmt->setType(str_replace([ 'integer', 'any', ], [ 'int', '', - ], $parameter->schema->type)) - ); + ], $parameter->schema->type)); + } + $class->addStmt($paramterStmt); - $param = (new Param( - $parameter->name - ))->setType( - str_replace([ - 'integer', - 'any', - ], [ - 'int', - '', - ], $parameter->schema->type) - ); + $param = new Param($parameter->name); + if ($parameter->schema->default !== null) { + $param->setType( + str_replace([ + 'integer', + 'any', + ], [ + 'int', + '', + ], $parameter->schema->type) + ); + } if ($parameter->schema->default !== null) { $param->setDefault($parameter->schema->default); } diff --git a/src/Generator/Path.php b/src/Generator/Path.php index d833c30..b70855b 100644 --- a/src/Generator/Path.php +++ b/src/Generator/Path.php @@ -30,17 +30,18 @@ public static function generate(string $path, string $namespace, string $baseNam ; $operationConstructorArguments = []; foreach ($operation->parameters as $parameter) { - $param = (new Param( - $parameter->name - ))->setType( - str_replace([ - 'integer', - 'any', - ], [ - 'int', - '', - ], $parameter->schema->type) - ); + $param = new Param($parameter->name); + if ($parameter->schema->default !== null) { + $param->setType( + str_replace([ + 'integer', + 'any', + ], [ + 'int', + '', + ], $parameter->schema->type) + ); + } if ($parameter->schema->default !== null) { $param->setDefault($parameter->schema->default); } diff --git a/src/Generator/Schema.php b/src/Generator/Schema.php new file mode 100644 index 0000000..dae975a --- /dev/null +++ b/src/Generator/Schema.php @@ -0,0 +1,102 @@ + + */ + public static function generate(string $name, string $namespace, string $className, OpenAPiSchema $operation): Node + { + $factory = new BuilderFactory(); + $stmt = $factory->namespace($namespace); + + $class = $factory->class($className)->makeFinal()->addStmt( + new Node\Stmt\ClassConst( + [ + new Node\Const_( + 'SCHEMA_TITLE', + new Node\Scalar\String_( + $operation->title ?? $name + ) + ), + ], + Class_::MODIFIER_PUBLIC + ) + )->addStmt( + new Node\Stmt\ClassConst( + [ + new Node\Const_( + 'SCHEMA_DESCRIPTION', + new Node\Scalar\String_( + $operation->description ?? '' + ) + ), + ], + Class_::MODIFIER_PUBLIC + ) + ); + + foreach ($operation->properties as $propertyName => $property) { + $propertyStmt = $factory->property($propertyName)->makePrivate(); + if (strlen($property->description) > 0) { + $propertyStmt->setDocComment('/**' . $property->description . '**/'); + } + $method = $factory->method($propertyName)->makePublic()/*->setReturnType('string')*/->addStmt( + new Node\Stmt\Return_( + new Node\Expr\PropertyFetch( + new Node\Expr\Variable('this'), + $propertyName + ) + ) + ); + if (is_string($property->type)) { + $propertyStmt->setType(str_replace([ + 'integer', + 'any', + ], [ + 'int', + '', + ], $property->type)); + $method->setReturnType(str_replace([ + 'integer', + 'any', + ], [ + 'int', + '', + ], $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); + } + } + + return $stmt->addStmt($class)->getNode(); + } +}