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\Stmt; |
12
|
|
|
use PhpParser\Node\Scalar; |
13
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
14
|
|
|
|
15
|
|
|
trait OutputGeneratorTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @return DenormalizerInterface |
19
|
|
|
*/ |
20
|
|
|
abstract protected function getDenormalizer(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param $status |
24
|
|
|
* @param $schema |
25
|
|
|
* @param Context $context |
26
|
|
|
* |
27
|
|
|
* @return [string, null|Stmt\If_] |
|
|
|
|
28
|
7 |
|
*/ |
29
|
|
|
protected function createResponseDenormalizationStatement($status, $schema, Context $context) |
30
|
7 |
|
{ |
31
|
7 |
|
$resolvedSchema = null; |
32
|
|
|
$reference = null; |
33
|
7 |
|
$array = false; |
34
|
2 |
|
|
35
|
2 |
|
if ($schema instanceof Reference) { |
36
|
|
|
list($reference, $resolvedSchema) = $this->resolve($schema, Schema::class); |
37
|
7 |
|
} |
38
|
2 |
|
|
39
|
2 |
View Code Duplication |
if ($schema instanceof Schema && $schema->getType() == "array" && $schema->getItems() instanceof Reference) { |
|
|
|
|
40
|
2 |
|
list($reference, $resolvedSchema) = $this->resolve($schema->getItems(), Schema::class); |
41
|
|
|
$array = true; |
42
|
7 |
|
} |
43
|
6 |
|
|
44
|
|
|
if ($resolvedSchema === null) { |
45
|
|
|
return [null, null]; |
46
|
2 |
|
} |
47
|
2 |
|
|
48
|
|
|
$class = $context->getRegistry()->getClass($reference); |
49
|
2 |
|
$class = $context->getRegistry()->getSchema($reference)->getNamespace() . "\\Model\\" . $class->getName(); |
50
|
2 |
|
|
51
|
2 |
|
if ($array) { |
52
|
|
|
$class .= "[]"; |
53
|
2 |
|
} |
54
|
2 |
|
|
55
|
2 |
|
return ["\\" . $class, new Stmt\If_( |
56
|
2 |
|
new Expr\BinaryOp\Equal( |
57
|
2 |
|
new Scalar\String_($status), |
58
|
|
|
new Expr\MethodCall(new Expr\Variable('response'), 'getStatusCode') |
59
|
|
|
), |
60
|
2 |
|
[ |
61
|
2 |
|
'stmts' => [ |
62
|
2 |
|
new Stmt\Return_(new Expr\MethodCall( |
63
|
|
|
new Expr\PropertyFetch(new Expr\Variable('this'), 'serializer'), |
64
|
2 |
|
'deserialize', |
65
|
2 |
|
[ |
66
|
2 |
|
new Arg(new Expr\Cast\String_(new Expr\MethodCall(new Expr\Variable('response'), 'getBody'))), |
67
|
2 |
|
new Arg(new Scalar\String_($class)), |
68
|
2 |
|
new Arg(new Scalar\String_('json')) |
69
|
2 |
|
] |
70
|
2 |
|
)) |
71
|
2 |
|
] |
72
|
|
|
] |
73
|
|
|
)]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Reference $reference |
78
|
|
|
* @param $class |
79
|
|
|
* |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
private function resolve(Reference $reference, $class) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$result = $reference; |
85
|
|
|
|
86
|
|
|
do { |
87
|
|
|
$refString = (string) $reference->getMergedUri(); |
88
|
|
|
$result = $result->resolve(function ($data) use($result, $class) { |
89
|
|
|
return $this->getDenormalizer()->denormalize($data, $class, 'json', [ |
90
|
|
|
'document-origin' => (string) $result->getMergedUri()->withFragment('') |
91
|
|
|
]); |
92
|
|
|
}); |
93
|
|
|
} while ($result instanceof Reference); |
94
|
|
|
|
95
|
|
|
return [$refString, $result]; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.