BodyParam::getValue()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 3
nc 2
nop 4
1
<?php
2
3
namespace Jasny\Controller\Parameter;
4
5
use Jasny\Controller\ParameterException;
6
use Psr\Http\Message\ServerRequestInterface;
7
8
#[\Attribute]
9
class BodyParam extends SingleParameter
10
{
11
    /**
12
     * Get request body parameter.
13
     *
14
     * Optionally apply filtering to the value.
15
     * @link http://php.net/manual/en/filter.filters.php
16
     */
17
    public function getValue(
18
        ServerRequestInterface $request,
19
        string $name,
20
        ?string $type,
21
        bool $required = false
22
    ): mixed {
23
        $key = $this->key ?? $name;
24
        $params = $request->getParsedBody();
25
26
        if ($required && !isset($params[$key])) {
27
            throw new ParameterException("Missing required body parameter '$key'");
28
        }
29
30
        return $this->filter($params[$key] ?? null, $type);
31
    }
32
}
33