OpenApiRequestBody::match()   B
last analyzed

Complexity

Conditions 8
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 19
c 0
b 0
f 0
rs 8.4444
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\InvalidDefinitionException;
7
use ByJG\ApiTools\Exception\RequiredArgumentNotFound;
8
9
class OpenApiRequestBody extends Body
10
{
11
    /**
12
     * @inheritDoc
13
     */
14
    public function match(mixed $body): bool
15
    {
16
        if (isset($this->structure['content']) || isset($this->structure['$ref'])) {
17
            if (isset($this->structure['required']) && $this->structure['required'] === true && empty($body)) {
18
                throw new RequiredArgumentNotFound('The body is required but it is empty');
19
            }
20
21
            if (isset($this->structure['$ref'])) {
22
                return $this->matchSchema($this->name, $this->structure, $body) ?? false;
23
            }
24
25
            return $this->matchSchema($this->name, $this->structure['content'][key($this->structure['content'])]['schema'], $body) ?? false;
26
        }
27
28
        if (!empty($body)) {
29
            throw new InvalidDefinitionException('Body is passed but there is no request body definition');
30
        }
31
32
        return false;
33
    }
34
}
35