MessageTrait::withBody()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Aidphp\Http;
6
7
use Psr\Http\Message\StreamInterface;
8
use InvalidArgumentException;
9
10
trait MessageTrait
11
{
12
    private $headers = [];
13
    private $headerNames = [];
14
    private $protocol = '1.1';
15
    private $stream;
16
17 5
    public function getProtocolVersion(): string
18
    {
19 5
        return $this->protocol;
20
    }
21
22 6
    public function withProtocolVersion($version): self
23
    {
24 6
        $version = $this->filterProtocolVersion($version);
25 2
        if ($this->protocol === $version)
26
        {
27 1
            return $this;
28
        }
29
30 1
        $new = clone $this;
31 1
        $new->protocol = $version;
32 1
        return $new;
33
    }
34
35 8
    public function getHeaders(): array
36
    {
37 8
        return $this->headers;
38
    }
39
40 57
    public function hasHeader($header): bool
41
    {
42 57
        return isset($this->headerNames[strtolower($header)]);
43
    }
44
45 11
    public function getHeader($header): array
46
    {
47 11
        $header = strtolower($header);
48 11
        return isset($this->headerNames[$header]) ? $this->headers[$this->headerNames[$header]] : [];
49
    }
50
51 9
    public function getHeaderLine($header): string
52
    {
53 9
        return implode(',', $this->getHeader($header));
54
    }
55
56 9
    public function withHeader($header, $value): self
57
    {
58 9
        if (! is_array($value))
59
        {
60 4
            $value = [$value];
61
        }
62
63 9
        $key = strtolower($header);
64 9
        $new = clone $this;
65
66 9
        if (isset($new->headerNames[$key]))
67
        {
68 1
            unset($new->headers[$new->headerNames[$key]]);
69
        }
70
71 9
        $new->headerNames[$key] = $header;
72 9
        $new->headers[$header]  = $value;
73
74 9
        return $new;
75
    }
76
77 2
    public function withAddedHeader($header, $value): self
78
    {
79 2
        if (! is_array($value))
80
        {
81 2
            $value = [$value];
82
        }
83
84 2
        $key = strtolower($header);
85 2
        $new = clone $this;
86
87 2
        if (isset($new->headerNames[$key]))
88
        {
89 2
            $header = $this->headerNames[$key];
90 2
            $new->headers[$header] = array_merge($this->headers[$header], $value);
91
        }
92
        else
93
        {
94 1
            $new->headerNames[$key] = $header;
95 1
            $new->headers[$header] = $value;
96
        }
97
98 2
        return $new;
99
    }
100
101 1
    public function withoutHeader($header): self
102
    {
103 1
        $key = strtolower($header);
104
105 1
        if (! isset($this->headerNames[$key]))
106
        {
107 1
            return $this;
108
        }
109
110 1
        $header = $this->headerNames[$key];
111
112 1
        $new = clone $this;
113 1
        unset($new->headers[$header], $new->headerNames[$key]);
114
115 1
        return $new;
116
    }
117
118 7
    public function getBody(): StreamInterface
119
    {
120 7
        return $this->stream;
121
    }
122
123 2
    public function withBody(StreamInterface $body): self
124
    {
125 2
        if ($body === $this->stream)
126
        {
127 1
            return $this;
128
        }
129
130 2
        $new = clone $this;
131 2
        $new->stream = $body;
132 2
        return $new;
133
    }
134
135 65
    private function setHeaders(array $headers): void
136
    {
137 65
        $this->headerNames =
138 65
        $this->headers     = [];
139
140 65
        foreach ($headers as $header => $value)
141
        {
142 4
            if (! is_array($value))
143
            {
144 2
                $value = [$value];
145
            }
146
147 4
            $key = strtolower($header);
148
149 4
            if (isset($this->headerNames[$key]))
150
            {
151 2
                $header = $this->headerNames[$key];
152 2
                $this->headers[$header] = array_merge($this->headers[$header], $value);
153
            }
154
            else
155
            {
156 4
                $this->headerNames[$key] = $header;
157 4
                $this->headers[$header]  = $value;
158
            }
159
        }
160 65
    }
161
162 71
    private function filterProtocolVersion(string $version): string
163
    {
164 71
        if (! preg_match('#^[1-2]\.[0-1]$#', $version))
165
        {
166 4
            throw new InvalidArgumentException('Invalid HTTP version protocol "' . $version . '" provided');
167
        }
168
169 67
        return $version;
170
    }
171
}