|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Veto. |
|
4
|
|
|
* PHP Microframework. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Damien Walsh <[email protected]> |
|
7
|
|
|
* @copyright Damien Walsh 2013-2014 |
|
8
|
|
|
* @version 0.1 |
|
9
|
|
|
* @package veto |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Veto\Http; |
|
12
|
|
|
|
|
13
|
|
|
use Veto\Collection\Bag; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* HeaderBag - a Bag of HTTP headers |
|
17
|
|
|
*/ |
|
18
|
|
|
class HeaderBag extends Bag |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Special HTTP headers that do not have the "HTTP_" prefix, for some reason. |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected static $special = array( |
|
26
|
|
|
'CONTENT_TYPE', |
|
27
|
|
|
'CONTENT_LENGTH', |
|
28
|
|
|
'PHP_AUTH_USER', |
|
29
|
|
|
'PHP_AUTH_PW', |
|
30
|
|
|
'PHP_AUTH_DIGEST', |
|
31
|
|
|
'AUTH_TYPE', |
|
32
|
|
|
); |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create a new HeaderBag, derived from the provided environment bag. |
|
36
|
|
|
* |
|
37
|
|
|
* @param Bag $environment The environment variables |
|
38
|
|
|
* @return self |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function createFromEnvironment(Bag $environment) |
|
41
|
|
|
{ |
|
42
|
|
|
$headers = new static(); |
|
43
|
|
|
|
|
44
|
|
|
foreach ($environment as $key => $value) { |
|
45
|
|
|
$key = strtoupper($key); |
|
46
|
|
|
if (strpos($key, 'HTTP_') === 0 || in_array($key, static::$special)) { |
|
47
|
|
|
if ($key === 'HTTP_CONTENT_TYPE' || $key === 'HTTP_CONTENT_LENGTH') { |
|
48
|
|
|
continue; |
|
49
|
|
|
} |
|
50
|
|
|
$headers->add($key, $value); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $headers; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Add a header to the bag |
|
59
|
|
|
* |
|
60
|
|
|
* @param mixed $key |
|
61
|
|
|
* @param mixed $value |
|
62
|
|
|
* @return $this |
|
63
|
|
|
*/ |
|
64
|
|
|
public function add($key, $value) |
|
65
|
|
|
{ |
|
66
|
|
|
$header = $this->get($key, array()); |
|
67
|
|
|
|
|
68
|
|
|
if (is_array($value)) { |
|
69
|
|
|
$header = array_merge($header, $value); |
|
70
|
|
|
} else { |
|
71
|
|
|
$header[] = $value; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
parent::add($key, $header); |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get a header from the bag by key |
|
81
|
|
|
* |
|
82
|
|
|
* @param mixed $key |
|
83
|
|
|
* @param array $default The default value if no header matches |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
|
|
public function get($key, $default = array()) |
|
87
|
|
|
{ |
|
88
|
|
|
return parent::get($this->normalizeKey($key), $default); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Check if the bag contains a given key |
|
93
|
|
|
* |
|
94
|
|
|
* @param mixed $key |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
|
|
public function has($key) |
|
98
|
|
|
{ |
|
99
|
|
|
return array_key_exists($key, $this->items); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* If the bag contains the given key, return the value, then remove it from the bag. |
|
104
|
|
|
* |
|
105
|
|
|
* @param $key |
|
106
|
|
|
* @return mixed|null |
|
107
|
|
|
*/ |
|
108
|
|
|
public function remove($key) |
|
109
|
|
|
{ |
|
110
|
|
|
return parent::remove($this->normalizeKey($key)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get the underlying array of the contents of the bag. |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function all() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->items; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Normalize header name, converting it to lower-case |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $key The case-insensitive header name |
|
127
|
|
|
* @return string Normalized header name |
|
128
|
|
|
*/ |
|
129
|
|
|
public function normalizeKey($key) |
|
130
|
|
|
{ |
|
131
|
|
|
$key = strtolower($key); |
|
132
|
|
|
$key = str_replace(array('-', '-'), ' ', $key); |
|
133
|
|
|
$key = ucwords($key); |
|
134
|
|
|
$key = str_replace(' ', '-', $key); |
|
135
|
|
|
|
|
136
|
|
|
return $key; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|