Skip to content

Commit 3aae539

Browse files
committed
Add support for items array type detection
1 parent c5fa262 commit 3aae539

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

src/Generator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public function __construct(string $specUrl)
2323
public function generate(string $namespace, string $destinationPath)
2424
{
2525
$codePrinter = new Standard();
26+
$schemaClassNameMap = [];
27+
foreach ($this->spec->components->schemas as $name => $schema) {
28+
$schemaClassNameMap[spl_object_hash($schema)] = str_replace(['{', '}'], ['Cb', 'Rcb'], (new Convert($name))->toPascal());
29+
}
2630
foreach ($this->spec->components->schemas as $name => $schema) {
2731
$schemaClassName = str_replace(['{', '}'], ['Cb', 'Rcb'], (new Convert($name))->toPascal());
2832
@mkdir(dirname($destinationPath . '/Schema/' . $schemaClassName), 0777, true);
@@ -31,14 +35,14 @@ public function generate(string $namespace, string $destinationPath)
3135
$name,
3236
$namespace . str_replace('/', '\\', dirname('Schema/' . $schemaClassName)),
3337
strrev(explode('/', strrev($schemaClassName))[0]),
34-
$schema
38+
$schema,
39+
$schemaClassNameMap
3540
),
3641
]) . PHP_EOL);
3742
}
3843

3944
foreach ($this->spec->paths as $path => $pathItem) {
4045
$pathClassName = str_replace(['{', '}'], ['Cb', 'Rcb'], (new Convert($path))->toPascal());
41-
echo $pathClassName, PHP_EOL;
4246
@mkdir(dirname($destinationPath . '/Path/' . $pathClassName), 0777, true);
4347
file_put_contents($destinationPath . '/Path/' . $pathClassName . '.php', $codePrinter->prettyPrintFile([
4448
Path::generate(

src/Generator/Schema.php

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ final class Schema
1616
* @param string $name
1717
* @param string $namespace
1818
* @param string $className
19-
* @param OpenAPiSchema $operation
19+
* @param OpenAPiSchema $schema
2020
* @return iterable<Node>
2121
*/
22-
public static function generate(string $name, string $namespace, string $className, OpenAPiSchema $operation): Node
22+
public static function generate(string $name, string $namespace, string $className, OpenAPiSchema $schema, array $schemaClassNameMap): Node
2323
{
2424
$factory = new BuilderFactory();
2525
$stmt = $factory->namespace($namespace);
@@ -30,30 +30,42 @@ public static function generate(string $name, string $namespace, string $classNa
3030
new Node\Const_(
3131
'SCHEMA_TITLE',
3232
new Node\Scalar\String_(
33-
$operation->title ?? $name
33+
$schema->title ?? $name
3434
)
3535
),
3636
],
3737
Class_::MODIFIER_PUBLIC
3838
)
3939
)->addStmt(
40+
new Node\Stmt\ClassConst(
41+
[
42+
new Node\Const_(
43+
'SPL_HASH',
44+
new Node\Scalar\String_(
45+
spl_object_hash($schema)
46+
)
47+
),
48+
],
49+
Class_::MODIFIER_PUBLIC
50+
))->addStmt(
4051
new Node\Stmt\ClassConst(
4152
[
4253
new Node\Const_(
4354
'SCHEMA_DESCRIPTION',
4455
new Node\Scalar\String_(
45-
$operation->description ?? ''
56+
$schema->description ?? ''
4657
)
4758
),
4859
],
4960
Class_::MODIFIER_PUBLIC
5061
)
5162
);
5263

53-
foreach ($operation->properties as $propertyName => $property) {
64+
foreach ($schema->properties as $propertyName => $property) {
5465
$propertyStmt = $factory->property($propertyName)->makePrivate();
66+
$docBlock = [];
5567
if (strlen($property->description) > 0) {
56-
$propertyStmt->setDocComment('/**' . $property->description . '**/');
68+
$docBlock[] = $property->description;
5769
}
5870
$method = $factory->method($propertyName)->makePublic()/*->setReturnType('string')*/->addStmt(
5971
new Node\Stmt\Return_(
@@ -64,6 +76,9 @@ public static function generate(string $name, string $namespace, string $classNa
6476
)
6577
);
6678
if (is_string($property->type)) {
79+
if ($property->type === 'array' && array_key_exists(spl_object_hash($property->items), $schemaClassNameMap)) {
80+
$docBlock[] = '@var array<' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>';
81+
}
6782
$propertyStmt->setType(str_replace([
6883
'integer',
6984
'any',
@@ -79,22 +94,12 @@ public static function generate(string $name, string $namespace, string $classNa
7994
'',
8095
], $property->type));
8196
}
82-
$class->addStmt($propertyStmt)->addStmt($method);
8397

84-
$param = (new Param(
85-
$propertyName
86-
))/*->setType(
87-
str_replace([
88-
'integer',
89-
'any',
90-
], [
91-
'int',
92-
'',
93-
], $property->type)
94-
)*/;
95-
if ($property->default !== null) {
96-
$param->setDefault($property->default);
98+
if (count($docBlock) > 0) {
99+
$propertyStmt->setDocComment('/**' . PHP_EOL . ' * ' . implode(PHP_EOL . ' * ', $docBlock) . PHP_EOL .' */');
97100
}
101+
102+
$class->addStmt($propertyStmt)->addStmt($method);
98103
}
99104

100105
return $stmt->addStmt($class)->getNode();

0 commit comments

Comments
 (0)