Passed
Push — master ( b083ab...a3db06 )
by Sebastian
03:30
created

URLInfoTrait   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 39
eloc 60
c 1
b 0
f 0
dl 0
loc 193
rs 9.28

29 Methods

Rating   Name   Duplication   Size   Complexity  
A getKey() 0 3 1
A removeKey() 0 4 1
A setScheme() 0 3 1
A setTypeURL() 0 3 1
A hasHost() 0 3 1
A getHost() 0 3 1
A isSchemeLess() 0 3 1
A getPath() 0 3 1
A hasKey() 0 3 1
A setIP() 0 6 1
A setSchemeHTTPS() 0 3 1
A isPathOnly() 0 11 4
A isHostOnly() 0 11 4
A getInfo() 0 3 1
A getType() 0 3 1
A setSchemeMailto() 0 3 1
A setHostFromEmail() 0 4 1
A setKey() 0 4 1
A isFragmentOnly() 0 13 5
A getScheme() 0 3 1
A hasQuery() 0 3 1
A hasPath() 0 3 1
A setHost() 0 3 1
A removePath() 0 3 1
A setType() 0 4 1
A hasFragment() 0 3 1
A hasScheme() 0 3 1
A setPath() 0 3 1
A setTypeEmail() 0 3 1
1
<?php
2
/**
3
 * @package Application Utils
4
 * @subpackage URLInfo
5
 * @see \AppUtils\URLInfo\URLInfoTrait
6
 */
7
8
declare(strict_types=1);
9
10
namespace AppUtils\URLInfo;
11
12
use AppUtils\URLInfo;
13
14
/**
15
 * Trait used for classes that access a URL info array,
16
 * as parsed using the parse_url method.
17
 *
18
 * @package Application Utils
19
 * @subpackage URLInfo
20
 * @author Sebastian Mordziol <[email protected]>
21
 */
22
trait URLInfoTrait
23
{
24
    /**
25
     * @var array<string,mixed>
26
     */
27
    protected array $info = array();
28
29
    /**
30
     * @return array<string,mixed>
31
     */
32
    public function getInfo() : array
33
    {
34
        return $this->info;
35
    }
36
37
    public function getKey(string $name) : ?string
38
    {
39
        return $this->info[$name] ?? null;
40
    }
41
42
    public function getHost() : ?string
43
    {
44
        return $this->getKey('host');
45
    }
46
47
    public function getType() : string
48
    {
49
        return $this->getKey('type');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getKey('type') could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
50
    }
51
52
    public function getPath() : ?string
53
    {
54
        return $this->getKey('path');
55
    }
56
57
    public function getScheme() : ?string
58
    {
59
        return $this->getKey('scheme');
60
    }
61
62
    public function setHost(string $host) : self
63
    {
64
        return $this->setKey('host', $host);
65
    }
66
67
    public function setHostFromEmail(string $email) : void
68
    {
69
        $parts = explode('@', $email);
70
        $this->setHost(array_pop($parts));
71
    }
72
73
    public function setPath(string $path) : self
74
    {
75
        return $this->setKey('path', $path);
76
    }
77
78
    public function setKey(string $name, string $value) : self
79
    {
80
        $this->info[$name] = $value;
81
        return $this;
82
    }
83
84
    public function setIP(string $ip) : self
85
    {
86
        $this->setHost($ip);
87
        $this->setKey('ip', $ip);
88
89
        return $this;
90
    }
91
92
    public function setSchemeHTTPS() : self
93
    {
94
        return $this->setScheme('https');
95
    }
96
97
    public function setSchemeMailto() : self
98
    {
99
        return $this->setScheme('mailto');
100
    }
101
102
    public function setScheme(string $scheme) : self
103
    {
104
        return $this->setKey('scheme', $scheme);
105
    }
106
107
    public function setTypeURL() : self
108
    {
109
        return $this->setType(URLInfo::TYPE_URL);
110
    }
111
112
    public function setTypeEmail() : void
113
    {
114
        $this->setType(URLInfo::TYPE_EMAIL);
115
    }
116
117
    /**
118
     * @param string $type
119
     * @return $this
120
     */
121
    public function setType(string $type) : self
122
    {
123
        $this->info['type'] = $type;
124
        return $this;
125
    }
126
127
    public function removePath() : self
128
    {
129
        return $this->removeKey('path');
130
    }
131
132
    public function removeKey(string $name) : self
133
    {
134
        unset($this->info[$name]);
135
        return $this;
136
    }
137
138
    public function hasKey(string $name) : bool
139
    {
140
        return isset($this->info[$name]);
141
    }
142
143
    public function hasHost() : bool
144
    {
145
        return $this->hasKey('host');
146
    }
147
148
    public function hasQuery() : bool
149
    {
150
        return $this->hasKey('query');
151
    }
152
153
    public function hasScheme() : bool
154
    {
155
        return $this->hasKey('scheme');
156
    }
157
158
    public function hasPath() : bool
159
    {
160
        return $this->hasKey('path');
161
    }
162
163
    public function hasFragment() : bool
164
    {
165
        return $this->hasKey('fragment');
166
    }
167
168
    public function isFragmentOnly() : bool
169
    {
170
        return
171
            $this->hasFragment()
172
            &&
173
            (
174
                !$this->hasScheme()
175
                &&
176
                !$this->hasHost()
177
                &&
178
                !$this->hasPath()
179
                &&
180
                !$this->hasQuery()
181
            );
182
    }
183
184
    public function isPathOnly() : bool
185
    {
186
        return
187
            $this->hasPath()
188
            &&
189
            (
190
                !$this->hasScheme()
191
                &&
192
                !$this->hasHost()
193
                &&
194
                !$this->hasQuery()
195
            );
196
    }
197
198
    public function isHostOnly() : bool
199
    {
200
        return
201
            $this->hasHost()
202
            &&
203
            (
204
                !$this->hasScheme()
205
                &&
206
                !$this->hasPath()
207
                &&
208
                !$this->hasQuery()
209
            );
210
    }
211
212
    public function isSchemeLess() : bool
213
    {
214
        return $this->isFragmentOnly();
215
    }
216
}
217