102 lines
2.6 KiB
PHP
102 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\HPILOEntity;
|
|
|
|
class HPILOModel extends BaseModel
|
|
{
|
|
protected $table = "tw_hpilo";
|
|
protected $returnType = HPILOEntity::class;
|
|
public function __construct()
|
|
{
|
|
parent::__construct('HPILO');
|
|
$this->allowedFields = [
|
|
...$this->allowedFields,
|
|
'customer', 'ip', 'port', 'id', 'passwd', 'status',
|
|
'model', 'processor', 'memory', 'health', 'power', 'detail',
|
|
];
|
|
$this->validationRules = [
|
|
...$this->validationRules,
|
|
...$this->getFieldRules($this->allowedFields),
|
|
];
|
|
}
|
|
public function getTitleField(): string
|
|
{
|
|
return 'customer';
|
|
}
|
|
public function getContentField(): string
|
|
{
|
|
return 'model';
|
|
}
|
|
protected function getFieldRule(string $field, array $rules, string $action = ""): array
|
|
{
|
|
switch ($field) {
|
|
case "customer":
|
|
$rules[$field] = "required|trim|string";
|
|
$rules[$field] .= $action == "insert" ? "|is_unique[{$this->table}.{$field}]" : "";
|
|
break;
|
|
case "ip":
|
|
$rules[$field] = "required|valid_ip[ipv4]";
|
|
break;
|
|
case "port":
|
|
$rules[$field] = "required|numeric";
|
|
break;
|
|
case "id":
|
|
$rules[$field] = "required|string";
|
|
break;
|
|
case "passwd":
|
|
$rules[$field] = "required|trim|string";
|
|
$rules["confirmpassword"] = "required|trim|string|matches[passwd]";
|
|
break;
|
|
case "model":
|
|
case "processor":
|
|
case "memory":
|
|
case "health":
|
|
case "power":
|
|
case "detail":
|
|
$rules[$field] = "required|string";
|
|
break;
|
|
default:
|
|
$rules = parent::getFieldRule($field, $rules, $action);
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
public function getEntity($conditions): HPILOEntity
|
|
{
|
|
return $this->where($conditions)->first() ?: throw new \Exception("해당 데이터가 없습니다.\n" . var_export($conditions, true));
|
|
}
|
|
protected function changeFormData(string $action, string $field, array $formDatas, $entity)
|
|
{
|
|
switch ($field) {
|
|
case "passwd": //API에서 사용하므로 암호를 바꾸면 않됨
|
|
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
|
|
$entity->$field = $formDatas[$field];
|
|
}
|
|
break;
|
|
default:
|
|
return parent::changeFormData($action, $field, $formDatas, $entity);
|
|
break;
|
|
}
|
|
return $entity;
|
|
}
|
|
public function create(array $formDatas): HPILOEntity
|
|
{
|
|
return $this->create_process(new HPILOEntity(), $formDatas);
|
|
}
|
|
public function modify(HPILOEntity $entity, array $formDatas): HPILOEntity
|
|
{
|
|
return $this->modify_process($entity, $formDatas);
|
|
}
|
|
|
|
//Index관련
|
|
public function setIndexWordFilter(string $word)
|
|
{
|
|
if ($word !== DEFAULTS['EMPTY']) {
|
|
parent::setIndexWordFilter($word);
|
|
$this->orLike($this->getTitleField(), $word, "both"); //befor , after , both
|
|
}
|
|
}
|
|
}
|