Dispositif /
Wikibot
| 1 | <?php |
||
| 2 | /* |
||
| 3 | * This file is part of dispositif/wikibot application (@github) |
||
| 4 | * 2019-2023 © Philippe M./Irønie <[email protected]> |
||
| 5 | * For the full copyright and MIT license information, view the license file. |
||
| 6 | */ |
||
| 7 | |||
| 8 | declare(strict_types=1); |
||
| 9 | |||
| 10 | namespace App\Infrastructure; |
||
| 11 | |||
| 12 | use App\Application\Utils\HttpUtil; |
||
| 13 | use App\Domain\InfrastructurePorts\InternetDomainParserInterface; |
||
| 14 | use Exception; |
||
| 15 | use Pdp\Domain; |
||
| 16 | use Pdp\ResolvedDomainName; |
||
| 17 | use Pdp\Rules; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Doc https://packagist.org/packages/jeremykendall/php-domain-parser |
||
| 21 | */ |
||
| 22 | class InternetDomainParser implements InternetDomainParserInterface |
||
| 23 | { |
||
| 24 | private const PATH_CACHE_PUBLIC_SUFFIX_LIST = __DIR__ . '/resources/public_suffix_list.dat'; |
||
| 25 | |||
| 26 | private readonly Rules $rules; |
||
| 27 | |||
| 28 | public function __construct() |
||
| 29 | { |
||
| 30 | if (!file_exists(self::PATH_CACHE_PUBLIC_SUFFIX_LIST)) { |
||
| 31 | throw new Exception('Public suffix list not found'); |
||
| 32 | } |
||
| 33 | $this->rules = Rules::fromPath(self::PATH_CACHE_PUBLIC_SUFFIX_LIST); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 34 | } |
||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * https://www.google.fr => google.fr |
||
| 39 | * http://fu.co.uk => fu.co.uk |
||
| 40 | * @throws Exception |
||
| 41 | */ |
||
| 42 | public function getRegistrableDomainFromURL(string $httpURL): string |
||
| 43 | { |
||
| 44 | $result = $this->getResolvedDomainName($httpURL); |
||
| 45 | |||
| 46 | return $result->registrableDomain()->toString(); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * todo move ? HttpUtil |
||
| 51 | * Ok static method (only native php parsing). |
||
| 52 | * https://www.google.fr => google.fr |
||
| 53 | * http://fu.co.uk => fu.co.uk |
||
| 54 | */ |
||
| 55 | public static function extractSubdomainString(string $httpURL): string |
||
| 56 | { |
||
| 57 | if (!HttpUtil::isHttpURL($httpURL)) { |
||
| 58 | throw new Exception('string is not an URL ' . $httpURL); |
||
| 59 | } |
||
| 60 | |||
| 61 | return parse_url($httpURL, PHP_URL_HOST); |
||
| 62 | } |
||
| 63 | |||
| 64 | protected function getResolvedDomainName(string $httpURL): ResolvedDomainName |
||
| 65 | { |
||
| 66 | $domain = Domain::fromIDNA2008(parse_url($httpURL, PHP_URL_HOST)); |
||
| 67 | |||
| 68 | return $this->rules->resolve($domain); |
||
| 69 | } |
||
| 70 | } |