198 lines
6.5 KiB
PHP
198 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
abstract class CommonModel extends Model
|
|
{
|
|
protected $table = '';
|
|
protected $primaryKey = '';
|
|
protected $useAutoIncrement = true;
|
|
protected $returnType = 'array';
|
|
|
|
protected $useSoftDeletes = false;
|
|
protected $protectFields = true;
|
|
protected $allowedFields = [];
|
|
|
|
protected bool $allowEmptyInserts = false;
|
|
protected bool $updateOnlyChanged = true;
|
|
|
|
protected array $casts = [];
|
|
protected array $castHandlers = [];
|
|
|
|
// Dates
|
|
protected $useTimestamps = false;
|
|
protected $dateFormat = 'datetime';
|
|
protected $createdField = 'created_at';
|
|
protected $updatedField = 'updated_at';
|
|
protected $deletedField = 'deleted_at';
|
|
|
|
// Validation
|
|
protected $validationRules = [];
|
|
protected $validationMessages = [];
|
|
protected $skipValidation = false;
|
|
protected $cleanValidationRules = true;
|
|
|
|
// Callbacks
|
|
protected $allowCallbacks = true;
|
|
protected $beforeInsert = [];
|
|
protected $afterInsert = [];
|
|
protected $beforeUpdate = [];
|
|
protected $afterUpdate = [];
|
|
protected $beforeFind = [];
|
|
protected $afterFind = [];
|
|
protected $beforeDelete = [];
|
|
protected $afterDelete = [];
|
|
|
|
protected function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
abstract public function getPK(): string;
|
|
abstract public function getTitleField(): string;
|
|
abstract public function setCreateField();
|
|
abstract public function setModifyField();
|
|
|
|
final public function getFields(): array
|
|
{
|
|
return $this->allowedFields;
|
|
}
|
|
final public function setFields(array $fields): void
|
|
{
|
|
$this->allowedFields = $fields;
|
|
}
|
|
final public function getRules(array $options): array
|
|
{
|
|
return $this->getValidationRules($options); //options=>except or only
|
|
}
|
|
final public function setRules(array $fields, $rules = []): void
|
|
{
|
|
foreach ($fields as $field) {
|
|
$rules = $this->getFieldRule($field, $rules);
|
|
}
|
|
$this->setValidationRules($rules);
|
|
}
|
|
|
|
|
|
final public function getUUID(): string
|
|
{
|
|
$randomBytes = bin2hex(random_bytes(32));
|
|
return sprintf(
|
|
'%08s-%04s-%04x-%04x-%12s',
|
|
substr($randomBytes, 0, 8),
|
|
substr($randomBytes, 8, 4),
|
|
substr($randomBytes, 12, 4),
|
|
substr($randomBytes, 16, 4),
|
|
substr($randomBytes, 20, 12)
|
|
);
|
|
}
|
|
protected function getUUIDFieldRule($condition = 'required'): string
|
|
{
|
|
return "{$condition}|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
|
|
}
|
|
protected function getFieldRule(string $field, array $rules): array
|
|
{
|
|
switch ($field) {
|
|
case $this->getPK():
|
|
//수동입력인경우
|
|
$rules[$field] = !$this->useAutoIncrement ? $this->getUUIDFieldRule() . "|is_unique[{$this->table}.{$field}]" : "required|numeric";
|
|
break;
|
|
case $this->getTitleField():
|
|
$rules[$field] = "required|trim|string";
|
|
break;
|
|
case "passwd":
|
|
$rules[$field] = "required|trim|string";
|
|
break;
|
|
case "confirmpassword":
|
|
$rules["confirmpassword"] = "required|trim|string|matches[passwd]";
|
|
break;
|
|
case "email":
|
|
$rules[$field] = "if_exist|trim|valid_email";
|
|
break;
|
|
case 'image':
|
|
$rules[$field] = "is_image[{$field}]|mime_in[{$field},image/jpg,image/jpeg,image/gif,image/png,image/webp]|max_size[{$field},300]|max_dims[{$field},2048,768]";
|
|
break;
|
|
case "updated_at":
|
|
case "created_at":
|
|
case "deleted_at":
|
|
$rules[$field] = "if_exist|valid_date";
|
|
break;
|
|
default:
|
|
$rules[$field] = "if_exist|string";
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
|
|
final public function getEntity()
|
|
{
|
|
return $this->asObject($this->returnType)->first();
|
|
}
|
|
final public function getEntitys(): array
|
|
{
|
|
return $this->asObject($this->returnType)->findAll();
|
|
}
|
|
|
|
private function save_process($entity)
|
|
{
|
|
// echo var_export($entity, true);
|
|
// exit;
|
|
if ($entity->hasChanged()) {
|
|
if (!$this->save($entity)) {
|
|
log_message("error", __FUNCTION__ . "에서 호출:" . $this->getLastQuery());
|
|
log_message("error", implode("\n", $this->errors()));
|
|
throw new \Exception(__FUNCTION__ . " 오류 발생.\n" . $this->getLastQuery() . "\n" . var_dump($this->errors(), true));
|
|
}
|
|
} else {
|
|
throw new \Exception(__FUNCTION__ . " 오류 발생.\n 기존정보와 동일하여 수정되지 않았습니다.");
|
|
}
|
|
return $entity;
|
|
}
|
|
|
|
//create , modify 직전 작업용 작업
|
|
protected function changeFormData(string $field, array $formDatas, $entity)
|
|
{
|
|
switch ($field) {
|
|
case "passwd":
|
|
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
|
|
$entity->$field = password_hash($formDatas[$field], PASSWORD_DEFAULT);
|
|
}
|
|
break;
|
|
case "content":
|
|
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
|
|
$entity->$field = htmlentities($formDatas[$field]);
|
|
}
|
|
break;
|
|
default:
|
|
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
|
|
$entity->$field = $formDatas[$field];
|
|
}
|
|
break;
|
|
}
|
|
return $entity;
|
|
}
|
|
|
|
protected function create_process($entity, array $formDatas = [])
|
|
{
|
|
foreach ($this->getFields() as $field) {
|
|
$entity = $this->changeFormData($field, $formDatas, $entity);
|
|
}
|
|
$entity = $this->save_process($entity);
|
|
//primaryKey가 자동입력이면
|
|
if ($this->useAutoIncrement) {
|
|
$pk = $this->getPK();
|
|
$entity->$pk = $this->getInsertID();
|
|
}
|
|
return $entity;
|
|
}
|
|
protected function modify_process($entity, array $formDatas = [])
|
|
{
|
|
foreach ($this->getFields() as $field) {
|
|
$entity = $this->changeFormData($field, $formDatas, $entity);
|
|
}
|
|
return $this->save_process($entity);
|
|
}
|
|
}
|