1
|
|
|
<?php |
2
|
|
|
namespace nebula\component\request\header; |
3
|
|
|
|
4
|
|
|
use nebula\component\request\header\ContentTypeItem; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* HTTP请求头 |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
class HeaderItem |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* 头部名 |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $name; |
18
|
|
|
/** |
19
|
|
|
* 头部值 |
20
|
|
|
* |
21
|
|
|
* @var mixed |
22
|
|
|
*/ |
23
|
|
|
protected $value; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* 获取支持解析的头部 |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected static $headers =[ |
31
|
|
|
'content-type' => ContentTypeItem::class, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
public function __construct(string $name, string $value) |
35
|
|
|
{ |
36
|
|
|
$this->name = $name; |
37
|
|
|
$this->value = $value; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* 从 $_SERVER 变量获取 |
42
|
|
|
* |
43
|
|
|
* @param array $server |
44
|
|
|
* @return HeaderItem[] 头部数组 |
45
|
|
|
*/ |
46
|
|
|
public static function buildFromServer(array $server):array |
47
|
|
|
{ |
48
|
|
|
$headers = []; |
49
|
|
|
foreach ($server as $key => $value) { |
50
|
|
|
if (strpos($key, 'HTTP_') === 0) { |
51
|
|
|
$name = substr($key, strlen('HTTP_')); |
52
|
|
|
$headers[$name] = $value; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
return static::build($headers); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* 获取头部信息 |
60
|
|
|
* |
61
|
|
|
* @param array $headers |
62
|
|
|
* @return HeaderItem[] |
63
|
|
|
*/ |
64
|
|
|
public static function build(array $headers):array |
65
|
|
|
{ |
66
|
|
|
$build = []; |
67
|
|
|
foreach ($headers as $name => $value) { |
68
|
|
|
$name = \strtolower(\str_replace('_', '-', $name)); |
69
|
|
|
if ($value instanceof HeaderItem) { |
70
|
|
|
$header = $value; |
71
|
|
|
} elseif (array_key_exists($name, static::$headers)) { |
72
|
|
|
$class =static::$headers[$name]; |
73
|
|
|
$header = new $class($name, $value); |
74
|
|
|
} else { |
75
|
|
|
$header = new HeaderItem($name, $value); |
76
|
|
|
} |
77
|
|
|
$build[$header->getName()] = $header; |
78
|
|
|
} |
79
|
|
|
return $build; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get 头部名 |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getName() |
88
|
|
|
{ |
89
|
|
|
return $this->name; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set 头部名 |
94
|
|
|
* |
95
|
|
|
* @param string $name 头部名 |
96
|
|
|
* |
97
|
|
|
* @return self |
98
|
|
|
*/ |
99
|
|
|
public function setName(string $name) |
100
|
|
|
{ |
101
|
|
|
$this->name = $name; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get 头部值 |
108
|
|
|
* |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
public function getValue() |
112
|
|
|
{ |
113
|
|
|
return $this->value; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set 头部值 |
118
|
|
|
* |
119
|
|
|
* @param mixed $value 头部值 |
120
|
|
|
* |
121
|
|
|
* @return self |
122
|
|
|
*/ |
123
|
|
|
public function setValue($value) |
124
|
|
|
{ |
125
|
|
|
$this->value = $value; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|