1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Joli\Jane\OpenApi\Generator\Parameter; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Inflector\Inflector; |
6
|
|
|
use Joli\Jane\Generator\Context\Context; |
7
|
|
|
use Joli\Jane\Runtime\Reference; |
8
|
|
|
use Joli\Jane\Reference\Resolver; |
9
|
|
|
use Joli\Jane\OpenApi\Model\BodyParameter; |
10
|
|
|
use Joli\Jane\OpenApi\Model\Schema; |
11
|
|
|
use PhpParser\Node; |
12
|
|
|
use PhpParser\Parser; |
13
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
14
|
|
|
|
15
|
|
|
class BodyParameterGenerator extends ParameterGenerator |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var DenormalizerInterface |
19
|
|
|
*/ |
20
|
|
|
private $denormalizer; |
21
|
|
|
|
22
|
13 |
|
public function __construct(Parser $parser, DenormalizerInterface $denormalizer) |
23
|
|
|
{ |
24
|
13 |
|
parent::__construct($parser); |
25
|
|
|
|
26
|
13 |
|
$this->denormalizer = $denormalizer; |
27
|
13 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritDoc} |
31
|
|
|
* |
32
|
|
|
* @param $parameter BodyParameter |
33
|
|
|
*/ |
34
|
3 |
|
public function generateMethodParameter($parameter, Context $context, $reference) |
35
|
|
|
{ |
36
|
3 |
|
$name = Inflector::camelize($parameter->getName()); |
37
|
|
|
|
38
|
3 |
|
list($class, $array) = $this->getClass($parameter, $context, $reference); |
39
|
|
|
|
40
|
3 |
|
if (null === $array || true === $array) { |
41
|
2 |
|
if ($class == "array") { |
42
|
1 |
|
return new Node\Param($name, null, "array"); |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
return new Node\Param($name); |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
return new Node\Param($name, null, $class); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
* |
54
|
|
|
* @param $parameter BodyParameter |
55
|
|
|
*/ |
56
|
3 |
|
public function generateDocParameter($parameter, Context $context, $reference) |
57
|
|
|
{ |
58
|
3 |
|
list($class, $array) = $this->getClass($parameter, $context, $reference); |
|
|
|
|
59
|
|
|
|
60
|
3 |
|
if (null === $class) { |
61
|
|
|
return sprintf('%s $%s %s', 'mixed', Inflector::camelize($parameter->getName()), $parameter->getDescription() ?: ''); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
return sprintf('%s $%s %s', $class, Inflector::camelize($parameter->getName()), $parameter->getDescription() ?: ''); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param BodyParameter $parameter |
69
|
|
|
* @param Context $context |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
3 |
|
protected function getClass(BodyParameter $parameter, Context $context, $reference) |
74
|
|
|
{ |
75
|
3 |
|
$resolvedSchema = null; |
76
|
3 |
|
$jsonReference = null; |
77
|
3 |
|
$array = false; |
78
|
3 |
|
$schema = $parameter->getSchema(); |
79
|
|
|
|
80
|
3 |
|
if ($schema instanceof Reference) { |
81
|
1 |
|
list($jsonReference, $resolvedSchema) = $this->resolveSchema($schema, Schema::class); |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
View Code Duplication |
if ($schema instanceof Schema && $schema->getType() == "array" && $schema->getItems() instanceof Reference) { |
|
|
|
|
85
|
|
|
list($jsonReference, $resolvedSchema) = $this->resolveSchema($schema->getItems(), Schema::class); |
86
|
|
|
$array = true; |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
if ($resolvedSchema === null) { |
90
|
3 |
|
if ($context->getRegistry()->hasClass($reference)) { |
91
|
1 |
|
return ["\\" . $context->getRegistry()->getSchema($reference)->getNamespace() . "\\Model\\" . $context->getRegistry()->getClass($reference)->getName(), false]; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
return [$schema->getType(), null]; |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
$class = $context->getRegistry()->getClass($jsonReference); |
98
|
|
|
|
99
|
|
|
// Happens when reference resolve to a none object |
100
|
1 |
|
if ($class === null) { |
101
|
|
|
return [$schema->getType(), null]; |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
$class = "\\" . $context->getRegistry()->getSchema($jsonReference)->getNamespace() . "\\Model\\" . $class->getName(); |
105
|
|
|
|
106
|
1 |
|
if ($array) { |
107
|
|
|
$class .= "[]"; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return [$class, $array]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param Reference $reference |
115
|
|
|
* @param $class |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
1 |
View Code Duplication |
private function resolveSchema(Reference $reference, $class) |
|
|
|
|
120
|
|
|
{ |
121
|
1 |
|
$result = $reference; |
122
|
|
|
|
123
|
|
|
do { |
124
|
1 |
|
$refString = (string) $reference->getMergedUri(); |
125
|
1 |
|
$result = $result->resolve(function ($data) use($result, $class) { |
126
|
1 |
|
return $this->denormalizer->denormalize($data, $class, 'json', [ |
127
|
1 |
|
'document-origin' => (string) $result->getMergedUri()->withFragment('') |
128
|
|
|
]); |
129
|
1 |
|
}); |
130
|
1 |
|
} while ($result instanceof Reference); |
131
|
|
|
|
132
|
1 |
|
return [$refString, $result]; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.