Passed
Push — master ( bfa3fc...bba89e )
by Sebastian
08:53
created

ParsedInfoFilter::filter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AppUtils\URLInfo\Parser;
6
7
use AppUtils\ConvertHelper;
8
use AppUtils\URLInfo;
9
use AppUtils\URLInfo\URISchemes;
10
use AppUtils\URLInfo\URLHosts;
11
use AppUtils\URLInfo\URLInfoTrait;
12
13
class ParsedInfoFilter
14
{
15
    use URLInfoTrait;
16
17
    private string $url;
18
19
    /**
20
     * @param string $url
21
     * @param array<string,mixed> $info
22
     */
23
    public function __construct(string $url, array $info)
24
    {
25
        $this->info = $info;
26
        $this->url = $url;
27
    }
28
29
    /**
30
     * @return array<string,mixed>
31
     */
32
    public function filter() : array
33
    {
34
        $this->info['type'] = URLInfo::TYPE_NONE;
35
36
        $this->filterScheme();
37
        $this->filterAuth();
38
        $this->filterHost();
39
        $this->filterPath();
40
        $this->filterKnownHosts();
41
        $this->filterParams();
42
        $this->filterEmail();
43
44
        return $this->info;
45
    }
46
47
    /**
48
     * Special case for Email addresses: the path component
49
     * must be stripped of spaces, as no spaces are allowed.
50
     * This differs from the usual path behavior, which is to
51
     * allow spaces.
52
     *
53
     * @return void
54
     */
55
    private function filterEmail() : void
56
    {
57
        $path = $this->info['path'] ?? '';
58
59
        if(strpos($path, '@') !== false)
60
        {
61
            $this->info['path'] = str_replace(' ', '', $path);
62
        }
63
    }
64
65
    private function filterScheme() : void
66
    {
67
        if($this->hasScheme())
68
        {
69
            $this->setScheme(strtolower($this->getScheme()));
70
        }
71
        else
72
        {
73
            $scheme = URISchemes::detectScheme($this->url);
74
            if(!empty($scheme)) {
75
                $this->setScheme(URISchemes::resolveSchemeName($scheme));
76
            }
77
        }
78
    }
79
80
    private function filterAuth() : void
81
    {
82
        if(!$this->hasAuth()) {
83
            return;
84
        }
85
86
        $this->setAuth(
87
            urldecode($this->getUser()),
88
            urldecode($this->getPassword())
89
        );
90
    }
91
92
    private function filterHost() : void
93
    {
94
        if(!$this->hasHost())
95
        {
96
            return;
97
        }
98
99
        $host = strtolower($this->getHost());
100
        $host = str_replace(' ', '', $host);
101
102
        $this->setHost($host);
103
    }
104
105
    private function filterPath() : void
106
    {
107
        if($this->hasPath()) {
108
            $this->setPath($this->getPath());
109
        }
110
    }
111
112
    private function filterKnownHosts() : void
113
    {
114
        $host = $this->getPath();
115
116
        if(empty($host) || !URLHosts::isHostKnown($host))
117
        {
118
            return;
119
        }
120
121
        $this->setHost($host);
122
        $this->removePath();
123
124
        if(!$this->hasScheme()) {
125
            $this->setSchemeHTTPS();
126
        }
127
    }
128
129
    private function filterParams() : void
130
    {
131
        $this->info['params'] = array();
132
133
        $query = $this->getQuery();
134
        if(empty($query)) {
135
            return;
136
        }
137
138
        $this->info['params'] = ConvertHelper::parseQueryString($query);
139
140
        ksort($this->info['params']);
141
    }
142
}
143