1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\ApiTools\Swagger; |
4
|
|
|
|
5
|
|
|
use ByJG\ApiTools\Base\Body; |
6
|
|
|
use ByJG\ApiTools\Exception\DefinitionNotFoundException; |
7
|
|
|
use ByJG\ApiTools\Exception\GenericSwaggerException; |
8
|
|
|
use ByJG\ApiTools\Exception\InvalidDefinitionException; |
9
|
|
|
use ByJG\ApiTools\Exception\InvalidRequestException; |
10
|
|
|
use ByJG\ApiTools\Exception\NotMatchedException; |
11
|
|
|
use ByJG\ApiTools\Exception\RequiredArgumentNotFound; |
12
|
|
|
|
13
|
|
|
class SwaggerRequestBody extends Body |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param $body |
17
|
|
|
* @return bool |
18
|
|
|
* @throws GenericSwaggerException |
19
|
|
|
* @throws InvalidDefinitionException |
20
|
|
|
* @throws InvalidRequestException |
21
|
|
|
* @throws NotMatchedException |
22
|
|
|
* @throws RequiredArgumentNotFound |
23
|
|
|
* @throws DefinitionNotFoundException |
24
|
|
|
*/ |
25
|
|
|
public function match($body) |
26
|
|
|
{ |
27
|
|
|
$hasFormData = false; |
28
|
|
|
foreach ($this->structure as $parameter) { |
29
|
|
|
if ($parameter['in'] === "body") { |
30
|
|
|
if (isset($parameter['required']) && $parameter['required'] === true && empty($body)) { |
31
|
|
|
throw new RequiredArgumentNotFound('The body is required but it is empty'); |
32
|
|
|
} |
33
|
|
|
return $this->matchSchema($this->name, $parameter['schema'], $body); |
34
|
|
|
} |
35
|
|
|
if ($parameter['in'] === "formData") { |
36
|
|
|
$hasFormData = true; |
37
|
|
|
if (isset($parameter['required']) && $parameter['required'] === true && !isset($body[$parameter['name']])) { |
38
|
|
|
throw new RequiredArgumentNotFound("The formData parameter '${parameter['name']}' is required but it isn't found. "); |
39
|
|
|
} |
40
|
|
|
if (!$this->matchTypes($parameter['name'], $parameter, (isset($body[$parameter['name']]) ? $body[$parameter['name']] : null))) { |
41
|
|
|
throw new NotMatchedException("The formData parameter '${parameter['name']}' not match with the specification"); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (!empty($body) && !$hasFormData) { |
47
|
|
|
throw new InvalidDefinitionException('Body is passed but there is no request body definition'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|