osb/app/Models/TLD.php

67 lines
1.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use App\Models\Service\Domain;
class TLD extends Model
{
protected $table = 'tlds';
/* 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;
}
/* ATTRIBUTES */
public function getNameAttribute($value): string
{
return strtoupper($value);
}
/* 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);
}
}