1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Prometee\SwaggerClientGenerator\Base\Generator\Method; |
6
|
|
|
|
7
|
|
|
use Prometee\SwaggerClientGenerator\Base\Generator\AbstractGenerator; |
8
|
|
|
use Prometee\SwaggerClientGenerator\Base\Generator\Other\UsesGeneratorInterface; |
9
|
|
|
use Prometee\SwaggerClientGenerator\Base\View\Method\MethodParameterViewInterface; |
10
|
|
|
|
11
|
|
|
class MethodParameterGenerator extends AbstractGenerator implements MethodParameterGeneratorInterface |
12
|
|
|
{ |
13
|
|
|
/** @var UsesGeneratorInterface */ |
14
|
|
|
protected $usesGenerator; |
15
|
|
|
|
16
|
|
|
/** @var string[] */ |
17
|
|
|
protected $types = []; |
18
|
|
|
/** @var string */ |
19
|
|
|
protected $name; |
20
|
|
|
/** @var string|null */ |
21
|
|
|
protected $value; |
22
|
|
|
/** @var bool */ |
23
|
|
|
protected $byReference = false; |
24
|
|
|
/** @var string */ |
25
|
|
|
protected $description = ''; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param MethodParameterViewInterface $methodParameterView |
29
|
|
|
* @param UsesGeneratorInterface $usesGenerator |
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
MethodParameterViewInterface $methodParameterView, |
33
|
|
|
UsesGeneratorInterface $usesGenerator |
34
|
|
|
) |
35
|
|
|
{ |
36
|
|
|
$this->setView($methodParameterView); |
37
|
|
|
$this->usesGenerator = $usesGenerator; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritDoc |
42
|
|
|
*/ |
43
|
|
|
public function configure( |
44
|
|
|
array $types, |
45
|
|
|
string $name, |
46
|
|
|
?string $value = null, |
47
|
|
|
bool $byReference = false, |
48
|
|
|
string $description = '' |
49
|
|
|
): void |
50
|
|
|
{ |
51
|
|
|
$this->setTypes($types); |
52
|
|
|
$this->setName($name); |
53
|
|
|
$this->setValue($value); |
54
|
|
|
$this->setByReference($byReference); |
55
|
|
|
$this->setDescription($description); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritDoc} |
60
|
|
|
*/ |
61
|
|
|
public function getPhpName(): string |
62
|
|
|
{ |
63
|
|
|
return '$' . $this->getName(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritDoc} |
68
|
|
|
*/ |
69
|
|
|
public function getTypes(): array |
70
|
|
|
{ |
71
|
|
|
return $this->types; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritDoc} |
76
|
|
|
*/ |
77
|
|
|
public function setTypes(array $types): void |
78
|
|
|
{ |
79
|
|
|
$this->types = []; |
80
|
|
|
foreach ($types as $type) { |
81
|
|
|
$this->addType($type); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritDoc} |
87
|
|
|
*/ |
88
|
|
|
public function addType(string $type): void |
89
|
|
|
{ |
90
|
|
|
$type = $this->usesGenerator->guessUseOrReturnType($type); |
91
|
|
|
if (false === $this->hasType($type)) { |
92
|
|
|
$this->types[] = $type; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritDoc} |
98
|
|
|
*/ |
99
|
|
|
public function hasType(string $type): bool |
100
|
|
|
{ |
101
|
|
|
return false !== array_search($type, $this->types); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritDoc} |
106
|
|
|
*/ |
107
|
|
|
public function getPhpType(): ?string |
108
|
|
|
{ |
109
|
|
|
if (empty($this->types)) { |
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$phpType = ''; |
114
|
|
|
if (in_array('null', $this->types)) { |
115
|
|
|
$phpType = '?'; |
116
|
|
|
} |
117
|
|
|
foreach ($this->getTypes() as $type) { |
118
|
|
|
if (preg_match('#\[\]$#', $type)) { |
119
|
|
|
$phpType .= 'array'; |
120
|
|
|
|
121
|
|
|
break; |
122
|
|
|
} |
123
|
|
|
if ($type !== 'null') { |
124
|
|
|
$phpType .= $type; |
125
|
|
|
|
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $phpType; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritDoc} |
135
|
|
|
*/ |
136
|
|
|
public function getValueType(): ?string |
137
|
|
|
{ |
138
|
|
|
if (null === $this->value) { |
139
|
|
|
return null; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ($this->value === '[]') { |
143
|
|
|
return 'array'; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (preg_match('#^[\'"].*[\'"]$#', $this->value)) { |
147
|
|
|
return 'string'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (in_array($this->value, ['true', 'false'])) { |
151
|
|
|
return 'bool'; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (preg_match('#^[0-9]+$#', $this->value)) { |
155
|
|
|
return 'int'; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (preg_match('#^[0-9\.]+$#', $this->value)) { |
159
|
|
|
return 'float'; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return null; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritDoc} |
167
|
|
|
*/ |
168
|
|
|
public function getType(): string |
169
|
|
|
{ |
170
|
|
|
return implode('|', $this->types); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritDoc} |
175
|
|
|
*/ |
176
|
|
|
public function getName(): string |
177
|
|
|
{ |
178
|
|
|
return $this->name; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritDoc} |
183
|
|
|
*/ |
184
|
|
|
public function setName(string $name): void |
185
|
|
|
{ |
186
|
|
|
$this->name = $name; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritDoc} |
191
|
|
|
*/ |
192
|
|
|
public function getValue(): ?string |
193
|
|
|
{ |
194
|
|
|
return $this->value; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritDoc} |
199
|
|
|
*/ |
200
|
|
|
public function setValue(?string $value): void |
201
|
|
|
{ |
202
|
|
|
$this->value = $value; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritDoc} |
207
|
|
|
*/ |
208
|
|
|
public function isByReference(): bool |
209
|
|
|
{ |
210
|
|
|
return $this->byReference; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritDoc} |
215
|
|
|
*/ |
216
|
|
|
public function setByReference(bool $byReference): void |
217
|
|
|
{ |
218
|
|
|
$this->byReference = $byReference; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* {@inheritDoc} |
223
|
|
|
*/ |
224
|
|
|
public function getDescription(): string |
225
|
|
|
{ |
226
|
|
|
return $this->description; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* {@inheritDoc} |
231
|
|
|
*/ |
232
|
|
|
public function setDescription(string $description): void |
233
|
|
|
{ |
234
|
|
|
$this->description = $description; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|