-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClient.php
More file actions
66 lines (60 loc) · 2.31 KB
/
Copy pathClient.php
File metadata and controls
66 lines (60 loc) · 2.31 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
<?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 Client
{
/**
* @param array<string, string> $operations
* @return iterable<Node>
*/
public static function generate(string $operationGroup, string $namespace, string $className, array $operations): iterable
{
$factory = new BuilderFactory();
$stmt = $factory->namespace($namespace);
$class = $factory->class($className)->makeFinal();
foreach ($operations as $operationOperation => $operationDetails) {
$params = [];
$cn = str_replace('/', '\\', '\\' . $namespace . '\\' . $operationDetails['class']);
$method = $factory->method(lcfirst($operationOperation))->setReturnType($cn)->makePublic();
foreach ($operationDetails['operation']->parameters as $parameter) {
$params[] = new Node\Arg(new Node\Expr\Variable($parameter->name));
$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);
}
$method->addParam($param);
}
$class->addStmt($method->addStmt(
new Node\Stmt\Return_(
new Node\Expr\New_(
new Node\Name(
$cn
),
$params
)
)
));
}
yield new File($namespace . '\\' . $className, $stmt->addStmt($class)->getNode());
}
}