diff --git a/src/Illuminate/Support/Uri.php b/src/Illuminate/Support/Uri.php index ea46aa54380b..8c863b86bdc1 100644 --- a/src/Illuminate/Support/Uri.php +++ b/src/Illuminate/Support/Uri.php @@ -149,6 +149,50 @@ public function host(): ?string return $this->uri->getHost(); } + /** + * Get the subdomain from the URI. + */ + public function subdomain(): ?string + { + if (! $host = $this->host()) { + return null; + } + + if (filter_var($host, FILTER_VALIDATE_IP)) { + return null; + } + + if (count($parts = explode('.', $host)) < 3) { + return null; + } + + return $parts[0]; + } + + /** + * Get the domain from the URI. + */ + public function domain(): ?string + { + if (! $host = $this->host()) { + return null; + } + + if (filter_var($host, FILTER_VALIDATE_IP)) { + return null; + } + + if (count($parts = explode('.', $host)) < 2) { + return null; + } + + if (count($parts) >= 3) { + return implode('.', array_slice($parts, 1)); + } + + return $host; + } + /** * Get the URI's port. */ diff --git a/tests/Support/SupportUriTest.php b/tests/Support/SupportUriTest.php index 1f86588a1eb8..5e4b4bee3169 100644 --- a/tests/Support/SupportUriTest.php +++ b/tests/Support/SupportUriTest.php @@ -221,6 +221,99 @@ public function test_path_segments() $this->assertEquals(3, $uri->pathSegments()->count()); } + public function test_subdomain_extraction() + { + $uri = Uri::of('https://api.laravel.com'); + $this->assertEquals('api', $uri->subdomain()); + + $uri = Uri::of('https://www.laravel.com'); + $this->assertEquals('www', $uri->subdomain()); + + $uri = Uri::of('https://admin.dashboard.laravel.com'); + $this->assertEquals('admin', $uri->subdomain()); + + $uri = Uri::of('https://laravel.com'); + $this->assertNull($uri->subdomain()); + + $uri = Uri::of('https://example.org'); + $this->assertNull($uri->subdomain()); + + $uri = Uri::of('https://192.168.1.1'); + $this->assertNull($uri->subdomain()); + + $uri = Uri::of('http://127.0.0.1:8080'); + $this->assertNull($uri->subdomain()); + + $uri = Uri::of('http://localhost'); + $this->assertNull($uri->subdomain()); + + $uri = Uri::of('http://localhost:3000'); + $this->assertNull($uri->subdomain()); + + $uri = Uri::of('/path/only'); + $this->assertNull($uri->subdomain()); + + $uri = Uri::of(''); + $this->assertNull($uri->subdomain()); + + $uri = Uri::of('https://staging.api.laravel.com'); + $this->assertEquals('staging', $uri->subdomain()); + + $uri = Uri::of('https://test-env.laravel.com'); + $this->assertEquals('test-env', $uri->subdomain()); + + $uri = Uri::of('https://example.co.uk'); + $this->assertEquals('example', $uri->subdomain()); + } + + public function test_domain_extraction() + { + $uri = Uri::of('https://api.laravel.com'); + $this->assertEquals('laravel.com', $uri->domain()); + + $uri = Uri::of('https://www.laravel.com'); + $this->assertEquals('laravel.com', $uri->domain()); + + $uri = Uri::of('https://admin.dashboard.laravel.com'); + $this->assertEquals('dashboard.laravel.com', $uri->domain()); + + $uri = Uri::of('https://laravel.com'); + $this->assertEquals('laravel.com', $uri->domain()); + + $uri = Uri::of('https://example.org'); + $this->assertEquals('example.org', $uri->domain()); + + $uri = Uri::of('https://192.168.1.1'); + $this->assertNull($uri->domain()); + + $uri = Uri::of('http://127.0.0.1:8080'); + $this->assertNull($uri->domain()); + + $uri = Uri::of('http://localhost'); + $this->assertNull($uri->domain()); + + $uri = Uri::of('http://localhost:3000'); + $this->assertNull($uri->domain()); + + $uri = Uri::of('/path/only'); + $this->assertNull($uri->domain()); + + $uri = Uri::of(''); + $this->assertNull($uri->domain()); + + $uri = Uri::of('https://staging.api.laravel.com'); + $this->assertEquals('api.laravel.com', $uri->domain()); + + $uri = Uri::of('https://test-env.example.co.uk'); + $this->assertEquals('example.co.uk', $uri->domain()); + + $uri = Uri::of('https://example.co.uk'); + $this->assertEquals('co.uk', $uri->domain()); + + $uri = Uri::of('https://hostname'); + $this->assertNull($uri->domain()); + } + public function test_macroable() { Uri::macro('myMacro', function () {