1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\ApiTools\Swagger; |
4
|
|
|
|
5
|
|
|
use ByJG\ApiTools\Base\Body; |
6
|
|
|
use ByJG\ApiTools\Base\Schema; |
7
|
|
|
use ByJG\ApiTools\Exception\DefinitionNotFoundException; |
8
|
|
|
use ByJG\ApiTools\Exception\InvalidDefinitionException; |
9
|
|
|
use ByJG\ApiTools\Exception\InvalidRequestException; |
10
|
|
|
use ByJG\ApiTools\Exception\NotMatchedException; |
11
|
|
|
|
12
|
|
|
class SwaggerSchema extends Schema |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Initialize with schema data, which can be a PHP array or encoded as JSON. |
16
|
|
|
* |
17
|
|
|
* @param array|string $data |
18
|
|
|
* @param bool $allowNullValues |
19
|
|
|
*/ |
20
|
|
|
public function __construct(array|string $data, bool $allowNullValues = false) |
21
|
|
|
{ |
22
|
|
|
// when given a string, decode from JSON |
23
|
|
|
if (is_string($data)) { |
|
|
|
|
24
|
|
|
$data = json_decode($data, true); |
25
|
|
|
} |
26
|
|
|
$this->jsonFile = $data; |
27
|
|
|
$this->allowNullValues = $allowNullValues; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getHttpSchema() |
31
|
|
|
{ |
32
|
|
|
return isset($this->jsonFile['schemes']) ? $this->jsonFile['schemes'][0] : ''; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getHost() |
36
|
|
|
{ |
37
|
|
|
return $this->jsonFile['host'] ?? ''; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getBasePath(): string |
41
|
|
|
{ |
42
|
|
|
return $this->jsonFile['basePath'] ?? ''; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getServerUrl(): string |
46
|
|
|
{ |
47
|
|
|
$httpSchema = $this->getHttpSchema(); |
48
|
|
|
if (!empty($httpSchema)) { |
49
|
|
|
$httpSchema .= "://"; |
50
|
|
|
} |
51
|
|
|
$host = $this->getHost(); |
52
|
|
|
$basePath = $this->getBasePath(); |
53
|
|
|
return "$httpSchema$host$basePath"; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritDoc |
58
|
|
|
*/ |
59
|
|
|
protected function validateArguments(string $parameterIn, array $parameters, array $arguments): void |
60
|
|
|
{ |
61
|
|
|
foreach ($parameters as $parameter) { |
62
|
|
|
if ($parameter['in'] === $parameterIn |
63
|
|
|
&& $parameter['type'] === "integer" |
64
|
|
|
&& filter_var($arguments[$parameter['name']], FILTER_VALIDATE_INT) === false) { |
65
|
|
|
throw new NotMatchedException('Path expected an integer value'); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $name |
72
|
|
|
* @return mixed |
73
|
|
|
* @throws DefinitionNotFoundException |
74
|
|
|
* @throws InvalidDefinitionException |
75
|
|
|
*/ |
76
|
|
|
public function getDefinition($name): mixed |
77
|
|
|
{ |
78
|
|
|
$nameParts = explode('/', $name); |
79
|
|
|
|
80
|
|
|
if (count($nameParts) < 3 || $nameParts[0] !== '#') { |
81
|
|
|
throw new InvalidDefinitionException('Invalid Definition'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (!isset($this->jsonFile[$nameParts[1]][$nameParts[2]])) { |
85
|
|
|
throw new DefinitionNotFoundException("Definition '$name' not found"); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->jsonFile[$nameParts[1]][$nameParts[2]]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritDoc |
93
|
|
|
* @throws InvalidRequestException |
94
|
|
|
*/ |
95
|
|
|
public function getRequestParameters(string $path, string $method): Body |
96
|
|
|
{ |
97
|
|
|
$structure = $this->getPathDefinition($path, $method); |
98
|
|
|
|
99
|
|
|
if (!isset($structure[self::SWAGGER_PARAMETERS])) { |
100
|
|
|
return new SwaggerRequestBody($this, "$method $path", []); |
101
|
|
|
} |
102
|
|
|
return new SwaggerRequestBody($this, "$method $path", $structure[self::SWAGGER_PARAMETERS]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* OpenApi 2.0 doesn't describe null values, so this flag defines, |
107
|
|
|
* if match is ok when one of property |
108
|
|
|
* |
109
|
|
|
* @param string|bool $value |
110
|
|
|
*/ |
111
|
|
|
public function setAllowNullValues(string|bool $value): void |
112
|
|
|
{ |
113
|
|
|
$this->allowNullValues = (bool) $value; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritDoc |
118
|
|
|
*/ |
119
|
|
|
public function getResponseBody(Schema $schema, string $name, array $structure, bool $allowNullValues = false): Body |
120
|
|
|
{ |
121
|
|
|
return new SwaggerResponseBody($schema, $name, $structure, $allowNullValues); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|