Issues (16)

src/Message.php (3 issues)

1
<?php
2
3
/**
4
 * Platine HTTP
5
 *
6
 * Platine HTTP Message is the implementation of PSR 7
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine HTTP
11
 * Copyright (c) 2019 Dion Chaika
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file Message.php
34
 *
35
 *  The default or base HTTP Message
36
 *
37
 *  @package    Platine\Http
38
 *  @author Platine Developers Team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   https://www.platine-php.com
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Http;
49
50
/**
51
 * @class Message
52
 * @package Platine\Http
53
 */
54
abstract class Message implements MessageInterface
55
{
56
    /**
57
     * The message protocol version.
58
     * @var string
59
     */
60
    protected string $protocolVersion = '1.1';
61
62
    /**
63
     * The array of message headers
64
     * @var array<string, array<string>>
65
     */
66
    protected array $headers = [];
67
68
    /**
69
     * The message body.
70
     * @var StreamInterface|null
71
     */
72
    protected ?StreamInterface $body = null;
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getProtocolVersion(): string
78
    {
79
        return $this->protocolVersion;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     * @return $this
85
     */
86
    public function withProtocolVersion(string $version): self
87
    {
88
        $that = clone $this;
89
        $that->protocolVersion = $version;
90
91
        return $that;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getHeaders(): array
98
    {
99
        $headers = [];
100
        foreach ($this->headers as $name => $values) {
101
            $name = implode('-', array_map('ucfirst', explode('-', strtolower($name))));
102
            $headers[$name] = $values;
103
        }
104
105
        return $headers;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function hasHeader(string $name): bool
112
    {
113
        $lowerName = strtolower($name);
114
115
        return isset($this->headers[$lowerName]);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getHeader(string $name): array
122
    {
123
        $lowerName = strtolower($name);
124
125
        return $this->headers[$lowerName] ?? [];
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function getHeaderLine(string $name): string
132
    {
133
        $lowerName = strtolower($name);
134
        return isset($this->headers[$lowerName])
135
                ? implode(', ', $this->headers[$lowerName])
136
                : '';
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     * @return $this
142
     */
143
    public function withHeader(string $name, string|array $value): self
144
    {
145
        $that = clone $this;
146
        $lowerName = strtolower($name);
147
        $that->headers[$lowerName] = is_array($value) ? $value : [$value];
0 ignored issues
show
The condition is_array($value) is always true.
Loading history...
148
149
        return $that;
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function withAddedHeader(string $name, string|array $value): self
156
    {
157
        $that = clone $this;
158
        $lowerName = strtolower($name);
159
        if (is_array($value)) {
0 ignored issues
show
The condition is_array($value) is always true.
Loading history...
160
            $value = array_values($value);
161
            $that->headers[$lowerName] = isset($that->headers[$lowerName])
162
                                        ? array_merge(
163
                                            $that->headers[$lowerName],
164
                                            $value
165
                                        )
166
                                        : $value;
167
        } else {
168
            $that->headers[$lowerName][] = $value;
169
        }
170
        return $that;
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function withoutHeader(string $name): self
177
    {
178
        $that = clone $this;
179
        $lowerName = strtolower($name);
180
        unset($that->headers[$lowerName]);
181
182
        return $that;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function getBody(): StreamInterface
189
    {
190
        if ($this->body === null) {
191
            $this->body = new Stream();
192
        }
193
        return $this->body;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->body could return the type null which is incompatible with the type-hinted return Platine\Http\StreamInterface. Consider adding an additional type-check to rule them out.
Loading history...
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function withBody(StreamInterface $body): self
200
    {
201
        $that = clone $this;
202
        $that->body = $body;
203
        $size = $body->getSize();
204
205
        if ($size === null) {
206
            $that = $that->withHeader('Transfer-Encoding', 'chunked')
207
                         ->withoutHeader('Content-Length');
208
        } else {
209
            $that = $that->withHeader('Content-Length', (string) $size)
210
                         ->withoutHeader('Transfer-Encoding');
211
        }
212
213
        return $that;
214
    }
215
}
216