115 lines
3.8 KiB
PHP
115 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Cloudflare;
|
|
|
|
use App\Models\CommonModel;
|
|
use App\Models\Cloudflare\API\RecordModel;
|
|
use App\Entities\Cloudflare\ZoneEntity;
|
|
use App\Entities\Cloudflare\AccountEntity;
|
|
|
|
class ZoneModel extends CommonModel
|
|
{
|
|
const TABLE = "cloudflarezone";
|
|
const PK = "uid";
|
|
const TITLE = "domain";
|
|
const PARENT = "account_uid";
|
|
protected $table = self::TABLE;
|
|
protected $primaryKey = self::PK;
|
|
protected $useAutoIncrement = false;
|
|
protected $returnType = ZoneEntity::class; //object,array,entity명::class
|
|
protected $allowedFields = [self::PK, self::PARENT, self::TITLE, 'name_servers', 'original_name_servers', 'plan', 'development_mode', 'ipv6', 'security_level', 'status', 'updated_at', 'crated_at '];
|
|
protected $useTimestamps = true;
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
public function getTitleField(): string
|
|
{
|
|
return self::TITLE;
|
|
}
|
|
public function getFieldRule(string $field, array $rules): array
|
|
{
|
|
switch ($field) {
|
|
case self::PARENT:
|
|
$rules[$field] = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";;
|
|
break;
|
|
case self::TITLE:
|
|
case "plan":
|
|
$rules[$field] = "required|trim|string";
|
|
break;
|
|
case "name_servers":
|
|
case "security_level":
|
|
$rules[$field] = "if_exist|trim|string";
|
|
break;
|
|
case "development_mode":
|
|
case "ipv6":
|
|
$rules[$field] = "if_exist|in_list[on,off]";
|
|
break;
|
|
default:
|
|
$rules = parent::getFieldRule($field, $rules);
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
public function getFormFieldOption(string $field, array $options = []): array
|
|
{
|
|
switch ($field) {
|
|
default:
|
|
$this->where('status', DEFAULTS['STATUS']);
|
|
$this->orderBy(self::TITLE, 'asc');
|
|
$options = parent::getFormFieldOption($field, $options);
|
|
break;
|
|
}
|
|
return $options;
|
|
}
|
|
public function getEntityByPK(int $uid): null | ZoneEntity
|
|
{
|
|
$this->where(self::PK, $uid);
|
|
return $this->getEntity();
|
|
}
|
|
public function getEntityByID(string $id): null | ZoneEntity
|
|
{
|
|
$this->where(self::TITLE, $id);
|
|
return $this->getEntity();
|
|
}
|
|
public function getEntitysByParent(AccountEntity $account_entity)
|
|
{
|
|
$this->where(self::PARENT, $account_entity->getPK());
|
|
return $this->getEntitys();
|
|
}
|
|
//create용
|
|
public function create(array $formDatas = []): ZoneEntity
|
|
{
|
|
return $this->create_process(new ZoneEntity(), $formDatas);
|
|
}
|
|
//modify용
|
|
public function modify(ZoneEntity $entity, array $formDatas): ZoneEntity
|
|
{
|
|
return $this->modify_process($entity, $formDatas);
|
|
}
|
|
|
|
//도메인이 이미 존재하는지 체크
|
|
public function isUniqueDomain(string $account_uid, string $domain): bool
|
|
{
|
|
$this->where(self::PARENT, $account_uid);
|
|
$this->where(self::TITLE, $domain);
|
|
return is_null($this->first()) ? true : false;
|
|
}
|
|
//Index 검색용
|
|
public function setIndexWordFilter(string $word): void
|
|
{
|
|
$subquery = $this->db->table(RecordModel::TABLE)->select(RecordModel::PARENT)->like('content', $word, 'both');
|
|
$this->like(self::TITLE, $word, 'both'); //befor , after , both
|
|
$this->orWhereIn(self::PK, $subquery);
|
|
}
|
|
public function setIndexDateFilter($start, $end): void
|
|
{
|
|
$this->where('created_at >=', $start);
|
|
$this->where('created_at <=', $end);
|
|
}
|
|
public function setIndexOrderBy($field, $order = 'ASC')
|
|
{
|
|
$this->orderBy(self::TITLE . " ASC, {$field} {$order}");
|
|
}
|
|
}
|