-
Notifications
You must be signed in to change notification settings - Fork 126
/
DenormalizerGenerator.php
215 lines (190 loc) · 9.8 KB
/
DenormalizerGenerator.php
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
namespace Jane\Component\JsonSchema\Generator\Normalizer;
use Jane\Component\JsonSchema\Generator\Context\Context;
use Jane\Component\JsonSchema\Generator\Naming;
use Jane\Component\JsonSchema\Guesser\Guess\ClassGuess;
use Jane\Component\JsonSchema\Guesser\Guess\Type;
use PhpParser\Comment\Doc;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt;
trait DenormalizerGenerator
{
/**
* The naming service.
*
* @return Naming
*/
abstract protected function getNaming();
/**
* Create method to check if denormalization is supported.
*
* @param string $modelFqdn Fully Qualified name of the model class denormalized
* @param bool $symfony7 Use Symfony 7 prototype or not
*
* @return Stmt\ClassMethod
*/
protected function createSupportsDenormalizationMethod(string $modelFqdn, bool $symfony7)
{
return new Stmt\ClassMethod('supportsDenormalization', [
'type' => Stmt\Class_::MODIFIER_PUBLIC,
'returnType' => 'bool',
'params' => [
$symfony7 ? new Param(new Expr\Variable('data'), type: 'mixed') : new Param(new Expr\Variable('data')),
$symfony7 ? new Param(new Expr\Variable('type'), type: 'string') : new Param(new Expr\Variable('type')),
new Param(new Expr\Variable('format'), new Expr\ConstFetch(new Name('null')), 'string'),
new Param(new Expr\Variable('context'), new Expr\Array_(), 'array'),
],
'stmts' => [new Stmt\Return_(new Expr\BinaryOp\Identical(new Expr\Variable('type'), new Scalar\String_($modelFqdn)))],
]);
}
protected function createDenormalizeMethod(string $modelFqdn, Context $context, ClassGuess $classGuess, bool $symfony7): Stmt\ClassMethod
{
$context->refreshScope();
$objectVariable = new Expr\Variable('object');
$dataVariable = new Expr\Variable('data');
$statements = [];
if ($this->useReference) {
$statements[] = new Stmt\If_(
new Expr\Isset_([new Expr\ArrayDimFetch($dataVariable, new Scalar\String_('$ref'))]),
[
'stmts' => [
new Stmt\Return_(new Expr\New_(new Name('Reference'), [
new Arg(new Expr\ArrayDimFetch($dataVariable, new Scalar\String_('$ref'))),
new Arg(new Expr\ArrayDimFetch(new Expr\Variable('context'), new Scalar\String_('document-origin'))),
])),
],
]
);
$statements[] = new Stmt\If_(
new Expr\Isset_([new Expr\ArrayDimFetch($dataVariable, new Scalar\String_('$recursiveRef'))]),
[
'stmts' => [
new Stmt\Return_(new Expr\New_(new Name('Reference'), [
new Arg(new Expr\ArrayDimFetch($dataVariable, new Scalar\String_('$recursiveRef'))),
new Arg(new Expr\ArrayDimFetch(new Expr\Variable('context'), new Scalar\String_('document-origin'))),
])),
],
]
);
}
$statements[] = new Stmt\Expression(new Expr\Assign($objectVariable, new Expr\New_(new Name('\\' . $modelFqdn))));
foreach ($classGuess->getProperties() as $property) {
if (Type::TYPE_FLOAT !== $property->getType()->getName()) {
continue;
}
$baseCondition = new Expr\FuncCall(new Name('\array_key_exists'), [
new Arg(new Scalar\String_($property->getName())),
new Arg($dataVariable),
]);
$arrayElement = new Expr\ArrayDimFetch($dataVariable, new Scalar\String_($property->getName()));
$intCondition = new Expr\FuncCall(new Name('\is_int'), [$arrayElement]);
$condition = new Expr\BinaryOp\BooleanAnd($baseCondition, $intCondition);
$castFloat = new Stmt\Expression(new Expr\Assign($arrayElement, new Expr\Cast\Double($arrayElement)));
$statements[] = new Stmt\If_($condition, ['stmts' => [$castFloat]]);
}
if ($this->validation) {
$schema = $context->getCurrentSchema();
$contextVariable = new Expr\Variable('context');
$constraintFqdn = $schema->getNamespace() . '\\Validator\\' . $this->naming->getConstraintName($classGuess->getName());
$statements[] = new Stmt\If_(new Expr\BooleanNot(new Expr\BinaryOp\Coalesce(new Expr\ArrayDimFetch($contextVariable, new Scalar\String_('skip_validation')), new Expr\ConstFetch(new Name('false')))), ['stmts' => [
new Stmt\Expression(new Expr\MethodCall(new Expr\Variable('this'), 'validate', [
new Arg($dataVariable), new Arg(new Expr\New_(new Name('\\' . $constraintFqdn))),
])),
]]);
}
$denormalizeMethodStatements = $this->denormalizeMethodStatements($classGuess, $context);
if (\count($denormalizeMethodStatements) > 0) {
array_unshift($statements, ...$denormalizeMethodStatements);
}
$unset = \count($classGuess->getExtensionsType()) > 0;
$statements[] = new Stmt\If_(new Expr\BinaryOp\BooleanOr(new Expr\BinaryOp\Identical(new Expr\ConstFetch(new Name('null')), $dataVariable), new Expr\BinaryOp\Identical(new Expr\ConstFetch(new Name('false')), new Expr\FuncCall(new Name('\is_array'), [new Arg($dataVariable)]))), [
'stmts' => [new Stmt\Return_($objectVariable)],
]);
foreach ($classGuess->getProperties() as $property) {
$propertyVar = new Expr\ArrayDimFetch($dataVariable, new Scalar\String_($property->getName()));
list($denormalizationStatements, $outputVar) = $property->getType()->createDenormalizationStatement($context, $propertyVar);
$baseCondition = new Expr\FuncCall(new Name('\array_key_exists'), [
new Arg(new Scalar\String_($property->getName())),
new Arg($dataVariable),
]);
$fullCondition = $baseCondition;
$mutatorStmt = array_merge($denormalizationStatements, [
new Stmt\Expression(new Expr\MethodCall($objectVariable, $this->getNaming()->getPrefixedMethodName('set', $property->getAccessorName()), [$outputVar])),
], $unset ? [new Stmt\Unset_([$propertyVar])] : []);
if (!$context->isStrict() || $property->isNullable()) {
$fullCondition = new Expr\BinaryOp\BooleanAnd(
$baseCondition,
new Expr\BinaryOp\NotIdentical(
$propertyVar,
new Expr\ConstFetch(new Name('null'))
)
);
}
$statements[] = new Stmt\If_($fullCondition, [
'stmts' => $mutatorStmt,
]);
if (!$context->isStrict() || $property->isNullable()) {
$invertCondition = new Expr\BinaryOp\BooleanAnd(
$baseCondition,
new Expr\BinaryOp\Identical(
$propertyVar,
new Expr\ConstFetch(new Name('null'))
)
);
$statements[] = new Stmt\ElseIf_($invertCondition, [
new Stmt\Expression(new Expr\MethodCall($objectVariable, $this->getNaming()->getPrefixedMethodName('set', $property->getAccessorName()), [new Expr\ConstFetch(new Name('null'))])),
]);
}
}
$patternCondition = [];
$loopKeyVar = new Expr\Variable($context->getUniqueVariableName('key'));
$loopValueVar = new Expr\Variable($context->getUniqueVariableName('value'));
foreach ($classGuess->getExtensionsType() as $pattern => $type) {
list($denormalizationStatements, $outputVar) = $type->createDenormalizationStatement($context, $loopValueVar);
$patternCondition[] = new Stmt\If_(
new Expr\FuncCall(new Name('preg_match'), [
new Arg(new Expr\ConstFetch(new Name("'/" . str_replace('/', '\/', $pattern) . "/'"))),
new Arg(new Expr\Cast\String_($loopKeyVar)),
]),
[
'stmts' => array_merge($denormalizationStatements, [
new Stmt\Expression(new Expr\Assign(new Expr\ArrayDimFetch($objectVariable, $loopKeyVar), $outputVar)),
]),
]
);
}
if (\count($patternCondition) > 0) {
$statements[] = new Stmt\Foreach_($dataVariable, $loopValueVar, [
'keyVar' => $loopKeyVar,
'stmts' => $patternCondition,
]);
}
$statements[] = new Stmt\Return_($objectVariable);
return new Stmt\ClassMethod('denormalize', [
'type' => Stmt\Class_::MODIFIER_PUBLIC,
'returnType' => $symfony7 ? 'mixed' : null,
'params' => [
$symfony7 ? new Param($dataVariable, type: 'mixed') : new Param($dataVariable),
$symfony7 ? new Param(new Expr\Variable('type'), type: 'string') : new Param(new Expr\Variable('type')),
$symfony7 ? new Param(new Expr\Variable('format'), new Expr\ConstFetch(new Name('null')), 'string') : new Param(new Expr\Variable('format'), new Expr\ConstFetch(new Name('null'))),
new Param(new Expr\Variable('context'), new Expr\Array_(), 'array'),
],
'stmts' => $statements,
], [
'comments' => $symfony7 ? [] : [new Doc(<<<EOD
/**
* @return mixed
*/
EOD
)],
]);
}
protected function denormalizeMethodStatements(ClassGuess $classGuess, Context $context): array
{
return [];
}
}