BodyParam   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 7
c 1
b 0
f 0
dl 0
loc 23
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 14 3
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