Passed
Push — master ( a3db06...6080f3 )
by Sebastian
03:13
created

URLInfoTrait::hasAuth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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 getUser() : ?string
53
    {
54
        return $this->getKey('user');
55
    }
56
57
    public function getPassword() : ?string
58
    {
59
        return $this->getKey('pass');
60
    }
61
62
    public function getQuery() : ?string
63
    {
64
        return $this->getKey('query');
65
    }
66
67
    public function getPath() : ?string
68
    {
69
        return $this->getKey('path');
70
    }
71
72
    public function getScheme() : ?string
73
    {
74
        return $this->getKey('scheme');
75
    }
76
77
    public function setHost(string $host) : self
78
    {
79
        return $this->setKey('host', $host);
80
    }
81
82
    public function setHostFromEmail(string $email) : self
83
    {
84
        $parts = explode('@', $email);
85
        $this->setHost(array_pop($parts));
86
87
        return $this;
88
    }
89
90
    public function setAuth(string $user, string $password) : self
91
    {
92
        $this->setKey('user', $user);
93
        $this->setKey('pass', $password);
94
95
        return $this;
96
    }
97
98
    public function setPath(string $path) : self
99
    {
100
        return $this->setKey('path', $path);
101
    }
102
103
    public function setKey(string $name, string $value) : self
104
    {
105
        $this->info[$name] = $value;
106
        return $this;
107
    }
108
109
    public function setIP(string $ip) : self
110
    {
111
        $this->setHost($ip);
112
        $this->setKey('ip', $ip);
113
114
        return $this;
115
    }
116
117
    public function setSchemeHTTPS() : self
118
    {
119
        return $this->setScheme('https');
120
    }
121
122
    public function setSchemeMailto() : self
123
    {
124
        return $this->setScheme('mailto');
125
    }
126
127
    public function setScheme(string $scheme) : self
128
    {
129
        return $this->setKey('scheme', $scheme);
130
    }
131
132
    public function setTypeURL() : self
133
    {
134
        return $this->setType(URLInfo::TYPE_URL);
135
    }
136
137
    public function setTypeEmail() : void
138
    {
139
        $this->setType(URLInfo::TYPE_EMAIL);
140
    }
141
142
    /**
143
     * @param string $type
144
     * @return $this
145
     */
146
    public function setType(string $type) : self
147
    {
148
        $this->info['type'] = $type;
149
        return $this;
150
    }
151
152
    public function removePath() : self
153
    {
154
        return $this->removeKey('path');
155
    }
156
157
    public function removeKey(string $name) : self
158
    {
159
        unset($this->info[$name]);
160
        return $this;
161
    }
162
163
    public function hasAuth() : bool
164
    {
165
        return isset($this->info['user'], $this->info['pass']);
166
    }
167
168
    public function hasKey(string $name) : bool
169
    {
170
        return isset($this->info[$name]);
171
    }
172
173
    public function hasHost() : bool
174
    {
175
        return $this->hasKey('host');
176
    }
177
178
    public function hasQuery() : bool
179
    {
180
        return $this->hasKey('query');
181
    }
182
183
    public function hasScheme() : bool
184
    {
185
        return $this->hasKey('scheme');
186
    }
187
188
    public function hasPath() : bool
189
    {
190
        return $this->hasKey('path');
191
    }
192
193
    public function hasFragment() : bool
194
    {
195
        return $this->hasKey('fragment');
196
    }
197
198
    public function isFragmentOnly() : bool
199
    {
200
        return
201
            $this->hasFragment()
202
            &&
203
            (
204
                !$this->hasScheme()
205
                &&
206
                !$this->hasHost()
207
                &&
208
                !$this->hasPath()
209
                &&
210
                !$this->hasQuery()
211
            );
212
    }
213
214
    public function isPathOnly() : bool
215
    {
216
        return
217
            $this->hasPath()
218
            &&
219
            (
220
                !$this->hasScheme()
221
                &&
222
                !$this->hasHost()
223
                &&
224
                !$this->hasQuery()
225
            );
226
    }
227
228
    public function isHostOnly() : bool
229
    {
230
        return
231
            $this->hasHost()
232
            &&
233
            (
234
                !$this->hasScheme()
235
                &&
236
                !$this->hasPath()
237
                &&
238
                !$this->hasQuery()
239
            );
240
    }
241
242
    public function isSchemeLess() : bool
243
    {
244
        return $this->isFragmentOnly();
245
    }
246
}
247