<?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);
    }
}
