12
12
*
13
13
* Реализует управление заголовками запроса
14
14
*/
15
- class Headers implements HeadersInterface
16
- {
15
+ class Headers implements HeadersInterface {
17
16
18
17
/**
19
18
* @var array Заголовки
20
19
*/
21
20
private array $ headers ;
22
21
23
- public function __construct ()
24
- {
22
+ public function __construct () {
25
23
$ this ->headers = $ this ->getAllHeaders ();
26
24
}
27
25
@@ -30,23 +28,19 @@ public function __construct()
30
28
*
31
29
* @return array
32
30
*/
33
- private function getAllHeaders (): array
34
- {
31
+ private function getAllHeaders (): array {
35
32
if (function_exists ('getallheaders ' )) {
36
33
return getallheaders () !== false ? getallheaders () : [];
37
34
}
38
-
39
- if (!is_array ($ _SERVER ))
40
- {
35
+
36
+ if (!is_array ($ _SERVER )) {
41
37
return [];
42
38
}
43
39
44
40
$ headers = [];
45
41
46
- foreach ($ _SERVER as $ name => $ value )
47
- {
48
- if (strpos ($ name , 'HTTP_ ' ) === 0 )
49
- {
42
+ foreach ($ _SERVER as $ name => $ value ) {
43
+ if (strpos ($ name , 'HTTP_ ' ) === 0 ) {
50
44
$ headers [str_replace (' ' , '- ' , ucwords (strtolower (str_replace ('_ ' , ' ' , substr ($ name , 5 )))))] = $ value ;
51
45
}
52
46
}
@@ -57,12 +51,10 @@ private function getAllHeaders(): array
57
51
/**
58
52
* @inheritDoc
59
53
*/
60
- public function set (array $ params ): void
61
- {
54
+ public function set (array $ params ): void {
62
55
$ this ->getAll ();
63
56
64
- foreach ($ params as $ header => $ value )
65
- {
57
+ foreach ($ params as $ header => $ value ) {
66
58
$ this ->headers [$ header ] = $ value ;
67
59
}
68
60
@@ -72,10 +64,17 @@ public function set(array $params): void
72
64
/**
73
65
* @inheritDoc
74
66
*/
75
- public function add (array $ params ): void
76
- {
77
- foreach ($ params as $ header => $ value )
78
- {
67
+ public function getAll (): array {
68
+ $ this ->headers = !empty ($ this ->headers ) ? $ this ->headers : $ this ->getAllHeaders ();
69
+
70
+ return $ this ->headers ;
71
+ }
72
+
73
+ /**
74
+ * @inheritDoc
75
+ */
76
+ public function add (array $ params ): void {
77
+ foreach ($ params as $ header => $ value ) {
79
78
$ headerExists = array_key_exists ($ header , $ this ->headers );
80
79
$ this ->headers [$ header ] = $ value ;
81
80
@@ -86,8 +85,7 @@ public function add(array $params): void
86
85
/**
87
86
* @inheritDoc
88
87
*/
89
- public function remove (string $ key ): void
90
- {
88
+ public function remove (string $ key ): void {
91
89
$ this ->getAll ();
92
90
93
91
unset($ this ->headers [$ key ]);
@@ -97,8 +95,7 @@ public function remove(string $key): void
97
95
/**
98
96
* @inheritDoc
99
97
*/
100
- public function removeAll (): void
101
- {
98
+ public function removeAll (): void {
102
99
$ this ->headers = [];
103
100
104
101
header_remove ();
@@ -107,20 +104,8 @@ public function removeAll(): void
107
104
/**
108
105
* @inheritDoc
109
106
*/
110
- public function has (string $ key ): bool
111
- {
112
- $ this ->getAll ();
113
-
114
- return array_key_exists ($ key , $ this ->headers );
115
- }
116
-
117
- /**
118
- * @inheritDoc
119
- */
120
- public function get (string $ key ): string
121
- {
122
- if (!$ this ->has ($ key ))
123
- {
107
+ public function get (string $ key ): string {
108
+ if (!$ this ->has ($ key )) {
124
109
throw new InvalidArgumentException ("Заголовок {$ key } отсутствует. " );
125
110
}
126
111
@@ -130,10 +115,9 @@ public function get(string $key): string
130
115
/**
131
116
* @inheritDoc
132
117
*/
133
- public function getAll (): array
134
- {
135
- $ this ->headers = !empty ($ this ->headers ) ? $ this ->headers : $ this ->getAllHeaders ();
118
+ public function has (string $ key ): bool {
119
+ $ this ->getAll ();
136
120
137
- return $ this ->headers ;
121
+ return array_key_exists ( $ key , $ this ->headers ) ;
138
122
}
139
123
}
0 commit comments