Array_To_CSV.php
3 years ago
Currency.php
3 years ago
Device.php
2 years ago
Number_Formatter.php
3 years ago
Option.php
2 years ago
Request.php
2 years ago
Salt.php
3 years ago
Security.php
3 years ago
Singleton.php
2 years ago
String_Util.php
3 years ago
URL.php
3 years ago
WP_Async_Request.php
3 years ago
WordPress_Site_Date_Format_Pattern.php
3 years ago
URL.php
43 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP\Utils; |
| 4 | |
| 5 | use IAWP_SCOPED\League\Uri\Contracts\UriException; |
| 6 | use IAWP_SCOPED\League\Uri\Uri; |
| 7 | class URL |
| 8 | { |
| 9 | private $url; |
| 10 | public function __construct(string $url) |
| 11 | { |
| 12 | $this->url = $url; |
| 13 | } |
| 14 | public function is_valid_url() : bool |
| 15 | { |
| 16 | $valid_url = \filter_var($this->url, \FILTER_VALIDATE_URL); |
| 17 | if (!$valid_url) { |
| 18 | return \false; |
| 19 | } |
| 20 | try { |
| 21 | // Recommend approach for uri validation: https://uri.thephpleague.com/uri/6.0/rfc3986/#uri-validation |
| 22 | $components = Uri::createFromString($this->url); |
| 23 | if (\is_null($components->getHost())) { |
| 24 | return \false; |
| 25 | } |
| 26 | return \true; |
| 27 | } catch (UriException $e) { |
| 28 | return \false; |
| 29 | } |
| 30 | } |
| 31 | public function get_domain() : ?string |
| 32 | { |
| 33 | if ($this->is_valid_url()) { |
| 34 | $components = Uri::createFromString($this->url); |
| 35 | $host = $components->getHost(); |
| 36 | if (!\is_null($host)) { |
| 37 | return $host; |
| 38 | } |
| 39 | } |
| 40 | return null; |
| 41 | } |
| 42 | } |
| 43 |