-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Extract Domain formatter + add more formats to accept (#3000)
- Loading branch information
Showing
5 changed files
with
92 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
class MastodonDomainExtractionService | ||
{ | ||
private const string HTTP_PREFIX = 'https://'; | ||
private const string REPLACE_PREFIX = 'http://'; | ||
|
||
public function formatDomain(string $domain): string { | ||
$domain = strtolower(trim($domain)); | ||
|
||
if (empty($domain)) { | ||
return ''; | ||
} | ||
|
||
$domain = $this->removeProtocolFromUrl($domain); | ||
$domain = $this->removePathFromUrl($domain); | ||
$domain = $this->extractUrlFromUserHandleFormat($domain); | ||
|
||
return $this->forceHttps($domain); | ||
} | ||
|
||
private function extractUrlFromUserHandleFormat(string $domain): string { | ||
if (str_contains($domain, '@')) { | ||
$domain = last(explode('@', $domain)); | ||
} | ||
|
||
return $domain; | ||
} | ||
|
||
private function removePathFromUrl(string $domain): string { | ||
return explode('/', $domain)[0]; | ||
} | ||
|
||
private function removeProtocolFromUrl(string $domain): string { | ||
return preg_replace('/^https?:\/\//', '', $domain); | ||
} | ||
|
||
private function forceHttps(string $domain): string { | ||
$domain = str_replace(self::REPLACE_PREFIX, self::HTTP_PREFIX, $domain); | ||
if (!str_starts_with($domain, self::HTTP_PREFIX)) { | ||
$domain = self::HTTP_PREFIX . $domain; | ||
} | ||
|
||
return $domain; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
tests/Unit/Services/MastodonDomainExtractionServiceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Services; | ||
|
||
use App\Services\MastodonDomainExtractionService; | ||
use Illuminate\Support\Facades\Validator; | ||
use Tests\Unit\UnitTestCase; | ||
|
||
class MastodonDomainExtractionServiceTest extends UnitTestCase | ||
{ | ||
/** | ||
* @dataProvider providerTestFormatDomain | ||
*/ | ||
public function testFormatDomain($case, $expected): void { | ||
$formatted = (new MastodonDomainExtractionService())->formatDomain($case); | ||
$this->assertEquals($expected, $formatted); | ||
|
||
$validated = Validator::make(['domain' => $formatted], ['domain' => ['active_url']]); | ||
|
||
$this->assertFalse($validated->fails()); | ||
} | ||
|
||
public static function providerTestFormatDomain(): array { | ||
return [ | ||
[' https://github.com ', 'https://github.com'], | ||
[' ', ''], | ||
['', ''], | ||
['https://github.com', 'https://github.com'], | ||
['http://github.com', 'https://github.com'], | ||
['github.com', 'https://github.com'], | ||
['great_username@github.com', 'https://github.com'], | ||
['@great_username@github.com', 'https://github.com'], | ||
['https://traewelling.github.io', 'https://traewelling.github.io'], | ||
['https://traewelling.github.io/', 'https://traewelling.github.io'], | ||
['https://traewelling.github.io/@foobar', 'https://traewelling.github.io'], | ||
['github.com/mastodon/mastodon', 'https://github.com'], | ||
['@user@github.com/@mastodon/mastodon', 'https://github.com'] // who would do this? | ||
]; | ||
} | ||
} |