25 lines
749 B
PHP
25 lines
749 B
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Support;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Naive "last two labels" root-domain heuristic — good enough for the common case
|
||
|
|
* (example.com, somesite.es) but wrong for real-world two-part public suffixes like
|
||
|
|
* .co.uk or .com.au, where it would treat "co.uk" as the root instead of
|
||
|
|
* "example.co.uk". Doing that correctly means integrating the Mozilla Public Suffix
|
||
|
|
* List; this is a deliberate simplification until that's worth the added complexity.
|
||
|
|
*/
|
||
|
|
class RootDomain
|
||
|
|
{
|
||
|
|
public static function of(string $domain): string
|
||
|
|
{
|
||
|
|
$labels = explode('.', strtolower(trim($domain, '.')));
|
||
|
|
|
||
|
|
if (count($labels) <= 2) {
|
||
|
|
return implode('.', $labels);
|
||
|
|
}
|
||
|
|
|
||
|
|
return implode('.', array_slice($labels, -2));
|
||
|
|
}
|
||
|
|
}
|