1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\ApiDescriptions package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\ApiDescriptions\Description\Standard\OpenApi; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\ApiDescriptions\Description\Operation; |
12
|
|
|
use KleijnWeb\ApiDescriptions\Description\Schema; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author John Kleijn <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class OpenApiOperation extends Operation |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Operation constructor. |
21
|
|
|
* |
22
|
|
|
* @param \stdClass $definition |
23
|
|
|
* @param string $path |
24
|
|
|
* @param string $method |
25
|
|
|
* @param array $pathParameters |
26
|
|
|
*/ |
27
|
|
|
public function __construct(\stdClass $definition, string $path, string $method, array $pathParameters = []) |
28
|
|
|
{ |
29
|
|
|
$this->path = $path; |
30
|
|
|
$this->method = $method; |
31
|
|
|
$this->parameters = $pathParameters; |
32
|
|
|
|
33
|
|
|
if (isset($definition->parameters)) { |
34
|
|
|
foreach ($definition->parameters as $parameterDefinition) { |
35
|
|
|
$this->parameters[] = new OpenApiParameter($parameterDefinition); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (isset($definition->responses)) { |
40
|
|
|
$hasOkResponse = false; |
41
|
|
|
foreach ($definition->responses as $code => $responseDefinition) { |
42
|
|
|
$code = (string)$code; |
43
|
|
|
if ($code === 'default' || substr((string)$code, 1) === '2') { |
44
|
|
|
$hasOkResponse = true; |
45
|
|
|
} |
46
|
|
|
$code = (int)$code; |
47
|
|
|
$this->responses[$code] = new OpenApiResponse($code, $responseDefinition); |
48
|
|
|
} |
49
|
|
|
if (!$hasOkResponse) { |
50
|
|
|
$this->responses[200] = new OpenApiResponse(200, (object)[]); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$schemaDefinition = (object)[]; |
55
|
|
View Code Duplication |
if (!isset($definition->parameters)) { |
|
|
|
|
56
|
|
|
$schemaDefinition->type = 'null'; |
57
|
|
|
$this->requestSchema = Schema::get($schemaDefinition); |
58
|
|
|
} else { |
59
|
|
|
$schemaDefinition->type = 'object'; |
60
|
|
|
$schemaDefinition->required = []; |
61
|
|
|
$schemaDefinition->properties = (object)[]; |
62
|
|
|
|
63
|
|
|
foreach ($this->parameters as $parameter) { |
64
|
|
|
if ($parameter->isRequired()) { |
65
|
|
|
$schemaDefinition->required[] = $parameter->getName(); |
66
|
|
|
} |
67
|
|
|
$schemaDefinition->properties->{$parameter->getName()} = $parameter->getSchema()->getDefinition(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->requestSchema = Schema::get($schemaDefinition); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.