1 <?php
2
3 namespace Docolight\Http;
4
5 use Docolight\Support\Set;
6
7 /**
8 * HTTP Headers.
9 *
10 * ```php
11 * $headers = new Docolight\Http\Headers;
12 *
13 * $headers->set('My-Header-Index', 'My header value');
14 * ```
15 *
16 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
17 */
18 class Headers extends Set
19 {
20 /**
21 * Special-case HTTP headers that are otherwise unidentifiable as HTTP headers.
22 * Typically, HTTP headers in the $_SERVER array will be prefixed with
23 * `HTTP_` or `X_`. These are not so we list them here for later reference.
24 *
25 * @var array
26 */
27 protected static $special = array(
28 'CONTENT_TYPE',
29 'CONTENT_LENGTH',
30 'PHP_AUTH_USER',
31 'PHP_AUTH_PW',
32 'PHP_AUTH_DIGEST',
33 'AUTH_TYPE',
34 );
35
36 /**
37 * Extract HTTP headers from an array of data (e.g. $_SERVER).
38 *
39 * @param array $data
40 *
41 * @return array
42 */
43 public static function extract($data)
44 {
45 $results = array();
46
47 foreach ($data as $key => $value) {
48 $key = strtoupper($key);
49
50 if (strpos($key, 'X_') === 0 or strpos($key, 'HTTP_') === 0 or in_array($key, static::$special)) {
51 if ($key !== 'HTTP_CONTENT_LENGTH') {
52 $results[$key] = $value;
53 }
54 }
55 }
56
57 return $results;
58 }
59
60 /**
61 * Transform header name into canonical form.
62 *
63 * @param string $key
64 *
65 * @return string
66 */
67 protected function normalizeKey($key)
68 {
69 $key = strtolower($key);
70 $key = str_replace(array('-', '_'), ' ', $key);
71 $key = preg_replace('#^http #', '', $key);
72 $key = ucwords($key);
73 $key = str_replace(' ', '-', $key);
74
75 return $key;
76 }
77 }
78