3
declare(strict_types=1);
6
* This file is part of CodeIgniter 4 framework.
8
* (c) CodeIgniter Foundation <admin@codeigniter.com>
10
* For the full copyright and license information, please view
11
* the LICENSE file that was distributed with this source code.
14
namespace CodeIgniter\HTTP;
21
* Represents a single HTTP header.
23
* @see \CodeIgniter\HTTP\HeaderTest
25
class Header implements Stringable
28
* The name of the header.
35
* The value of the header. May have more than one
36
* value. If so, will be an array of strings.
46
* @var array<int|string, array<string, string>|string>|string
51
* Header constructor. name is mandatory, if a value is provided, it will be set.
53
* @param array<int|string, array<string, string>|string>|string|null $value
55
public function __construct(string $name, $value = null)
58
$this->setValue($value);
62
* Returns the name of the header, in the same case it was set.
64
public function getName(): string
70
* Gets the raw value of the header. This may return either a string
71
* or an array, depending on whether the header has multiple values or not.
73
* @return array<int|string, array<string, string>|string>|string
75
public function getValue()
81
* Sets the name of the header, overwriting any previous value.
85
public function setName(string $name)
93
* Sets the value of the header, overwriting any previous value(s).
95
* @param array<int|string, array<string, string>|string>|string|null $value
99
public function setValue($value = null)
101
$this->value = is_array($value) ? $value : (string) $value;
107
* Appends a value to the list of values for this header. If the
108
* header is a single value string, it will be converted to an array.
110
* @param array<string, string>|string|null $value
114
public function appendValue($value = null)
116
if ($value === null) {
120
if (! is_array($this->value)) {
121
$this->value = [$this->value];
124
if (! in_array($value, $this->value, true)) {
125
$this->value[] = is_array($value) ? $value : (string) $value;
132
* Prepends a value to the list of values for this header. If the
133
* header is a single value string, it will be converted to an array.
135
* @param array<string, string>|string|null $value
139
public function prependValue($value = null)
141
if ($value === null) {
145
if (! is_array($this->value)) {
146
$this->value = [$this->value];
149
array_unshift($this->value, $value);
155
* Retrieves a comma-separated string of the values for a single header.
157
* NOTE: Not all header values may be appropriately represented using
158
* comma concatenation. For such headers, use getHeader() instead
159
* and supply your own delimiter when concatenating.
161
* @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
163
public function getValueLine(): string
165
if (is_string($this->value)) {
168
if (! is_array($this->value)) {
174
foreach ($this->value as $key => $value) {
175
if (is_string($key) && ! is_array($value)) {
176
$options[] = $key . '=' . $value;
177
} elseif (is_array($value)) {
179
$options[] = $key . '=' . $value[$key];
180
} elseif (is_numeric($key)) {
185
return implode(', ', $options);
189
* Returns a representation of the entire header string, including
190
* the header name and all values converted to the proper format.
192
public function __toString(): string
194
return $this->name . ': ' . $this->getValueLine();