1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\ApiTools\Base; |
4
|
|
|
|
5
|
|
|
use ByJG\ApiTools\Exception\DefinitionNotFoundException; |
6
|
|
|
use ByJG\ApiTools\Exception\HttpMethodNotFoundException; |
7
|
|
|
use ByJG\ApiTools\Exception\InvalidDefinitionException; |
8
|
|
|
use ByJG\ApiTools\Exception\NotMatchedException; |
9
|
|
|
use ByJG\ApiTools\Exception\PathNotFoundException; |
10
|
|
|
use ByJG\ApiTools\OpenApi\OpenApiResponseBody; |
11
|
|
|
use ByJG\ApiTools\OpenApi\OpenApiSchema; |
12
|
|
|
use ByJG\ApiTools\Swagger\SwaggerResponseBody; |
13
|
|
|
use ByJG\ApiTools\Swagger\SwaggerSchema; |
14
|
|
|
use ByJG\Util\Uri; |
15
|
|
|
|
16
|
|
|
abstract class Schema |
17
|
|
|
{ |
18
|
|
|
protected $jsonFile; |
19
|
|
|
protected $allowNullValues = false; |
20
|
|
|
protected $specificationVersion; |
21
|
|
|
|
22
|
|
|
const SWAGGER_PATHS = "paths"; |
23
|
|
|
const SWAGGER_PARAMETERS = "parameters"; |
24
|
|
|
const SWAGGER_COMPONENTS = "components"; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns the major specification version |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public function getSpecificationVersion() |
31
|
|
|
{ |
32
|
|
|
return $this->specificationVersion; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public static function getInstance($jsonFile, $extraArgs = false) |
36
|
|
|
{ |
37
|
|
|
$jsonFile = json_decode($jsonFile, true); |
38
|
|
|
if (isset($jsonFile['swagger'])) { |
39
|
|
|
return new SwaggerSchema($jsonFile, $extraArgs); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return new OpenApiSchema($jsonFile); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $path |
47
|
|
|
* @param $method |
48
|
|
|
* @return mixed |
49
|
|
|
* @throws DefinitionNotFoundException |
50
|
|
|
* @throws HttpMethodNotFoundException |
51
|
|
|
* @throws InvalidDefinitionException |
52
|
|
|
* @throws NotMatchedException |
53
|
|
|
* @throws PathNotFoundException |
54
|
|
|
*/ |
55
|
|
|
public function getPathDefinition($path, $method) |
56
|
|
|
{ |
57
|
|
|
$method = strtolower($method); |
58
|
|
|
|
59
|
|
|
$path = preg_replace('~^' . $this->getBasePath() . '~', '', $path); |
60
|
|
|
|
61
|
|
|
$uri = new Uri($path); |
62
|
|
|
|
63
|
|
|
// Try direct match |
64
|
|
|
if (isset($this->jsonFile[self::SWAGGER_PATHS][$uri->getPath()])) { |
65
|
|
|
if (isset($this->jsonFile[self::SWAGGER_PATHS][$uri->getPath()][$method])) { |
66
|
|
|
return $this->jsonFile[self::SWAGGER_PATHS][$uri->getPath()][$method]; |
67
|
|
|
} |
68
|
|
|
throw new HttpMethodNotFoundException("The http method '$method' not found in '$path'"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Try inline parameter |
72
|
|
|
foreach (array_keys($this->jsonFile[self::SWAGGER_PATHS]) as $pathItem) { |
73
|
|
|
if (strpos($pathItem, '{') === false) { |
74
|
|
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$pathItemPattern = '~^' . preg_replace('~{(.*?)}~', '(?<\1>[^/]+)', $pathItem) . '$~'; |
78
|
|
|
|
79
|
|
|
$matches = []; |
80
|
|
|
if (preg_match($pathItemPattern, $uri->getPath(), $matches)) { |
81
|
|
|
$pathDef = $this->jsonFile[self::SWAGGER_PATHS][$pathItem]; |
82
|
|
|
if (!isset($pathDef[$method])) { |
83
|
|
|
throw new HttpMethodNotFoundException("The http method '$method' not found in '$path'"); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$parametersPathMethod = []; |
87
|
|
|
$parametersPath = []; |
88
|
|
|
|
89
|
|
|
if (isset($pathDef[$method][self::SWAGGER_PARAMETERS])) { |
90
|
|
|
$parametersPathMethod = $pathDef[$method][self::SWAGGER_PARAMETERS]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (isset($pathDef[self::SWAGGER_PARAMETERS])) { |
94
|
|
|
$parametersPath = $pathDef[self::SWAGGER_PARAMETERS]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->validateArguments('path', array_merge($parametersPathMethod, $parametersPath), $matches); |
98
|
|
|
|
99
|
|
|
return $pathDef[$method]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
throw new PathNotFoundException('Path "' . $path . '" not found'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param $path |
108
|
|
|
* @param $method |
109
|
|
|
* @param $status |
110
|
|
|
* @return Body |
111
|
|
|
* @throws DefinitionNotFoundException |
112
|
|
|
* @throws HttpMethodNotFoundException |
113
|
|
|
* @throws InvalidDefinitionException |
114
|
|
|
* @throws NotMatchedException |
115
|
|
|
* @throws PathNotFoundException |
116
|
|
|
*/ |
117
|
|
|
public function getResponseParameters($path, $method, $status) |
118
|
|
|
{ |
119
|
|
|
$structure = $this->getPathDefinition($path, $method); |
120
|
|
|
|
121
|
|
|
if (!isset($structure['responses'][$status])) { |
122
|
|
|
if ($status != "200") { |
123
|
|
|
throw new InvalidDefinitionException("Could not found status code '$status' in '$path' and '$method'"); |
124
|
|
|
} |
125
|
|
|
$structure['responses'][$status] = ["description" => "Auto Generated OK"]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ($this instanceof SwaggerSchema) { |
129
|
|
|
return new SwaggerResponseBody($this, "$method $status $path", $structure['responses'][$status]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return new OpenApiResponseBody($this, "$method $status $path", $structure['responses'][$status]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* OpenApi 2.0 doesn't describe null values, so this flag defines, |
137
|
|
|
* if match is ok when one of property |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function isAllowNullValues() |
142
|
|
|
{ |
143
|
|
|
return $this->allowNullValues; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
abstract public function getServerUrl(); |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param $parameterIn |
150
|
|
|
* @param $parameters |
151
|
|
|
* @param $arguments |
152
|
|
|
* @throws DefinitionNotFoundException |
153
|
|
|
* @throws InvalidDefinitionException |
154
|
|
|
* @throws NotMatchedException |
155
|
|
|
*/ |
156
|
|
|
abstract protected function validateArguments($parameterIn, $parameters, $arguments); |
157
|
|
|
|
158
|
|
|
abstract public function getBasePath(); |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param $name |
162
|
|
|
* @return mixed |
163
|
|
|
* @throws DefinitionNotFoundException |
164
|
|
|
* @throws InvalidDefinitionException |
165
|
|
|
*/ |
166
|
|
|
abstract public function getDefinition($name); |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param $path |
170
|
|
|
* @param $method |
171
|
|
|
* @return Body |
172
|
|
|
* @throws HttpMethodNotFoundException |
173
|
|
|
* @throws PathNotFoundException |
174
|
|
|
*/ |
175
|
|
|
abstract public function getRequestParameters($path, $method); |
176
|
|
|
} |
177
|
|
|
|