Add getRootDomain() to Utils (#514)

This commit is contained in:
Nabeel S 2020-01-28 12:42:46 -05:00 committed by GitHub
parent 819ebece6d
commit e431f75ad4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

View File

@ -3,6 +3,7 @@
namespace App\Support; namespace App\Support;
use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Str;
use Nwidart\Modules\Facades\Module; use Nwidart\Modules\Facades\Module;
/** /**
@ -47,4 +48,29 @@ class Utils
return $installer->isEnabled(); return $installer->isEnabled();
} }
/**
* Get the domain from a URL
*
* @param string $url
*
* @return string
*/
public static function getRootDomain(string $url): string
{
if (!Str::contains($url, ['https://', 'http://'])) {
$url = 'http://'.$url;
}
$domain = parse_url($url, PHP_URL_HOST);
$domain = explode('.', $domain);
$len = count($domain);
if ($len == 1) {
return $domain[0];
}
$domain = $domain[$len - 2].'.'.$domain[$len - 1];
return $domain;
}
} }

View File

@ -69,4 +69,22 @@ class UtilsTest extends TestCase
$hex_code = \App\Support\ICAO::createHexCode(); $hex_code = \App\Support\ICAO::createHexCode();
$this->assertNotNull($hex_code); $this->assertNotNull($hex_code);
} }
public function testGetDomain()
{
$tests = [
'http://phpvms.net',
'https://phpvms.net',
'phpvms.net',
'https://phpvms.net/index.php',
'https://demo.phpvms.net',
'https://demo.phpvms.net/file/index.php',
];
foreach ($tests as $case) {
$this->assertEquals('phpvms.net', \App\Support\Utils::getRootDomain($case));
}
$this->assertEquals('phpvms', \App\Support\Utils::getRootDomain('http://phpvms'));
}
} }