Header   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 1
b 0
f 0
dl 0
loc 26
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 14 3
A convertNameToHeader() 0 4 1
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 Header extends SingleParameter
10
{
11
    /**
12
     * Get request header.
13
     */
14
    public function getValue(
15
        ServerRequestInterface $request,
16
        string $name,
17
        ?string $type,
18
        bool $required = false
19
    ): mixed {
20
        $key = $this->key ?? $this->convertNameToHeader($name);
21
        $value = $request->getHeaderLine($key);
22
23
        if ($required && $value === '') {
24
            throw new ParameterException("Missing required header '$key'");
25
        }
26
27
        return $this->filter($value, $type);
28
    }
29
30
    protected function convertNameToHeader(string $name): string
31
    {
32
        $sentence = preg_replace('/([a-z0-9])([A-Z])|(\w)_(\w)/', '$1$3-$2$4', $name);
33
        return str_replace(' ', '', ucwords($sentence, '-'));
34
    }
35
}
36