1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is a part of Woketo package. |
4
|
|
|
* |
5
|
|
|
* (c) Nekland <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full license, take a look to the LICENSE file |
8
|
|
|
* on the root directory of this project |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Nekland\Woketo\Http; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class HttpHeadersBag implements \ArrayAccess, \Iterator |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string[] |
18
|
|
|
*/ |
19
|
|
|
private $headers; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Is used by the iterator feature to store a possible "false" value when the loop over the array is finished. |
23
|
|
|
* |
24
|
|
|
* @var mixed |
25
|
|
|
*/ |
26
|
|
|
private $next; |
27
|
|
|
|
28
|
|
|
public function __construct(array $headers = null) |
29
|
|
|
{ |
30
|
|
|
$this->headers = $headers ?: []; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $name |
35
|
|
|
* @param mixed $value |
36
|
|
|
* @return self |
37
|
|
|
*/ |
38
|
|
|
public function set(string $name, $value) |
39
|
|
|
{ |
40
|
|
|
$this->headers[$name] = $value; |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $name |
47
|
|
|
* @param mixed $default |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
public function get(string $name, $default = null) |
51
|
|
|
{ |
52
|
|
|
$headersLower = \array_change_key_case($this->headers); |
53
|
|
|
$name = \strtolower($name); |
54
|
|
|
|
55
|
|
|
return isset($headersLower[$name]) ? $headersLower[$name] : $default; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $name |
60
|
|
|
* @param mixed $value |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
|
|
public function add(string $name, $value) |
64
|
|
|
{ |
65
|
|
|
if (!empty($this->headers[$name])) { |
66
|
|
|
if (!\is_array($this->headers[$name])){ |
67
|
|
|
$this->headers[$name] = [$this->headers[$name]]; |
68
|
|
|
} |
69
|
|
|
$this->headers[$name][] = $value; |
70
|
|
|
} else { |
71
|
|
|
$this->headers[$name] = $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $name |
79
|
|
|
*/ |
80
|
|
|
public function remove(string $name) |
81
|
|
|
{ |
82
|
|
|
unset($this->headers[$name]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function offsetExists($offset) |
89
|
|
|
{ |
90
|
|
|
return isset($this->headers[$offset]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function offsetGet($offset) |
97
|
|
|
{ |
98
|
|
|
return $this->get($offset); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function offsetSet($offset, $value) |
105
|
|
|
{ |
106
|
|
|
throw new \Exception('You must use either add or set method to update the header bag.'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function offsetUnset($offset) |
113
|
|
|
{ |
114
|
|
|
$this->remove($offset); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function current() |
118
|
|
|
{ |
119
|
|
|
return $this->next ?: \current($this->headers); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function next() |
123
|
|
|
{ |
124
|
|
|
$this->next = \next($this->headers); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function key() |
128
|
|
|
{ |
129
|
|
|
return \key($this->headers); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function valid() |
133
|
|
|
{ |
134
|
|
|
return $this->next !== false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function rewind() |
138
|
|
|
{ |
139
|
|
|
$this->current = 0; |
|
|
|
|
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: