forked from DoclerLabs/api-client-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutatorAccessorClassGeneratorAbstract.php
More file actions
189 lines (160 loc) · 6.38 KB
/
MutatorAccessorClassGeneratorAbstract.php
File metadata and controls
189 lines (160 loc) · 6.38 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
declare(strict_types=1);
namespace DoclerLabs\ApiClientGenerator\Generator;
use DoclerLabs\ApiClientException\RequestValidationException;
use DoclerLabs\ApiClientGenerator\Ast\Builder\CodeBuilder;
use DoclerLabs\ApiClientGenerator\Entity\Constraint\ConstraintInterface;
use DoclerLabs\ApiClientGenerator\Entity\Constraint\MaxItemsConstraint;
use DoclerLabs\ApiClientGenerator\Entity\Constraint\MinItemsConstraint;
use DoclerLabs\ApiClientGenerator\Entity\Field;
use DoclerLabs\ApiClientGenerator\Input\Specification;
use DoclerLabs\ApiClientGenerator\Naming\SchemaNaming;
use DoclerLabs\ApiClientGenerator\Output\Php\PhpFileCollection;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\NodeAbstract;
abstract class MutatorAccessorClassGeneratorAbstract extends GeneratorAbstract
{
protected CodeBuilder $builder;
abstract public function generate(Specification $specification, PhpFileCollection $fileRegistry): void;
protected function generateValidationStmts(Field $field): array
{
return array_filter(
[
...$this->generateConstraints($field),
]
);
}
/**
* @param array<int, NodeAbstract> $additionalStatements
*/
protected function generateSet(Field $field, array $additionalStatements = []): ClassMethod
{
$statements = $this->generateValidationStmts($field);
$thrownExceptionMap = empty($statements) ? [] : ['RequestValidationException' => true];
$docType = $field->getPhpDocType($field->isNullable());
$param = $this->builder
->param($field->getPhpVariableName())
->setType($field->getPhpTypeHint(), $field->isNullable())
->setDocBlockType($docType)
->getNode();
$statements[] = $this->builder->assign(
$this->builder->localPropertyFetch($field->getPhpVariableName()),
$this->builder->var($field->getPhpVariableName())
);
$statements = array_merge($statements, $additionalStatements);
$return = $this->builder->return($this->builder->var('this'));
$returnType = 'self';
return $this->builder
->method($this->getSetMethodName($field))
->makePublic()
->addParam($param)
->addStmts($statements)
->addStmt($return)
->setReturnType($returnType)
->composeDocBlock([$param], $returnType, array_keys($thrownExceptionMap))
->getNode();
}
protected function generateGet(Field $field): ClassMethod
{
$return = $this->builder->return($this->builder->localPropertyFetch($field->getPhpVariableName()));
return $this->builder
->method($this->getGetMethodName($field))
->makePublic()
->addStmt($return)
->setReturnType($field->getPhpTypeHint(), $field->isNullable() || !$field->isRequired())
->composeDocBlock([], $field->getPhpDocType())
->getNode();
}
protected function generateProperty(Field $field): Property
{
return $this->builder->localProperty(
$field->getPhpVariableName(),
$field->getPhpTypeHint(),
$field->getPhpDocType(),
nullable: $field->isOptional() || $field->isNullable(),
readonly: $field->isRequired()
);
}
protected function getSetMethodName(Field $field): string
{
return sprintf('set%s', ucfirst($field->getPhpVariableName()));
}
protected function getGetMethodName(Field $field): string
{
return sprintf('get%s', ucfirst($field->getPhpVariableName()));
}
protected function getHasMethodName(Field $field): string
{
return sprintf('has%s', ucfirst($field->getPhpVariableName()));
}
protected function generateEnumStatements(Field $field): array
{
if ($this->phpVersion->isEnumSupported()) {
return [];
}
$statements = [];
$enumValues = $field->isArrayOfEnums() ? $field->getArrayItem()->getEnumValues() : $field->getEnumValues();
if (!empty($enumValues)) {
foreach ($enumValues as $enumValue) {
if (is_string($enumValue)) {
$constName = SchemaNaming::getEnumConstName($field, $enumValue);
$statements[] = $this->builder->constant(
$constName,
$this->builder->val($enumValue)
);
}
}
}
return $statements;
}
protected function generateConstraints(Field $root): array
{
$stmts = [];
/** @var ConstraintInterface $constraint */
foreach ($root->getConstraints() as $constraint) {
if (!$constraint->exists()) {
continue;
}
$propertyVar = $this->builder->var($root->getPhpVariableName());
if (
$constraint instanceof MaxItemsConstraint
|| $constraint instanceof MinItemsConstraint
) {
$exceptionMessage = $this->builder->funcCall(
'sprintf',
[
'Invalid %s value. ' . $constraint->getExceptionMessage(),
$root->getName(),
]
);
} else {
$exceptionMessage = $this->builder->funcCall(
'sprintf',
[
'Invalid %s value. Given: `%s`. ' . $constraint->getExceptionMessage(),
$root->getName(),
$propertyVar,
]
);
}
$ifConditionExpr = $constraint->getIfCondition($propertyVar, $this->builder);
if ($root->isNullable()) {
$ifConditionExpr = $this->builder->and(
$this->builder->notEquals($propertyVar, $this->builder->val(null)),
$ifConditionExpr
);
}
$stmts[] = $this->builder->if(
$ifConditionExpr,
[
$this->builder->throw('RequestValidationException', $exceptionMessage),
]
);
}
if (!empty($stmts)) {
$this->addImport(RequestValidationException::class);
}
return $stmts;
}
}