|
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
|
7 |
|
|
|
22
|
|
|
public function __construct(Parser $parser, DenormalizerInterface $denormalizer) |
|
23
|
7 |
|
{ |
|
24
|
|
|
parent::__construct($parser); |
|
25
|
7 |
|
|
|
26
|
7 |
|
$this->denormalizer = $denormalizer; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritDoc} |
|
31
|
|
|
* |
|
32
|
|
|
* @param $parameter BodyParameter |
|
33
|
2 |
|
*/ |
|
34
|
|
|
public function generateMethodParameter($parameter, Context $context) |
|
35
|
2 |
|
{ |
|
36
|
|
|
$name = Inflector::camelize($parameter->getName()); |
|
37
|
2 |
|
|
|
38
|
|
|
list($class, $array) = $this->getClass($parameter, $context); |
|
39
|
2 |
|
|
|
40
|
2 |
|
if (null === $array || true === $array) { |
|
41
|
1 |
|
if ($class == "array") { |
|
42
|
|
|
return new Node\Param($name, null, "array"); |
|
43
|
|
|
} |
|
44
|
2 |
|
|
|
45
|
|
|
return new Node\Param($name); |
|
46
|
|
|
} |
|
47
|
1 |
|
|
|
48
|
|
|
return new Node\Param($name, null, $class); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritDoc} |
|
53
|
|
|
* |
|
54
|
|
|
* @param $parameter BodyParameter |
|
55
|
2 |
|
*/ |
|
56
|
|
|
public function generateDocParameter($parameter, Context $context) |
|
57
|
2 |
|
{ |
|
58
|
|
|
list($class, $array) = $this->getClass($parameter, $context); |
|
|
|
|
|
|
59
|
2 |
|
|
|
60
|
|
|
if (null === $class) { |
|
61
|
|
|
return sprintf('%s $%s %s', 'mixed', Inflector::camelize($parameter->getName()), $parameter->getDescription() ?: ''); |
|
62
|
|
|
} |
|
63
|
2 |
|
|
|
64
|
|
|
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
|
2 |
|
*/ |
|
73
|
|
|
protected function getClass(BodyParameter $parameter, Context $context) |
|
74
|
2 |
|
{ |
|
75
|
2 |
|
$resolvedSchema = null; |
|
76
|
2 |
|
$reference = null; |
|
77
|
|
|
$array = false; |
|
78
|
2 |
|
$schema = $parameter->getSchema(); |
|
79
|
1 |
|
|
|
80
|
1 |
|
if ($schema instanceof Reference) { |
|
81
|
|
|
list($reference, $resolvedSchema) = $this->resolveSchema($schema, Schema::class); |
|
82
|
2 |
|
} |
|
83
|
|
|
|
|
84
|
|
View Code Duplication |
if ($schema instanceof Schema && $schema->getType() == "array" && $schema->getItems() instanceof Reference) { |
|
|
|
|
|
|
85
|
|
|
list($reference, $resolvedSchema) = $this->resolveSchema($schema->getItems(), Schema::class); |
|
86
|
|
|
$array = true; |
|
87
|
2 |
|
} |
|
88
|
2 |
|
|
|
89
|
|
|
if ($resolvedSchema === null) { |
|
90
|
|
|
return [$schema->getType(), null]; |
|
91
|
1 |
|
} |
|
92
|
1 |
|
|
|
93
|
|
|
$class = $context->getRegistry()->getClass($reference); |
|
94
|
1 |
|
$class = "\\" . $context->getRegistry()->getSchema($reference)->getNamespace() . "\\Model\\" . $class->getName(); |
|
95
|
|
|
|
|
96
|
|
|
if ($array) { |
|
97
|
|
|
$class .= "[]"; |
|
98
|
1 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
return [$class, $array]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param Reference $reference |
|
105
|
|
|
* @param $class |
|
106
|
|
|
* |
|
107
|
|
|
* @return mixed |
|
108
|
|
|
*/ |
|
109
|
|
View Code Duplication |
private function resolveSchema(Reference $reference, $class) |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
$result = $reference; |
|
112
|
|
|
|
|
113
|
|
|
do { |
|
114
|
|
|
$refString = (string) $reference->getMergedUri(); |
|
115
|
|
|
$result = $result->resolve(function ($data) use($result, $class) { |
|
116
|
|
|
return $this->denormalizer->denormalize($data, $class, 'json', [ |
|
117
|
|
|
'document-origin' => (string) $result->getMergedUri()->withFragment('') |
|
118
|
|
|
]); |
|
119
|
|
|
}); |
|
120
|
|
|
} while ($result instanceof Reference); |
|
121
|
|
|
|
|
122
|
|
|
return [$refString, $result]; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
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
$aand$care used. There was no need to assign$b.Instead, the list call could have been.