Completed
Push — master ( 204760...d7809c )
by Sebastian
02:32
created

Message::withHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Linna Psr7.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Psr7;
13
14
use Linna\TypedArray;
15
use Psr\Http\Message\MessageInterface;
16
use Psr\Http\Message\StreamInterface;
17
18
/**
19
 * HTTP Psr7 Message implementation
20
 */
21
abstract class Message implements MessageInterface
22
{
23
    /**
24
    * @var string Protocol version.
25
    */
26
    protected $protocolVersion = '1.1';
27
28
    /**
29
    * @var array Allowed protocol versions.
30
    */
31
    protected static $allowedProtocolVersions = [
32
        '1.0' => true,
33
        '1.1' => true,
34
        '2.0' => true,
35
    ];
36
37
    /**
38
     * @var array Message headers .
39
     */
40
    protected $headers = [];
41
    
42
    /**
43
     * @var StreamInterface Body of the message.
44
     */
45
    protected $body;
46
    
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getProtocolVersion() : string
51
    {
52
        return $this->protocolVersion;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function withProtocolVersion(string $version) : MessageInterface
59
    {
60
        if (!isset(self::$validProtocolVersions[$version])) {
0 ignored issues
show
Bug Best Practice introduced by
The property validProtocolVersions does not exist on Linna\Psr7\Message. Did you maybe forget to declare it?
Loading history...
61
            throw new InvalidArgumentException(__CLASS__.': Invalid HTTP protocol version. Must be 1.0, 1.1 or 2.0');
0 ignored issues
show
Bug introduced by
The type Linna\Psr7\InvalidArgumentException was not found. Did you mean InvalidArgumentException? If so, make sure to prefix the type with \.
Loading history...
62
        }
63
64
        $new = clone $this;
65
        $new->protocolVersion = $version;
66
67
        return $new;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getHeaders() : array
74
    {
75
        return $this->headers;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function hasHeader(string $name) : bool
82
    {
83
        $this->normalize($name);
84
        
85
        return isset($this->headers[$name]);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getHeader(string $name) : array
92
    {
93
        $this->normalize($name);
94
                
95
        return isset($this->headers[$name]) ?  $this->headers[$name] : [];
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function getHeaderLine(string $name) : string
102
    {
103
        $this->normalize($name);
104
        
105
        return isset($this->headers[$name]) ?  implode(', ', $this->headers[$name]) : '';
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function withHeader(string $name, array $value) : MessageInterface
112
    {
113
        $this->normalize($name);
114
        
115
        $new = clone $this;
116
        
117
        //use typed array for assicure that array contains only strings
118
        $new->headers[$name] = (new TypedArray('string', $value))->getArrayCopy();
119
        
120
        return $new;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function withAddedHeader(string $name, array $value) : MessageInterface
127
    {
128
        $this->normalize($name);
129
        
130
        //use typed array for assicure that array contains only strings
131
        $headerValue = (new TypedArray('string', $value))->getArrayCopy();
132
        
133
        $new = clone $this;
134
        
135
        //check if header exist
136
        if (!isset($this->headers[$name])) {
137
            $new->headers[$name] = $headerValue;
138
        
139
            return $new;
140
        }
141
        
142
        //at this point header exists
143
        //remain only to append new value to existing header
144
        $new->headers[$name] = array_merge($this->headers[$name], $headerValue);
145
        
146
        return $new;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function withoutHeader(string $name) : MessageInterface
153
    {
154
        $this->normalize($name);
155
        
156
        $new = clone $this;
157
        unset($new->headers[$name]);
158
        
159
        return $new;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function getBody() : StreamInterface
166
    {
167
        return $this->body;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function withBody(StreamInterface $body) : MessageInterface
174
    {
175
        $new = clone $this;
176
        $new->body = $body;
177
        
178
        return $new;
179
    }
180
181
    /**
182
     * Normalize header name for case-unsensitive search.
183
     *
184
     * @param string $headerName
185
     */
186
    private function normalize(string &$headerName)
187
    {
188
        $headerName = ucwords(strtolower($headerName), '-');
189
    }
190
}
191