Passed
Push — master ( ffd7ca...6df4de )
by Evgeniy
06:28
created

SapiNormalizer::normalizeUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HttpSoft\Request;
6
7
use HttpSoft\Uri\UriFactory;
8
use Psr\Http\Message\UriInterface;
9
10
use function array_key_exists;
11
use function is_string;
12
use function strpos;
13
use function str_replace;
14
use function strtolower;
15
use function substr;
16
use function ucwords;
17
18
final class SapiNormalizer implements ServerNormalizerInterface
19
{
20
    /**
21
     * {@inheritDoc}
22
     */
23 6
    public function normalizeMethod(array $server): string
24
    {
25 6
        if (empty($server['REQUEST_METHOD'])) {
26 5
            return RequestMethodInterface::METHOD_GET;
27
        }
28
29 1
        return (string) $server['REQUEST_METHOD'];
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35 6
    public function normalizeProtocolVersion(array $server): string
36
    {
37 6
        if (empty($server['SERVER_PROTOCOL'])) {
38 5
            return '1.1';
39
        }
40
41 1
        return str_replace('HTTP/', '', (string) $server['SERVER_PROTOCOL']);
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47 6
    public function normalizeUri(array $server): UriInterface
48
    {
49 6
        return UriFactory::createFromServer($server);
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55 6
    public function normalizeHeaders(array $server): array
56
    {
57 6
        $headers = [];
58
59 6
        foreach ($server as $name => $value) {
60 4
            if (!is_string($name) ||  $value === '') {
61 2
                continue;
62
            }
63
64 4
            if (strpos($name, 'REDIRECT_') === 0) {
65
                if (array_key_exists($name = substr($name, 9), $server)) {
66
                    continue;
67
                }
68
            }
69
70 4
            if (strpos($name, 'HTTP_') === 0) {
71 2
                $headers[$this->normalizeHeaderName(substr($name, 5))] = $value;
72
            }
73
74 4
            if (strpos($name, 'CONTENT_') === 0) {
75 2
                $headers[$this->normalizeHeaderName($name)] = $value;
76
            }
77
        }
78
79 6
        return $headers;
80
    }
81
82
    /**
83
     * @param string $name
84
     * @return string
85
     */
86 2
    private function normalizeHeaderName(string $name): string
87
    {
88 2
        return str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', $name))));
89
    }
90
}
91