1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Joli\Jane\OpenApi\Generator; |
4
|
|
|
|
5
|
|
|
use Joli\Jane\Generator\Context\Context; |
6
|
|
|
use Joli\Jane\Runtime\Reference; |
7
|
|
|
use Joli\Jane\Reference\Resolver; |
8
|
|
|
use Joli\Jane\OpenApi\Model\Schema; |
9
|
|
|
use PhpParser\Node\Arg; |
10
|
|
|
use PhpParser\Node\Expr; |
11
|
|
|
use PhpParser\Node\Name; |
12
|
|
|
use PhpParser\Node\Stmt; |
13
|
|
|
use PhpParser\Node\Scalar; |
14
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
15
|
|
|
|
16
|
|
|
trait OutputGeneratorTrait |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @return DenormalizerInterface |
20
|
|
|
*/ |
21
|
|
|
abstract protected function getDenormalizer(); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param $status |
25
|
|
|
* @param $schema |
26
|
|
|
* @param Context $context |
27
|
|
|
* |
28
|
|
|
* @return [string, null|Stmt\If_] |
|
|
|
|
29
|
|
|
*/ |
30
|
11 |
|
protected function createResponseDenormalizationStatement($status, $schema, Context $context, $reference) |
31
|
|
|
{ |
32
|
11 |
|
$jsonReference = $reference; |
33
|
11 |
|
$array = false; |
34
|
|
|
|
35
|
11 |
|
if ($schema instanceof Reference) { |
36
|
5 |
|
list($jsonReference, $schema) = $this->resolve($schema, Schema::class); |
37
|
|
|
} |
38
|
|
|
|
39
|
11 |
View Code Duplication |
if ($schema instanceof Schema && $schema->getType() == "array") { |
|
|
|
|
40
|
4 |
|
$array = true; |
41
|
4 |
|
$jsonReference .= '/items'; |
42
|
|
|
|
43
|
4 |
|
if ($schema->getItems() instanceof Reference) { |
44
|
3 |
|
list($jsonReference, ) = $this->resolve($schema->getItems(), Schema::class); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
11 |
|
$class = $context->getRegistry()->getClass($jsonReference); |
49
|
|
|
|
50
|
|
|
// Happens when reference resolve to a none object |
51
|
11 |
|
if ($class === null) { |
52
|
7 |
|
$returnType = 'null'; |
53
|
7 |
|
$returnStmt = new Stmt\Return_(new Expr\ConstFetch(new Name('null'))); |
54
|
|
|
} else { |
55
|
6 |
|
$class = $context->getRegistry()->getSchema($jsonReference)->getNamespace() . "\\Model\\" . $class->getName(); |
56
|
|
|
|
57
|
6 |
|
if ($array) { |
58
|
4 |
|
$class .= "[]"; |
59
|
|
|
} |
60
|
|
|
|
61
|
6 |
|
$returnType = "\\" . $class; |
62
|
6 |
|
$returnStmt = new Stmt\Return_(new Expr\MethodCall( |
63
|
6 |
|
new Expr\PropertyFetch(new Expr\Variable('this'), 'serializer'), |
64
|
6 |
|
'deserialize', |
65
|
|
|
[ |
66
|
6 |
|
new Arg(new Expr\Cast\String_(new Expr\MethodCall(new Expr\Variable('response'), 'getBody'))), |
67
|
6 |
|
new Arg(new Scalar\String_($class)), |
68
|
6 |
|
new Arg(new Scalar\String_('json')) |
69
|
|
|
] |
70
|
|
|
)); |
71
|
|
|
} |
72
|
|
|
|
73
|
11 |
|
if ($status === 'default') { |
74
|
1 |
|
return [$returnType, $returnStmt]; |
75
|
|
|
} |
76
|
|
|
|
77
|
11 |
|
return [$returnType, new Stmt\If_( |
78
|
11 |
|
new Expr\BinaryOp\Equal( |
79
|
11 |
|
new Scalar\String_($status), |
80
|
11 |
|
new Expr\MethodCall(new Expr\Variable('response'), 'getStatusCode') |
81
|
|
|
), |
82
|
|
|
[ |
83
|
11 |
|
'stmts' => [$returnStmt] |
84
|
|
|
] |
85
|
|
|
)]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param Reference $reference |
90
|
|
|
* @param $class |
91
|
|
|
* |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
6 |
View Code Duplication |
private function resolve(Reference $reference, $class) |
|
|
|
|
95
|
|
|
{ |
96
|
6 |
|
$result = $reference; |
97
|
|
|
|
98
|
|
|
do { |
99
|
6 |
|
$refString = (string) $reference->getMergedUri(); |
100
|
6 |
|
$result = $result->resolve(function ($data) use($result, $class) { |
101
|
6 |
|
return $this->getDenormalizer()->denormalize($data, $class, 'json', [ |
102
|
6 |
|
'document-origin' => (string) $result->getMergedUri()->withFragment('') |
103
|
|
|
]); |
104
|
6 |
|
}); |
105
|
6 |
|
} while ($result instanceof Reference); |
106
|
|
|
|
107
|
6 |
|
return [$refString, $result]; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.