cfmgrv4/app/Models/MapurlModel.php
2024-10-08 15:11:33 +09:00

65 lines
1.8 KiB
PHP

<?php
namespace App\Models;
use App\Entities\MapurlEntity;
use App\Models\CommonModel;
class MapurlModel extends CommonModel
{
const TABLE = "mapurl";
const PK = "uid";
const TITLE = "oldurl";
protected $table = self::TABLE;
protected $primaryKey = self::PK;
protected $returnType = MapurlEntity::class;
protected $allowedFields = [
"oldurl",
"newurl",
"status"
];
public function __construct()
{
parent::__construct();
}
public function getTitleField(): string
{
return self::TITLE;
}
public function getFieldRule(string $action, string $field): string
{
if (is_array($field)) {
throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
}
switch ($field) {
case "oldurl":
$rule = "required|valid_url_strict";
$rule .= $action == "create" ? "|is_unique[{$this->table}.{$field}]" : "";
break;
case "newurl":
$rule = "required|valid_url_strict";
break;
default:
$rule = parent::getFieldRule($action, $field);
break;
}
return $rule;
}
public function getEntityByPK(string $uid): null|MapurlEntity
{
$this->where($this->getPKField(), intval($uid));
return $this->getEntity();
}
//create용
public function create(array $formDatas = []): MapurlEntity
{
return $this->create_process(new MapurlEntity(), $formDatas);
}
//modify용
public function modify(MapurlEntity $entity, array $formDatas): MapurlEntity
{
return $this->modify_process($entity, $formDatas);
}
}