2
2
3
3
namespace System \Foundation \Http ;
4
4
5
- defined ('DS ' ) or exit ('No direct script access. ' );
5
+ defined ('DS ' ) or exit ('No direct access. ' );
6
6
7
7
class Header implements \IteratorAggregate, \Countable
8
8
{
@@ -31,20 +31,23 @@ public function __construct(array $headers = [])
31
31
*/
32
32
public function __toString ()
33
33
{
34
- if (! $ this ->headers ) {
34
+ if (!$ this ->headers ) {
35
35
return '' ;
36
36
}
37
37
38
- $ max = max (array_map ('strlen ' , array_keys ($ this ->headers ))) + 1 ;
38
+ $ max = max (array_map (function ($ key ) {
39
+ return mb_strlen ((string ) $ key , '8bit ' );
40
+ }, array_keys ($ this ->headers ))) + 1 ;
41
+
39
42
ksort ($ this ->headers );
40
43
41
44
$ content = '' ;
42
45
43
46
foreach ($ this ->headers as $ name => $ values ) {
44
- $ name = implode ( ' - ' , array_map ( ' ucfirst ' , explode ( ' - ' , $ name)) );
47
+ $ name = $ this -> standardizeKey ( $ name );
45
48
46
49
foreach ($ values as $ value ) {
47
- $ content .= sprintf ("%- {$ max }s %s \r\n" , $ name. ': ' , $ value );
50
+ $ content .= sprintf ("%- {$ max }s %s \r\n" , $ name . ': ' , $ value );
48
51
}
49
52
}
50
53
@@ -105,9 +108,9 @@ public function add(array $headers)
105
108
*/
106
109
public function get ($ key , $ default = null , $ first = true )
107
110
{
108
- $ key = strtr ( strtolower ( $ key), ' _ ' , ' - ' );
111
+ $ key = $ this -> standardizeKey ( $ key );
109
112
110
- if (! array_key_exists ($ key , $ this ->headers )) {
113
+ if (!array_key_exists ($ key , $ this ->headers )) {
111
114
if (null === $ default ) {
112
115
return $ first ? null : [];
113
116
}
@@ -131,18 +134,13 @@ public function get($key, $default = null, $first = true)
131
134
*/
132
135
public function set ($ key , $ values , $ replace = true )
133
136
{
134
- $ values = (array ) $ values ;
135
-
136
- $ key = strtr (strtolower ($ key ), '_ ' , '- ' );
137
- $ values = array_values ($ values );
138
-
139
- if (true === $ replace || ! isset ($ this ->headers [$ key ])) {
140
- $ this ->headers [$ key ] = $ values ;
141
- } else {
142
- $ this ->headers [$ key ] = array_merge ($ this ->headers [$ key ], $ values );
143
- }
137
+ $ key = $ this ->standardizeKey ($ key );
138
+ $ values = array_values ((array ) $ values );
139
+ $ this ->headers [$ key ] = (true === $ replace || !isset ($ this ->headers [$ key ]))
140
+ ? $ values
141
+ : array_merge ($ this ->headers [$ key ], $ values );
144
142
145
- if ('cache-control ' === $ key ) {
143
+ if ('Cache-Control ' === $ key ) {
146
144
$ this ->cacheControl = $ this ->parseCacheControl ($ values [0 ]);
147
145
}
148
146
}
@@ -156,7 +154,8 @@ public function set($key, $values, $replace = true)
156
154
*/
157
155
public function has ($ key )
158
156
{
159
- return array_key_exists (strtr (strtolower ($ key ), '_ ' , '- ' ), $ this ->headers );
157
+ $ key = $ this ->standardizeKey ($ key );
158
+ return array_key_exists ($ key , $ this ->headers );
160
159
}
161
160
162
161
/**
@@ -179,10 +178,11 @@ public function contains($key, $value)
179
178
*/
180
179
public function remove ($ key )
181
180
{
182
- $ key = strtr (strtolower ($ key ), '_ ' , '- ' );
181
+ $ key = $ this ->standardizeKey ($ key );
182
+
183
183
unset($ this ->headers [$ key ]);
184
184
185
- if ('cache-control ' === $ key ) {
185
+ if ('Cache-Control ' === $ key ) {
186
186
$ this ->cacheControl = [];
187
187
}
188
188
}
@@ -197,21 +197,19 @@ public function remove($key)
197
197
*/
198
198
public function getDate ($ key , \DateTime $ default = null )
199
199
{
200
- if (null === $ value = $ this ->get ($ key )) {
200
+ if (null === ( $ value = $ this ->get ($ key) )) {
201
201
return $ default ;
202
202
}
203
203
204
204
if (false === ($ date = \DateTime::createFromFormat (DATE_RFC2822 , $ value ))) {
205
- throw new \RuntimeException (sprintf (
206
- "The '%s' HTTP header is not parseable (%s). " , $ key , $ value
207
- ));
205
+ throw new \RuntimeException (sprintf ("The '%s' HTTP header is not parseable (%s). " , $ key , $ value ));
208
206
}
209
207
210
208
return $ date ;
211
209
}
212
210
213
211
/**
214
- * Tambahkan penunjuk cache-control .
212
+ * Tambahkan header Cache-Control .
215
213
*
216
214
* @param string $key
217
215
* @param mixed $value
@@ -223,7 +221,7 @@ public function addCacheControlDirective($key, $value = true)
223
221
}
224
222
225
223
/**
226
- * Periksa ada tidaknya suatu penunjuk cache-control .
224
+ * Periksa ada tidaknya suatu header Cache-Control .
227
225
*
228
226
* @param string $key
229
227
*
@@ -235,21 +233,19 @@ public function hasCacheControlDirective($key)
235
233
}
236
234
237
235
/**
238
- * Ambil penunjuk cache-control berdasarkan key-nya.
236
+ * Ambil penunjuk Cache-Control berdasarkan key-nya.
239
237
*
240
238
* @param string $key
241
239
*
242
240
* @return string|null
243
241
*/
244
242
public function getCacheControlDirective ($ key )
245
243
{
246
- return array_key_exists ($ key , $ this ->cacheControl )
247
- ? $ this ->cacheControl [$ key ]
248
- : null ;
244
+ return array_key_exists ($ key , $ this ->cacheControl ) ? $ this ->cacheControl [$ key ] : null ;
249
245
}
250
246
251
247
/**
252
- * Hapus penunjuk cache-control berdasarkan key-nya.
248
+ * Hapus penunjuk Cache-Control berdasarkan key-nya.
253
249
*
254
250
* @param string $key
255
251
*/
@@ -282,7 +278,7 @@ public function count()
282
278
}
283
279
284
280
/**
285
- * Ambil seluruh data cache-control .
281
+ * Ambil seluruh data Cache-Control .
286
282
*
287
283
* @return string
288
284
*/
@@ -296,19 +292,19 @@ protected function getCacheControlHeader()
296
292
if (true === $ value ) {
297
293
$ parts [] = $ key ;
298
294
} else {
299
- if (preg_match ('# [^a-zA-Z0-9._-]# ' , $ value )) {
300
- $ value = '" ' . $ value. '" ' ;
295
+ if (preg_match ('/ [^a-zA-Z0-9._-]/ ' , $ value )) {
296
+ $ value = '" ' . $ value . '" ' ;
301
297
}
302
298
303
- $ parts [] = $ key. '= ' . $ value ;
299
+ $ parts [] = $ key . '= ' . $ value ;
304
300
}
305
301
}
306
302
307
303
return implode (', ' , $ parts );
308
304
}
309
305
310
306
/**
311
- * Parse header cache-control .
307
+ * Parse header Cache-Control .
312
308
*
313
309
* @param string $header
314
310
*
@@ -317,7 +313,7 @@ protected function getCacheControlHeader()
317
313
protected function parseCacheControl ($ header )
318
314
{
319
315
preg_match_all (
320
- '# ([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?# ' ,
316
+ '/ ([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?/ ' ,
321
317
$ header ,
322
318
$ matches ,
323
319
PREG_SET_ORDER
@@ -326,11 +322,24 @@ protected function parseCacheControl($header)
326
322
$ parsed = [];
327
323
328
324
foreach ($ matches as $ match ) {
329
- $ parsed [strtolower ($ match [1 ])] = isset ($ match [3 ])
325
+ $ parsed [strtolower (( string ) $ match [1 ])] = isset ($ match [3 ])
330
326
? $ match [3 ]
331
327
: (isset ($ match [2 ]) ? $ match [2 ] : true );
332
328
}
333
329
334
330
return $ parsed ;
335
331
}
332
+
333
+ /**
334
+ * Standarisasi nama header
335
+ *
336
+ * @param string $key
337
+ *
338
+ * @return string
339
+ */
340
+ protected static function standardizeKey ($ key )
341
+ {
342
+ $ key = strtr (strtolower ((string ) $ key ), '_ ' , '- ' );
343
+ return str_replace (' ' , '- ' , ucwords (strtr ($ key , '- ' , ' ' )));
344
+ }
336
345
}
0 commit comments