| 1 | <?php  | 
            ||
| 7 | class Headers  | 
            ||
| 8 | { | 
            ||
| 9 | /** @var array */  | 
            ||
| 10 | private array $headers;  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 11 | |||
| 12 | public function __construct(array $headers = [])  | 
            ||
| 13 |     { | 
            ||
| 14 | $this->headers = $headers;  | 
            ||
| 15 | }  | 
            ||
| 16 | |||
| 17 | /**  | 
            ||
| 18 | * Get headers from $_SERVER global.  | 
            ||
| 19 | * @return array  | 
            ||
| 20 | */  | 
            ||
| 21 | public static function getHeadersArrayFromGlobals(): array  | 
            ||
| 22 |     { | 
            ||
| 23 | $headers = array_filter($_SERVER, [__CLASS__, 'isServerKeyAHeader'], ARRAY_FILTER_USE_KEY);  | 
            ||
| 24 | $headers = array_map([__CLASS__, 'wrapValuesInArray'], $headers);  | 
            ||
| 25 |         $headerKeys = array_map('strval', array_keys($headers)); | 
            ||
| 26 | $headerKeys = array_map([__CLASS__, 'stripKeyOfLeadingHttpPrefix'], $headerKeys);  | 
            ||
| 27 |         $headerKeys = array_map('strval', array_values($headerKeys)); | 
            ||
| 28 | |||
| 29 | return array_combine($headerKeys, $headers);  | 
            ||
| 30 | }  | 
            ||
| 31 | |||
| 32 | /**  | 
            ||
| 33 | * @param string $serverParameter  | 
            ||
| 34 | * @return bool  | 
            ||
| 35 | */  | 
            ||
| 36 | private static function isServerKeyAHeader(string $serverParameter): bool  | 
            ||
| 37 |     { | 
            ||
| 38 | return stripos($serverParameter, 'HTTP_') === 0;  | 
            ||
| 39 | }  | 
            ||
| 40 | |||
| 41 | /**  | 
            ||
| 42 | * @param mixed $header  | 
            ||
| 43 | * @return array  | 
            ||
| 44 | */  | 
            ||
| 45 | private static function wrapValuesInArray($header): array  | 
            ||
| 46 |     { | 
            ||
| 47 | return is_array($header) ? $header : [$header];  | 
            ||
| 48 | }  | 
            ||
| 49 | |||
| 50 | /**  | 
            ||
| 51 | * @param mixed $headerKey  | 
            ||
| 52 | * @return string  | 
            ||
| 53 | */  | 
            ||
| 54 | private static function stripKeyOfLeadingHttpPrefix($headerKey): string  | 
            ||
| 55 |     { | 
            ||
| 56 |         return is_string($headerKey) ? str_replace('HTTP_', '', $headerKey) : ''; | 
            ||
| 57 | }  | 
            ||
| 58 | }  | 
            ||
| 59 |