|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* User: jg |
|
4
|
|
|
* Date: 22/05/17 |
|
5
|
|
|
* Time: 09:29 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace ByJG\Swagger; |
|
9
|
|
|
|
|
10
|
|
|
use ByJG\Swagger\Exception\DefinitionNotFoundException; |
|
11
|
|
|
use ByJG\Swagger\Exception\HttpMethodNotFoundException; |
|
12
|
|
|
use ByJG\Swagger\Exception\InvalidDefinitionException; |
|
13
|
|
|
use ByJG\Swagger\Exception\NotMatchedException; |
|
14
|
|
|
use ByJG\Swagger\Exception\PathNotFoundException; |
|
15
|
|
|
|
|
16
|
|
|
class SwaggerSchema |
|
17
|
|
|
{ |
|
18
|
|
|
protected $jsonFile; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct($jsonFile) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->jsonFile = json_decode($jsonFile, true); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function getHttpSchema() |
|
26
|
|
|
{ |
|
27
|
|
|
return isset($this->jsonFile['schemes']) ? $this->jsonFile['schemes'][0] : ''; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function getHost() |
|
31
|
|
|
{ |
|
32
|
|
|
return isset($this->jsonFile['host']) ? $this->jsonFile['host'] : ''; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getBasePath() |
|
36
|
|
|
{ |
|
37
|
|
|
return isset($this->jsonFile['basePath']) ? $this->jsonFile['basePath'] : ''; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getPathDefinition($path, $method) |
|
41
|
|
|
{ |
|
42
|
|
|
$method = strtolower($method); |
|
43
|
|
|
|
|
44
|
|
|
$path = preg_replace('~^' . $this->getBasePath() . '~', '', $path); |
|
45
|
|
|
|
|
46
|
|
|
// Try direct match |
|
47
|
|
|
if (isset($this->jsonFile['paths'][$path])) { |
|
48
|
|
|
if (isset($this->jsonFile['paths'][$path][$method])) { |
|
49
|
|
|
return $this->jsonFile['paths'][$path][$method]; |
|
50
|
|
|
} |
|
51
|
|
|
throw new HttpMethodNotFoundException("The http method '$method' not found in '$path'"); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Try inline parameter |
|
55
|
|
|
foreach (array_keys($this->jsonFile['paths']) as $pathItem) { |
|
56
|
|
|
if (strpos($pathItem, '{') === false) { |
|
57
|
|
|
continue; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$pathItemPattern = '~^' . preg_replace('~\{(.*?)\}~', '(?<\1>[^/]+)', $pathItem) . '$~'; |
|
61
|
|
|
|
|
62
|
|
|
$matches = []; |
|
63
|
|
|
if (preg_match($pathItemPattern, $path, $matches)) { |
|
64
|
|
|
$pathDef = $this->jsonFile['paths'][$pathItem]; |
|
65
|
|
|
if (!isset($pathDef[$method])) { |
|
66
|
|
|
throw new HttpMethodNotFoundException("The http method '$method' not found in '$path'"); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->validateArguments('path', $pathDef[$method]['parameters'], $matches); |
|
70
|
|
|
|
|
71
|
|
|
return $pathDef[$method]; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
throw new PathNotFoundException('Path "' . $path . '" not found'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function validateArguments($parameterIn, $parameters, $arguments) |
|
79
|
|
|
{ |
|
80
|
|
|
foreach ($parameters as $parameter) { |
|
81
|
|
|
if ($parameter['in'] === $parameterIn) { |
|
82
|
|
|
if ($parameter['type'] === "integer" |
|
83
|
|
|
&& filter_var($arguments[$parameter['name']], FILTER_VALIDATE_INT) === false) { |
|
84
|
|
|
throw new NotMatchedException('Path expected an integer value'); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getDefintion($name) |
|
91
|
|
|
{ |
|
92
|
|
|
$nameParts = explode('/', $name); |
|
93
|
|
|
|
|
94
|
|
|
if (count($nameParts) < 3 || $nameParts[0] != '#') { |
|
95
|
|
|
throw new InvalidDefinitionException('Invalid Definition'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if (!isset($this->jsonFile[$nameParts[1]][$nameParts[2]])) { |
|
99
|
|
|
throw new DefinitionNotFoundException("Definition '$name' not found"); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this->jsonFile[$nameParts[1]][$nameParts[2]]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function getRequestParameters($path, $method) |
|
106
|
|
|
{ |
|
107
|
|
|
$structure = $this->getPathDefinition($path, $method); |
|
108
|
|
|
|
|
109
|
|
|
if (!isset($structure['parameters'])) { |
|
110
|
|
|
return new SwaggerRequestBody($this, "$method $path", []); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return new SwaggerRequestBody($this, "$method $path", $structure['parameters']); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function getResponseParameters($path, $method, $status) |
|
117
|
|
|
{ |
|
118
|
|
|
$structure = $this->getPathDefinition($path, $method); |
|
119
|
|
|
|
|
120
|
|
|
if (!isset($structure['responses'][$status])) { |
|
121
|
|
|
throw new InvalidDefinitionException("Could not found status code '$status' in '$path' and '$method'"); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return new SwaggerResponseBody($this, "$method $status $path", $structure['responses'][$status]); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|