Completed
Pull Request — master (#33)
by Joao
06:03
created

OpenApiRequestBody::match()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.4444
c 0
b 0
f 0
cc 8
nc 5
nop 1
1
<?php
2
3
namespace ByJG\ApiTools\OpenApi;
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 OpenApiRequestBody 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
        if (isset($this->structure['content']) || isset($this->structure['$ref'])) {
28
            if (isset($this->structure['required']) && $this->structure['required'] === true && empty($body)) {
29
                throw new RequiredArgumentNotFound('The body is required but it is empty');
30
            }
31
32
            if (isset($this->structure['$ref'])) {
33
                return $this->matchSchema($this->name, $this->structure, $body);
34
            }
35
36
            return $this->matchSchema($this->name, $this->structure['content'][key($this->structure['content'])]['schema'], $body);
37
        }
38
39
        if (!empty($body)) {
40
            throw new InvalidDefinitionException('Body is passed but there is no request body definition');
41
        }
42
43
        return false;
44
    }
45
}
46