-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOperation.php
More file actions
141 lines (134 loc) · 5.19 KB
/
Copy pathOperation.php
File metadata and controls
141 lines (134 loc) · 5.19 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
135
136
137
138
139
140
141
<?php
namespace ApiClients\Tools\OpenApiClientGenerator\Generator;
use ApiClients\Tools\OpenApiClientGenerator\File;
use cebe\openapi\spec\Operation as OpenAPiOperation;
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 Operation
{
/**
* @param string $path
* @param string $method
* @param string $namespace
* @param string $className
* @param OpenAPiOperation $operation
* @return iterable<Node>
*/
public static function generate(string $path, string $method, string $namespace, string $className, OpenAPiOperation $operation): iterable
{
$factory = new BuilderFactory();
$stmt = $factory->namespace($namespace);
$class = $factory->class($className)->makeFinal()->addStmt(
new Node\Stmt\ClassConst(
[
new Node\Const_(
'OPERATION_ID',
new Node\Scalar\String_(
$operation->operationId
)
),
],
Class_::MODIFIER_PRIVATE
)
)->addStmt(
$factory->method('operationId')->makePublic()->setReturnType('string')->addStmt(
new Node\Stmt\Return_(
new Node\Expr\ClassConstFetch(
new Node\Name('self'),
'OPERATION_ID'
)
)
)
// )->setDocComment('/**' . var_export($operation, true) . '**/');
);
$constructor = $factory->method('__construct');
$requestReplaces = [];
$query = [];
foreach ($operation->parameters as $parameter) {
$paramterStmt = $factory->property($parameter->name);
if (strlen((string)$parameter->description) > 0) {
$paramterStmt->setDocComment('/**' . (string)$parameter->description . '**/');
}
if ($parameter->schema->type !== null) {
$paramterStmt->setType(str_replace([
'integer',
'any',
'boolean',
], [
'int',
'',
'bool',
], $parameter->schema->type));
}
$class->addStmt($paramterStmt->makeReadonly()->makePrivate());
$param = new Param($parameter->name);
if ($parameter->schema->type !== null) {
$param->setType(
str_replace([
'integer',
'any',
'boolean',
], [
'int',
'',
'bool',
], $parameter->schema->type)
);
}
if ($parameter->schema->default !== null) {
$param->setDefault($parameter->schema->default);
}
$constructor->addParam(
$param
)->addStmt(
new Node\Expr\Assign(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
$parameter->name
),
new Node\Expr\Variable($parameter->name),
)
);
if ($parameter->in === 'path' || $parameter->in === 'query') {
$requestReplaces['{' . $parameter->name . '}'] = new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
$parameter->name
);
}
if ($parameter->in === 'query') {
$query[] = $parameter->name . '={' . $parameter->name . '}';
}
}
$class->addStmt($constructor);
$class->addStmt(
$factory->method('createRequest')->setReturnType('\\' . RequestInterface::class)->addStmt(
new Node\Stmt\Return_(
new Node\Expr\New_(
new Node\Name(
'\\' . Request::class
),
[
new Node\Arg(new Node\Scalar\String_($method)),
new Node\Arg(new Node\Expr\FuncCall(
new Node\Name('\str_replace'),
[
new Node\Expr\Array_(array_map(static fn (string $key): Node\Expr\ArrayItem => new Node\Expr\ArrayItem(new Node\Scalar\String_($key)), array_keys($requestReplaces))),
new Node\Expr\Array_(array_values($requestReplaces)),
new Node\Scalar\String_(rtrim($path . '?' . implode('&', $query), '?')),
]
)),
]
)
)
)
);
$class->addStmt(
$factory->method('validateResponse')
);
yield new File($namespace . '\\' . $className, $stmt->addStmt($class)->getNode());
}
}