1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: jg |
4
|
|
|
* Date: 22/05/17 |
5
|
|
|
* Time: 10:52 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace ByJG\Swagger; |
9
|
|
|
|
10
|
|
|
use ByJG\Swagger\Exception\InvalidDefinitionException; |
11
|
|
|
use ByJG\Swagger\Exception\RequiredArgumentNotFound; |
12
|
|
|
|
13
|
|
|
class SwaggerRequestBody extends SwaggerBody |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param $body |
17
|
|
|
* @return bool |
18
|
|
|
* @throws Exception\DefinitionNotFoundException |
19
|
|
|
* @throws Exception\GenericSwaggerException |
20
|
|
|
* @throws Exception\InvalidRequestException |
21
|
|
|
* @throws Exception\NotMatchedException |
22
|
|
|
* @throws InvalidDefinitionException |
23
|
|
|
* @throws RequiredArgumentNotFound |
24
|
|
|
*/ |
25
|
|
|
public function match($body) |
26
|
|
|
{ |
27
|
|
|
if ($this->swaggerSchema->getSpecificationVersion() === '3') { |
28
|
|
|
if (isset($this->structure['content']) || isset($this->structure['$ref'])) { |
29
|
|
|
if (isset($this->structure['required']) && $this->structure['required'] === true && empty($body)) { |
30
|
|
|
throw new RequiredArgumentNotFound('The body is required but it is empty'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (isset($this->structure['$ref'])) { |
34
|
|
|
return $this->matchSchema($this->name, $this->structure, $body); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $this->matchSchema($this->name, $this->structure['content'][key($this->structure['content'])]['schema'], $body); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (!empty($body)) { |
41
|
|
|
throw new InvalidDefinitionException('Body is passed but there is no request body definition'); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
foreach ($this->structure as $parameter) { |
46
|
|
|
if ($parameter['in'] == "body") { |
47
|
|
|
if (isset($parameter['required']) && $parameter['required'] === true && empty($body)) { |
48
|
|
|
throw new RequiredArgumentNotFound('The body is required but it is empty'); |
49
|
|
|
} |
50
|
|
|
return $this->matchSchema($this->name, $parameter['schema'], $body); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!empty($body)) { |
55
|
|
|
throw new InvalidDefinitionException('Body is passed but there is no request body definition'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
|
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|