Skip to content

Add support for items array type detection #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(
Expand Down
45 changes: 25 additions & 20 deletions src/Generator/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ final class Schema
* @param string $name
* @param string $namespace
* @param string $className
* @param OpenAPiSchema $operation
* @param OpenAPiSchema $schema
* @return iterable<Node>
*/
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);
Expand All @@ -30,30 +30,42 @@ 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 ?? ''
)
),
],
Class_::MODIFIER_PUBLIC
)
);

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_(
Expand All @@ -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',
Expand All @@ -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();
Expand Down