1 | <?php |
||
8 | class Url |
||
9 | { |
||
10 | /** @var string */ |
||
11 | protected $inputUrl; |
||
12 | |||
13 | /** @var array */ |
||
14 | protected $parsedUrl; |
||
15 | |||
16 | /** @var string */ |
||
17 | protected $validatedURL; |
||
18 | |||
19 | /** @var string */ |
||
20 | protected $ipAddress; |
||
21 | |||
22 | 1 | public function __toString() |
|
23 | { |
||
24 | 1 | return $this->getInputUrl(); |
|
25 | } |
||
26 | |||
27 | 21 | private static function verifyAndGetDNS($domain): ?string |
|
28 | { |
||
29 | 21 | $domainIp = gethostbyname($domain); |
|
30 | 21 | if (! filter_var($domainIp, FILTER_VALIDATE_IP)) { |
|
31 | 4 | return null; |
|
32 | } |
||
33 | |||
34 | 19 | return $domainIp; |
|
35 | } |
||
36 | |||
37 | 22 | public function __construct(string $url) |
|
38 | { |
||
39 | 22 | $this->inputUrl = $url; |
|
40 | 22 | $parser = new UriParser(); |
|
41 | 22 | $this->parsedUrl = $parser($this->inputUrl); |
|
42 | |||
43 | // Verify parsing has a host |
||
44 | 22 | if (is_null($this->parsedUrl['host'])) { |
|
45 | try { |
||
46 | 18 | $this->parsedUrl = $parser('https://'.$this->inputUrl); |
|
47 | } catch (\Exception $e) { |
||
48 | throw InvalidUrl::couldNotValidate($url); |
||
49 | } |
||
50 | 18 | if (is_null($this->parsedUrl['host'])) { |
|
51 | throw InvalidUrl::couldNotDetermineHost($url); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | 22 | if (! filter_var($this->getValidUrl(), FILTER_VALIDATE_URL)) { |
|
56 | 1 | throw InvalidUrl::couldNotValidate($url); |
|
57 | } |
||
58 | |||
59 | 21 | $this->ipAddress = self::verifyAndGetDNS($this->parsedUrl['host']); |
|
60 | 21 | $this->validatedURL = $url; |
|
61 | 21 | } |
|
62 | |||
63 | 8 | public function getIp(): ?string |
|
71 | |||
72 | 6 | public function getInputUrl(): string |
|
76 | |||
77 | 22 | public function getHostName(): string |
|
81 | |||
82 | 2 | public function getValidatedURL(): string |
|
86 | |||
87 | 22 | public function getPort(): string |
|
91 | |||
92 | 12 | public function getTestURL(): string |
|
93 | { |
||
94 | 12 | return "{$this->getHostName()}:{$this->getPort()}"; |
|
96 | |||
97 | 22 | public function getValidUrl(): string |
|
105 | } |
||
106 |