-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSchema.php
More file actions
105 lines (97 loc) · 3.86 KB
/
Copy pathSchema.php
File metadata and controls
105 lines (97 loc) · 3.86 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
<?php
namespace ApiClients\Tools\OpenApiClientGenerator\Generator;
use cebe\openapi\spec\Schema as OpenAPiSchema;
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): 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_(
$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 = trim($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 && array_key_exists(spl_object_hash($property->items), $schemaClassNameMap)) {
$docBlock[] = '@var array<\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>';
}
$t = str_replace([
'integer',
'any',
'boolean',
], [
'int',
'',
'bool',
], $property->type);
if ($t !== '') {
$dft = [];
if ($t !== 'array') {
$t = '?' . $t;
$dft = null;
}
$propertyStmt->setType($t)->setDefault($dft);
$method->setReturnType($t);
}
} else 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);
}
if (count($docBlock) > 0) {
$propertyStmt->setDocComment('/**' . PHP_EOL . ' * ' . implode(PHP_EOL . ' * ', $docBlock) . PHP_EOL .' */');
}
$class->addStmt($propertyStmt)->addStmt($method);
}
return $stmt->addStmt($class)->getNode();
}
}