1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Raptor\Request\Components; |
4
|
|
|
|
5
|
|
|
class Header |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* header-fields (see https://tools.ietf.org/html/rfc7230#section-3.2) |
9
|
|
|
* |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $fields; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Cookies ($_COOKIE). |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $cookie; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Authorization header-field ($_SERVER['AUTHORIZATION']). |
23
|
|
|
* |
24
|
|
|
* @var \Raptor\Request\Components\Authorization |
25
|
|
|
*/ |
26
|
|
|
protected $authorization; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class constructor |
30
|
|
|
*/ |
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
$this->fields = []; |
34
|
|
|
// CONTENT_* are not prefixed with HTTP_ |
35
|
|
|
$content = ['CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true]; |
36
|
|
|
foreach ($_SERVER as $key => $value) { |
37
|
|
|
if (0 === strpos($key, 'HTTP_') || isset($content[$key])) { |
38
|
|
|
$this->fields[$key] = $value; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
$this->authorization = new Authorization; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get all header-fields. |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function all() |
50
|
|
|
{ |
51
|
|
|
return $this->fields; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get specific header-field. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function get($key, $default = null) |
60
|
|
|
{ |
61
|
|
|
return isset($this->fields[$key]) ? $this->fields[$key] : $default; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get cookies. |
66
|
|
|
* |
67
|
|
|
* @param string $key (optional) |
68
|
|
|
* @param mixed $default (optional) |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
|
|
public function cookie($key = null, $default = null) |
72
|
|
|
{ |
73
|
|
|
if ($this->cookie === null) return $this->cookie = $_COOKIE; |
74
|
|
|
if ($key === null) return $this->cookie; |
75
|
|
|
return isset($this->cookie[$key]) ? $this->cookie[$key] : $default; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get authorization header-field. |
80
|
|
|
* |
81
|
|
|
* @return \Raptor\Request\Components\Authorization |
82
|
|
|
*/ |
83
|
|
|
public function authorization() |
84
|
|
|
{ |
85
|
|
|
return $this->authorization; |
86
|
|
|
} |
87
|
|
|
} |