2022-02-01 05:40:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-08-10 05:18:56 +00:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
use App\Models\Service\Domain;
|
2022-02-01 05:40:46 +00:00
|
|
|
|
|
|
|
class TLD extends Model
|
|
|
|
{
|
|
|
|
protected $table = 'tlds';
|
|
|
|
|
2022-08-10 05:18:56 +00:00
|
|
|
/* RELATIONS */
|
|
|
|
|
|
|
|
public function domains()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Domain::class,'tld_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* STATIC */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a domain name, return the TLD component
|
|
|
|
*
|
|
|
|
* @param string $domainname
|
|
|
|
* @return TLD|null
|
|
|
|
*/
|
|
|
|
public static function domaintld(string $domainname): ?TLD
|
|
|
|
{
|
|
|
|
$tld = NULL;
|
|
|
|
|
|
|
|
foreach (self::get() as $o) {
|
|
|
|
// Find the most specific match
|
|
|
|
if (preg_match('/'.$o->name.'$/i',$domainname) && (! $tld || (strlen($o->name) > strlen($tld->name))))
|
|
|
|
$tld = $o;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tld;
|
|
|
|
}
|
|
|
|
|
2022-02-01 05:40:46 +00:00
|
|
|
/* ATTRIBUTES */
|
|
|
|
|
|
|
|
public function getNameAttribute($value): string
|
|
|
|
{
|
|
|
|
return strtoupper($value);
|
|
|
|
}
|
2022-08-10 05:18:56 +00:00
|
|
|
|
|
|
|
/* METHOD */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a domain name, return the domain part
|
|
|
|
*
|
|
|
|
* @param string $domainname
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function domain_part(string $domainname): ?string
|
|
|
|
{
|
|
|
|
$domainname = strtoupper($domainname);
|
|
|
|
|
|
|
|
// Quick check that this domain is part of this model
|
|
|
|
if (! Str::endsWith($domainname,'.'.$this->name))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return Str::replaceLast('.'.$this->name,'',$domainname);
|
|
|
|
}
|
2022-02-01 05:40:46 +00:00
|
|
|
}
|