Automation/app/Models/Cloudflare/AuthModel.php
2024-09-20 19:51:36 +09:00

64 lines
2.0 KiB
PHP

<?php
namespace App\Models\Cloudflare;
use App\Entities\Cloudflare\AuthEntity;
use CodeIgniter\Model;
use stdClass;
class AuthModel extends Model
{
protected $table = 'cloudflareauth';
protected $primaryKey = 'uid';
protected $useAutoIncrement = true;
protected $returnType = AuthEntity::class; //object,array,entity명::class
protected $allowedFields = ['uid', 'id', 'authkey', 'oldkey', 'status', 'updated_at', 'created_at'];
protected $useTimestamps = true;
public function getTitleField(): string
{
return 'id';
}
public function getFieldRule(string $field, array $rules): array
{
switch ($field) {
case "id":
$rules[$field] = "required|valid_emailvalid_email|is_unique[cloudflareauth.id]";
break;
case "authkey":
$rules[$field] = "required|trim|smin_length[10]|max_length[200]";
break;
case "oldkey":
$rules[$field] = "if_exist|trim|smin_length[10]|max_length[200]";
break;
default:
$rules = parent::getFieldRule($field, $rules);
break;
}
return $rules;
}
public function getEntityByPK(int $uid): null | AuthEntity
{
$this->where($this->getPKField(), $uid);
return $this->getEntity();
}
public function getEntityByID(string $id): null | AuthEntity
{
$this->where($this->getTitleField(), $id);
return $this->getEntity();
}
//create용
public function create(array $formDatas = []): AuthEntity
{
$formDatas['oldkey'] = $formDatas['authkey'];
return $this->create_process(new AuthEntity(), $formDatas);
}
//modify용
public function modify(AuthEntity $entity, array $formDatas): AuthEntity
{
//입력후 authkey값을 oldkey값에 넣어주기 위함
$formDatas['oldkey'] = $entity->authkey;
return $this->modify_process($entity, $formDatas);
}
}