Test Failed
Pull Request — main (#30)
by Stéphane
11:42
created

Headers::__construct()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 18
ccs 8
cts 8
cp 1
rs 8.8333
cc 7
nc 7
nop 1
crap 7
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
            if (\is_string($values)) {
29
                $this->set($name, $values);
30
            } elseif (\is_array($values)) {
31
                foreach ($values as $value) {
32 7
                    if (!\is_string($value)) {
33
                        throw new InvalidArgumentException('Header values must be string[]|string.');
34 7
                    }
35
                    $this->set($name, $value);
36
                }
37 1
            } else {
38
                throw new InvalidArgumentException('Header values must be string[]|string.');
39 1
            }
40 1
        }
41 1
    }
42 1
43 1
    public function getIterator(): Traversable
44
    {
45
        return new ArrayIterator($this->headers);
46
    }
47 1
48
    public function __toString(): string
49
    {
50 7
        $asString = '';
51
        foreach ($this->headers as $name => $values) {
52 7
            $name = ucwords($name, '-');
53
            foreach ($values as $value) {
54
                $asString .= \sprintf('%s: %s', $name, $value).PHP_EOL;
55 1
            }
56
        }
57 1
58
        return $asString;
59
    }
60 5
61
    private function normalizeName(string $name): string
62 5
    {
63 5
        return strtolower($name);
64
    }
65
66
    public function has(string $offset): bool
67
    {
68
        return isset($this->headers[$this->normalizeName($offset)]);
69 5
    }
70
71 5
    public function first(string $offset): ?string
72
    {
73
        $values = $this->get($offset);
74
        return \count($values) > 0 ? current($values) : null;
75
    }
76
77 6
    /**
78
     * @return array<string|null>
79 6
     */
80
    public function get(string $offset): array
81 6
    {
82 1
        return $this->headers[$this->normalizeName($offset)] ?? [];
83
    }
84 1
85 1
    /**
86
     * @param array<string>|string $values
87
     */
88
    public function set(string $offset, array|string $values): void
89
    {
90 6
        $normalized = $this->normalizeName($offset);
91 6
92
        if (\is_array($values)) {
0 ignored issues
show
introduced by
The condition is_array($values) is always true.
Loading history...
93 1
            $values = array_values($values);
94
95
            if (!isset($this->headers[$normalized])) {
96
                $this->headers[$normalized] = $values;
97
            } else {
98
                $this->headers[$normalized] = array_merge($this->headers[$normalized], $values);
99
            }
100
        } else {
101
            if (!isset($this->headers[$normalized])) {
102
                $this->headers[$normalized] = [$values];
103
            } else {
104
                $this->headers[$normalized][] = $values;
105
            }
106
        }
107
    }
108
}
109