/
SwaggerBody.php
189 lines (159 loc) · 5.46 KB
/
SwaggerBody.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace ByJG\Swagger;
use ByJG\Swagger\Exception\NotMatchedException;
abstract class SwaggerBody
{
/**
* @var \ByJG\Swagger\SwaggerSchema
*/
protected $swaggerSchema;
protected $structure;
protected $name;
/**
* OpenApi 2.0 does not describe null values, so this flag defines,
* if match is ok when one of property, which has type, is null
*
* @var bool
*/
protected $allowNullValues;
/**
* SwaggerRequestBody constructor.
*
* @param \ByJG\Swagger\SwaggerSchema $swaggerSchema
* @param string $name
* @param array $structure
* @param bool $allowNullValues
*/
public function __construct(SwaggerSchema $swaggerSchema, $name, $structure, $allowNullValues = false)
{
$this->swaggerSchema = $swaggerSchema;
$this->name = $name;
if (!is_array($structure)) {
throw new \InvalidArgumentException('I expected the structure to be an array');
}
$this->structure = $structure;
$this->allowNullValues = $allowNullValues;
}
abstract public function match($body);
protected function matchString($name, $schema, $body)
{
if (isset($schema['enum'])) {
if (!in_array($body, $schema['enum'])) {
throw new NotMatchedException("Value '$body' in '$name' not matched in ENUM. ", $this->structure);
};
}
return true;
}
protected function matchNumber($name, $body)
{
if (!is_numeric($body)) {
throw new NotMatchedException("Expected '$name' to be numeric, but found '$body'. ", $this->structure);
}
return true;
}
protected function matchBool($name, $body)
{
if (!is_bool($body)) {
throw new NotMatchedException("Expected '$name' to be boolean, but found '$body'. ", $this->structure);
}
return true;
}
protected function matchArray($name, $schema, $body)
{
foreach ((array)$body as $item) {
if (!isset($schema['items'])) { // If there is no type , there is no test.
continue;
}
$this->matchSchema($name, $schema['items'], $item);
}
return true;
}
/**
* @param string $name
* @param $schema
* @param array $body
* @return bool
* @throws \ByJG\Swagger\Exception\NotMatchedException
* @throws \Exception
*/
protected function matchSchema($name, $schema, $body)
{
if (isset($schema['type'])) {
$type = $schema['type'];
if (is_null($body)) {
return $this->matchNull($name, $type);
}
if ($type == 'string') {
return $this->matchString($name, $schema, $body);
}
if ($type == 'integer' || $type == 'float' || $schema['type'] == 'number') {
return $this->matchNumber($name, $body);
}
if ($type == 'bool' || $schema['type'] == 'boolean') {
return $this->matchBool($name, $body);
}
if ($type == 'array') {
return $this->matchArray($name, $schema, $body);
}
}
if (isset($schema['$ref'])) {
$defintion = $this->swaggerSchema->getDefintion($schema['$ref']);
return $this->matchSchema($schema['$ref'], $defintion, $body);
}
if (isset($schema['properties'])) {
if (!isset($schema['required'])) {
$schema['required'] = [];
}
foreach ($schema['properties'] as $prop => $def) {
$required = array_search($prop, $schema['required']);
if (!array_key_exists($prop, $body)) {
if ($required !== false) {
throw new NotMatchedException("Required property '$prop' in '$name' not found in object");
}
unset($body[$prop]);
continue;
}
$this->matchSchema($prop, $def, $body[$prop]);
unset($schema['properties'][$prop]);
if ($required !== false) {
unset($schema['required'][$required]);
}
unset($body[$prop]);
}
if (count($schema['required']) > 0) {
throw new NotMatchedException(
"The required property(ies) '"
. implode(', ', $schema['required'])
. "' does not exists in the body.",
$this->structure
);
}
if (count($body) > 0) {
throw new NotMatchedException(
"The property(ies) '"
. implode(', ', array_keys($body))
. "' has not defined in '$name'",
$body
);
}
return true;
}
throw new \Exception("Not all cases are defined. Please open an issue about this. Schema: $name");
}
/**
* @param $name
* @param $type
* @return bool
* @throws NotMatchedException
*/
protected function matchNull($name, $type)
{
if (false === $this->swaggerSchema->isAllowNullValues()) {
throw new NotMatchedException(
"Value of property '$name' is null, but should be of type '$type'",
$this->structure
);
}
return true;
}
}