-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSchema.php
More file actions
134 lines (124 loc) · 5.61 KB
/
Copy pathSchema.php
File metadata and controls
134 lines (124 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace ApiClients\Tools\OpenApiClientGenerator\Generator;
use ApiClients\Tools\OpenApiClientGenerator\File;
use cebe\openapi\spec\Schema as OpenAPiSchema;
use Jawira\CaseConverter\Convert;
use PhpParser\Builder\Param;
use PhpParser\BuilderFactory;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Psr\Http\Message\RequestInterface;
use RingCentral\Psr7\Request;
final class Schema
{
/**
* @param string $name
* @param string $namespace
* @param string $className
* @param OpenAPiSchema $schema
* @return iterable<Node>
*/
public static function generate(string $name, string $namespace, string $className, OpenAPiSchema $schema, array $schemaClassNameMap): iterable
{
$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_(
$schema->title ?? $name
)
),
],
Class_::MODIFIER_PUBLIC
)
)->addStmt(
new Node\Stmt\ClassConst(
[
new Node\Const_(
'SCHEMA_DESCRIPTION',
new Node\Scalar\String_(
$schema->description ?? ''
)
),
],
Class_::MODIFIER_PUBLIC
)
);
foreach ($schema->properties as $propertyName => $property) {
$propertyName = str_replace([
'@',
'+',
'-',
], [
'_AT_',
'_PLUSES_',
'_MINUS_',
], $propertyName);
$propertyStmt = $factory->property($propertyName)->makePrivate();
$docBlock = [];
if (strlen($property->description) > 0) {
$docBlock[] = $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)) {
if ($property->type === 'array' && $property->items instanceof OpenAPiSchema) {
if (array_key_exists(spl_object_hash($property->items), $schemaClassNameMap)) {
$docBlock[] = '@var array<\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>';
$docBlock[] = '@\WyriHaximus\Hydrator\Attribute\HydrateArray(\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '::class)';
} elseif ($property->items->type === 'object') {
yield from self::generate($name . '::' . $propertyName, $namespace . '\\' . $className, (new Convert($propertyName))->toPascal(), $property->items, $schemaClassNameMap);
$docBlock[] = '@var array<\\' . $namespace . '\\' . $className . '\\' . (new Convert($propertyName))->toPascal() . '>';
$docBlock[] = '@\WyriHaximus\Hydrator\Attribute\HydrateArray(\\' . $namespace . '\\' . $className . '\\' . (new Convert($propertyName))->toPascal() . '::class)';
}
}
$t = str_replace([
'object',
'integer',
'any',
'boolean',
], [
'array',
'int',
'',
'bool',
], $property->type);
if ($t !== '') {
$dft = [];
if ($t !== 'array') {
$t = '?' . $t;
$dft = null;
}
$propertyStmt->setType($t)->setDefault($dft);
$method->setReturnType($t);
}
}
if (is_array($property->anyOf) && $property->anyOf[0] instanceof OpenAPiSchema && array_key_exists(spl_object_hash($property->anyOf[0]), $schemaClassNameMap)) {
$fqcnn = '\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->anyOf[0])];
$propertyStmt->setType('?' . $fqcnn)->setDefault(null);
$method->setReturnType('?' . $fqcnn);
$docBlock[] = '@\WyriHaximus\Hydrator\Attribute\Hydrate(' . $fqcnn . '::class)';
}
if ($property->type === 'object' && $property instanceof OpenAPiSchema && array_key_exists(spl_object_hash($property), $schemaClassNameMap)) {
$fqcnn = '\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property)];
$propertyStmt->setType('?' . $fqcnn)->setDefault(null);
$method->setReturnType('?' . $fqcnn);
$docBlock[] = '@\WyriHaximus\Hydrator\Attribute\Hydrate(' . $fqcnn . '::class)';
}
if (count($docBlock) > 0) {
$propertyStmt->setDocComment('/**' . PHP_EOL . ' * ' . implode(PHP_EOL . ' * ', $docBlock) . PHP_EOL .' */');
}
$class->addStmt($propertyStmt)->addStmt($method);
}
yield new File($namespace . '\\' . $className, $stmt->addStmt($class)->getNode());
}
}