Passed
Push — main ( 8870e8...6b412e )
by Stéphane
04:32
created

Headers::set()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 17
ccs 9
cts 10
cp 0.9
rs 9.8666
cc 4
nc 4
nop 2
crap 4.016
1
<?php
2
3
namespace CHStudio\Raven\Http\Factory;
4
5
use ArrayIterator;
6
use InvalidArgumentException;
7
use IteratorAggregate;
8
use Stringable;
9
use Traversable;
10
11
/**
12
 * @implements IteratorAggregate<string, array<string>>
13
 */
14
class Headers implements IteratorAggregate, Stringable
15
{
16
    /**
17
     * @var array<string, array<string>>
18
     */
19
    private array $headers = [];
20
21 10
    public function __construct(mixed $headers = [])
22
    {
23 10
        if (!\is_array($headers)) {
24 1
            throw new InvalidArgumentException('headers collection must be an array.');
25
        }
26
27 9
        foreach ($headers as $name => $values) {
28 6
            $this->set($name, $values);
29
        }
30
    }
31
32 7
    public function getIterator(): Traversable
33
    {
34 7
        return new ArrayIterator($this->headers);
35
    }
36
37 1
    public function __toString(): string
38
    {
39 1
        $asString = '';
40 1
        foreach ($this->headers as $name => $values) {
41 1
            $name = ucwords($name, '-');
42 1
            foreach ($values as $value) {
43 1
                $asString .= sprintf('%s: %s', $name, $value).PHP_EOL;
44
            }
45
        }
46
47 1
        return $asString;
48
    }
49
50 7
    private function normalizeName(string $name): string
51
    {
52 7
        return strtolower($name);
53
    }
54
55 1
    public function has(string $offset): bool
56
    {
57 1
        return isset($this->headers[$this->normalizeName($offset)]);
58
    }
59
60 5
    public function first(string $offset): ?string
61
    {
62 5
        $values = $this->get($offset);
63 5
        return \count($values) > 0 ? current($values) : null;
64
    }
65
66
    /**
67
     * @return array<string|null>
68
     */
69 5
    public function get(string $offset): array
70
    {
71 5
        return $this->headers[$this->normalizeName($offset)] ?? [];
72
    }
73
74
    /**
75
     * @param array<string>|string $values
76
     */
77 6
    public function set(string $offset, array|string $values): void
78
    {
79 6
        $normalized = $this->normalizeName($offset);
80
81 6
        if (\is_array($values)) {
0 ignored issues
show
introduced by
The condition is_array($values) is always true.
Loading history...
82 1
            $values = array_values($values);
83
84 1
            if (!isset($this->headers[$normalized])) {
85 1
                $this->headers[$normalized] = $values;
86
            } else {
87
                $this->headers[$normalized] = array_merge($this->headers[$normalized], $values);
88
            }
89
        } else {
90 6
            if (!isset($this->headers[$normalized])) {
91 6
                $this->headers[$normalized] = [$values];
92
            } else {
93 1
                $this->headers[$normalized][] = $values;
94
            }
95
        }
96
    }
97
}
98