HeaderFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 40
rs 10
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B marshalHeaders() 0 31 8
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Http\Message\Factory;
15
16
use function array_key_exists;
17
use function str_replace;
18
use function strtolower;
19
use function substr;
20
21
abstract class HeaderFactory
22
{
23
    /**
24
     * Marshal headers from $_SERVER.
25
     *
26
     * @param array<string, string> $server
27
     *
28
     * @return array<string, string[]>
29
     */
30
    public static function marshalHeaders(array $server): array
31
    {
32
        $headers = [];
33
34
        foreach ($server as $key => $value) {
35
            // Apache prefixes environment variables with REDIRECT_
36
            // if they are added by rewrite rules
37
            if (str_starts_with($key, 'REDIRECT_')) {
38
                $key = substr($key, 9);
39
40
                // We will not overwrite existing variables with the
41
                // prefixed versions, though
42
                if (array_key_exists($key, $server)) {
43
                    continue;
44
                }
45
            }
46
47
            if ($value && str_starts_with($key, 'HTTP_')) {
48
                $name           = str_replace('_', '-', strtolower(substr($key, 5)));
49
                $headers[$name] = [$value];
50
51
                continue;
52
            }
53
54
            if ($value && str_starts_with($key, 'CONTENT_')) {
55
                $name           = 'content-' . strtolower(substr($key, 8));
56
                $headers[$name] = [$value];
57
            }
58
        }
59
60
        return $headers;
61
    }
62
}
63