Skip to content

Commit c5fa262

Browse files
authored
Merge pull request #3 from php-api-clients/add-schema-generator
Add Schema generator
2 parents f56f167 + d5bbd3e commit c5fa262

File tree

4 files changed

+152
-32
lines changed

4 files changed

+152
-32
lines changed

src/Generator.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use ApiClients\Tools\OpenApiClientGenerator\Generator\Operation;
66
use ApiClients\Tools\OpenApiClientGenerator\Generator\Path;
7+
use ApiClients\Tools\OpenApiClientGenerator\Generator\Schema;
78
use cebe\openapi\Reader;
89
use cebe\openapi\spec\OpenApi;
910
use Jawira\CaseConverter\Convert;
@@ -22,9 +23,23 @@ public function __construct(string $specUrl)
2223
public function generate(string $namespace, string $destinationPath)
2324
{
2425
$codePrinter = new Standard();
26+
foreach ($this->spec->components->schemas as $name => $schema) {
27+
$schemaClassName = str_replace(['{', '}'], ['Cb', 'Rcb'], (new Convert($name))->toPascal());
28+
@mkdir(dirname($destinationPath . '/Schema/' . $schemaClassName), 0777, true);
29+
file_put_contents($destinationPath . '/Schema/' . $schemaClassName . '.php', $codePrinter->prettyPrintFile([
30+
Schema::generate(
31+
$name,
32+
$namespace . str_replace('/', '\\', dirname('Schema/' . $schemaClassName)),
33+
strrev(explode('/', strrev($schemaClassName))[0]),
34+
$schema
35+
),
36+
]) . PHP_EOL);
37+
}
38+
2539
foreach ($this->spec->paths as $path => $pathItem) {
26-
$pathClassName = str_replace(['{', '}'], ['Cb', 'Rcb'], (new Convert($path))->toPascal()) . 'Path';
27-
@mkdir(dirname($destinationPath . '/Path' . $pathClassName), 0777, true);
40+
$pathClassName = str_replace(['{', '}'], ['Cb', 'Rcb'], (new Convert($path))->toPascal());
41+
echo $pathClassName, PHP_EOL;
42+
@mkdir(dirname($destinationPath . '/Path/' . $pathClassName), 0777, true);
2843
file_put_contents($destinationPath . '/Path/' . $pathClassName . '.php', $codePrinter->prettyPrintFile([
2944
Path::generate(
3045
$path,
@@ -35,7 +50,7 @@ public function generate(string $namespace, string $destinationPath)
3550
),
3651
]) . PHP_EOL);
3752
foreach ($pathItem->getOperations() as $method => $operation) {
38-
$operationClassName = (new Convert($operation->operationId))->fromTrain()->toPascal() . 'Operation';
53+
$operationClassName = (new Convert($operation->operationId))->fromTrain()->toPascal();
3954
$operations[$method] = $operationClassName;
4055

4156
@mkdir(dirname($destinationPath . '/Operation/' . $operationClassName), 0777, true);

src/Generator/Operation.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,32 @@ public static function generate(string $path, string $method, string $namespace,
4545
$requestReplaces = [];
4646
$query = [];
4747
foreach ($operation->parameters as $parameter) {
48-
$class->addStmt(
49-
$factory->
50-
property($parameter->name)->
51-
setDocComment('/**' . (string)$parameter->description . '**/')->
52-
setType(str_replace([
48+
$paramterStmt = $factory->
49+
property($parameter->name)->
50+
setDocComment('/**' . (string)$parameter->description . '**/');
51+
if ($parameter->schema->type !== null) {
52+
$paramterStmt->setType(str_replace([
5353
'integer',
5454
'any',
5555
], [
5656
'int',
5757
'',
58-
], $parameter->schema->type))
59-
);
58+
], $parameter->schema->type));
59+
}
60+
$class->addStmt($paramterStmt);
6061

61-
$param = (new Param(
62-
$parameter->name
63-
))->setType(
64-
str_replace([
65-
'integer',
66-
'any',
67-
], [
68-
'int',
69-
'',
70-
], $parameter->schema->type)
71-
);
62+
$param = new Param($parameter->name);
63+
if ($parameter->schema->default !== null) {
64+
$param->setType(
65+
str_replace([
66+
'integer',
67+
'any',
68+
], [
69+
'int',
70+
'',
71+
], $parameter->schema->type)
72+
);
73+
}
7274
if ($parameter->schema->default !== null) {
7375
$param->setDefault($parameter->schema->default);
7476
}

src/Generator/Path.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,18 @@ public static function generate(string $path, string $namespace, string $baseNam
3030
;
3131
$operationConstructorArguments = [];
3232
foreach ($operation->parameters as $parameter) {
33-
$param = (new Param(
34-
$parameter->name
35-
))->setType(
36-
str_replace([
37-
'integer',
38-
'any',
39-
], [
40-
'int',
41-
'',
42-
], $parameter->schema->type)
43-
);
33+
$param = new Param($parameter->name);
34+
if ($parameter->schema->default !== null) {
35+
$param->setType(
36+
str_replace([
37+
'integer',
38+
'any',
39+
], [
40+
'int',
41+
'',
42+
], $parameter->schema->type)
43+
);
44+
}
4445
if ($parameter->schema->default !== null) {
4546
$param->setDefault($parameter->schema->default);
4647
}

src/Generator/Schema.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace ApiClients\Tools\OpenApiClientGenerator\Generator;
4+
5+
use cebe\openapi\spec\Schema as OpenAPiSchema;
6+
use PhpParser\Builder\Param;
7+
use PhpParser\BuilderFactory;
8+
use PhpParser\Node;
9+
use PhpParser\Node\Stmt\Class_;
10+
use Psr\Http\Message\RequestInterface;
11+
use RingCentral\Psr7\Request;
12+
13+
final class Schema
14+
{
15+
/**
16+
* @param string $name
17+
* @param string $namespace
18+
* @param string $className
19+
* @param OpenAPiSchema $operation
20+
* @return iterable<Node>
21+
*/
22+
public static function generate(string $name, string $namespace, string $className, OpenAPiSchema $operation): Node
23+
{
24+
$factory = new BuilderFactory();
25+
$stmt = $factory->namespace($namespace);
26+
27+
$class = $factory->class($className)->makeFinal()->addStmt(
28+
new Node\Stmt\ClassConst(
29+
[
30+
new Node\Const_(
31+
'SCHEMA_TITLE',
32+
new Node\Scalar\String_(
33+
$operation->title ?? $name
34+
)
35+
),
36+
],
37+
Class_::MODIFIER_PUBLIC
38+
)
39+
)->addStmt(
40+
new Node\Stmt\ClassConst(
41+
[
42+
new Node\Const_(
43+
'SCHEMA_DESCRIPTION',
44+
new Node\Scalar\String_(
45+
$operation->description ?? ''
46+
)
47+
),
48+
],
49+
Class_::MODIFIER_PUBLIC
50+
)
51+
);
52+
53+
foreach ($operation->properties as $propertyName => $property) {
54+
$propertyStmt = $factory->property($propertyName)->makePrivate();
55+
if (strlen($property->description) > 0) {
56+
$propertyStmt->setDocComment('/**' . $property->description . '**/');
57+
}
58+
$method = $factory->method($propertyName)->makePublic()/*->setReturnType('string')*/->addStmt(
59+
new Node\Stmt\Return_(
60+
new Node\Expr\PropertyFetch(
61+
new Node\Expr\Variable('this'),
62+
$propertyName
63+
)
64+
)
65+
);
66+
if (is_string($property->type)) {
67+
$propertyStmt->setType(str_replace([
68+
'integer',
69+
'any',
70+
], [
71+
'int',
72+
'',
73+
], $property->type));
74+
$method->setReturnType(str_replace([
75+
'integer',
76+
'any',
77+
], [
78+
'int',
79+
'',
80+
], $property->type));
81+
}
82+
$class->addStmt($propertyStmt)->addStmt($method);
83+
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);
97+
}
98+
}
99+
100+
return $stmt->addStmt($class)->getNode();
101+
}
102+
}

0 commit comments

Comments
 (0)