Passed
Push — master ( 9013bb...125cf1 )
by Kirill
03:56
created

HeadersBag   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
c 1
b 0
f 0
dl 0
loc 63
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 3 1
A get() 0 9 3
A normalize() 0 6 1
A fetch() 0 14 3
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Http\Request;
13
14
/**
15
 * Provides access to headers property of server request, will normalize every requested name for
16
 * use convenience.
17
 */
18
final class HeadersBag extends InputBag
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function has(string $name): bool
24
    {
25
        return parent::has($this->normalize($name));
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     *
31
     *
32
     * @param bool|string $implode Implode header lines, false to return header as array.
33
34
     * @return string|array
35
     */
36
    public function get(string $name, $default = null, $implode = ',')
37
    {
38
        $value = parent::get($this->normalize($name), $default);
39
40
        if (!empty($implode) && is_array($value)) {
41
            return implode($implode, $value);
0 ignored issues
show
Bug introduced by
It seems like $implode can also be of type boolean; however, parameter $glue of implode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
            return implode(/** @scrutinizer ignore-type */ $implode, $value);
Loading history...
42
        }
43
44
        return $value;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     *
50
     * @param null|string $implode Implode header lines, null to return header as array.
51
     */
52
    public function fetch(array $keys, bool $fill = false, $filler = null, ?string $implode = ',')
53
    {
54
        $keys = array_map([$this, 'normalize'], $keys);
55
56
        $values = parent::fetch($keys, $fill, $filler);
57
58
        if (!empty($implode)) {
59
            foreach ($values as &$value) {
60
                $value = implode($implode, $value);
61
                unset($value);
62
            }
63
        }
64
65
        return $values;
66
    }
67
68
    /**
69
     * Normalize header name.
70
     *
71
     * @param string $header
72
73
     * @return string
74
     */
75
    protected function normalize(string $header): string
76
    {
77
        return str_replace(
78
            ' ',
79
            '-',
80
            ucwords(str_replace('-', ' ', $header))
81
        );
82
    }
83
}
84