BladeOne.php
2 years ago
CSV.php
9 months ago
Calculations.php
2 years ago
Currency.php
8 months ago
Device.php
1 year ago
Device_Cache.php
2 years ago
Dir.php
5 months ago
Format.php
5 months ago
Link_Validator.php
6 months ago
Number_Formatter.php
5 months ago
Obj.php
6 months ago
Request.php
11 months ago
Salt.php
1 year ago
Security.php
1 year ago
Server.php
2 years ago
Singleton.php
2 years ago
String_Util.php
2 years ago
Timezone.php
5 months ago
URL.php
6 months ago
WP_Async_Request.php
1 year ago
URL.php
69 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Utils; |
| 4 | |
| 5 | use IAWPSCOPED\Illuminate\Support\Str; |
| 6 | use IAWPSCOPED\League\Uri\Contracts\UriException; |
| 7 | use IAWPSCOPED\League\Uri\Uri; |
| 8 | /** @internal */ |
| 9 | class URL |
| 10 | { |
| 11 | private $url; |
| 12 | public function __construct(string $url) |
| 13 | { |
| 14 | $this->url = $url; |
| 15 | } |
| 16 | public function is_valid_url() : bool |
| 17 | { |
| 18 | $valid_url = \filter_var($this->url, \FILTER_VALIDATE_URL); |
| 19 | if (!$valid_url) { |
| 20 | return \false; |
| 21 | } |
| 22 | try { |
| 23 | // Recommend approach for uri validation: https://uri.thephpleague.com/uri/6.0/rfc3986/#uri-validation |
| 24 | $components = Uri::createFromString($this->url); |
| 25 | if (\is_null($components->getHost())) { |
| 26 | return \false; |
| 27 | } |
| 28 | return \true; |
| 29 | } catch (UriException $e) { |
| 30 | return \false; |
| 31 | } |
| 32 | } |
| 33 | public function get_url() : ?string |
| 34 | { |
| 35 | if (!$this->is_valid_url()) { |
| 36 | return null; |
| 37 | } |
| 38 | return $this->url; |
| 39 | } |
| 40 | public function get_domain() : ?string |
| 41 | { |
| 42 | if (!$this->is_valid_url()) { |
| 43 | return null; |
| 44 | } |
| 45 | return Uri::createFromString($this->url)->getHost(); |
| 46 | } |
| 47 | public function get_extension() : ?string |
| 48 | { |
| 49 | if (!$this->is_valid_url()) { |
| 50 | return null; |
| 51 | } |
| 52 | $path = Uri::createFromString($this->url)->getPath(); |
| 53 | $file = Str::afterLast($path, '/'); |
| 54 | $extension = Str::afterLast($file, '.'); |
| 55 | return $extension !== "" ? $extension : null; |
| 56 | } |
| 57 | public function get_path() : ?string |
| 58 | { |
| 59 | if (!$this->is_valid_url()) { |
| 60 | return null; |
| 61 | } |
| 62 | return Uri::createFromString($this->url)->getPath(); |
| 63 | } |
| 64 | public static function new(string $url) : self |
| 65 | { |
| 66 | return new self($url); |
| 67 | } |
| 68 | } |
| 69 |