allowedFields = ["updated_at", "created_at"]; if (!$this->useAutoIncrement) { array_push($this->allowedFields, $this->primaryKey); } $this->validationRules = []; } abstract public function getEntity($uid): BaseEntity; abstract public function getEntitys($conditions): array; abstract public function getFieldFilters(): array; abstract public function getFields(string $action): array; protected function getFieldRule(string $field, array $rules, string $action = ""): array { switch ($field) { case "user_uid": $rules[$field] = "if_exist|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]"; break; case "passwd": $rules[$field] = "if_exist|trim|string"; if (!$action) { $rules["confirmpassword"] = "if_exist|trim|string|matches[passwd]"; } break; case "view_cnt": $rules[$field] = "if_exist|numeric"; break; case "upload_file": $rules[$field] = !$action ? "if_exist|string" : "if_exist|uploaded[{$field}]|is_image[{$field}]|mime_in[{$field},image/jpg,image/jpeg,image/gif,image/png,image/webp]|max_size[{$field},100]|max_dims[{$field},1024,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 getFieldRules(array $fields, string $action = ""): array { $rules = array(); foreach ($fields as $field) { $rules = $this->getFieldRule($field, $rules, $action); } return $rules; } public function getFieldBatchFilters(array $skips = array()): array { //allowedFields에서 추가했으므로 Controller에는 적용되지 않게하기위함 $fields = array(); foreach ($this->getFieldFilters() as $field) { if (!in_array($field, $skips)) { array_push($fields, $field); } } return $fields; } final public function getPrimaryKey() { return $this->primaryKey; } final public function getFieldFormOptions($conditions, $options = array()): array { foreach ($this->getEntitys($conditions) as $entity) { $options[$entity->getPrimaryKey()] = $entity->getTitle(); } return $options; } final public function getUUID() { $randomBytes = bin2hex(random_bytes(16)); return sprintf( "%s-%s-%s-%s-%s", substr($randomBytes, 0, 8), substr($randomBytes, 8, 4), substr($randomBytes, 12, 4), substr($randomBytes, 16, 4), substr($randomBytes, 20) ); } //View관련 (게시판등의 조회수 증가함수) final public function increaseViewCount($uid, string $field = "view_cnt", int $cnt = 1) { //escape -> false옵션 반드시 있어야함 $this->builder()->set($field, "{$field}+{$cnt}", false); $this->builder()->where($this->primaryKey, $uid); $this->builder()->update(); } //create , modify 직전 작업용 작업 protected function changeFormData(string $field, array $formDatas, $entity) { switch ($field) { case $this->primaryKey: //primaryKey가 자동입력이 아니면 if (!$this->useAutoIncrement) { $pk = $this->primaryKey; $entity->$pk = $this->getUUID(); } break; case "user_uid": if (array_key_exists($field, $formDatas) && $formDatas[$field]) { $entity->$field = $formDatas[$field]; } elseif (session()->get(SESSION_NAMES["ISLOGIN"])) { $auth = session()->get(SESSION_NAMES["AUTH"]); $entity->$field = $auth[AUTH_FIELDS["ID"]]; } break; 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; } private function save_process($entity) { 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" . var_export($this->errors(), true)); } //primaryKey가 자동입력이면 if ($this->useAutoIncrement) { $pk = $this->primaryKey; $entity->$pk = $this->insertID(); } } else { throw new \Exception(__FUNCTION__ . " 오류 발생.\n 기존정보와 동일하여 수정되지 않았습니다."); } return $entity; } protected function create_process($entity, array $formDatas) { foreach ($this->allowedFields as $field) { $entity = $this->changeFormData($field, $formDatas, $entity); } // echo var_export($this->allowedFields); // echo "
"; // echo var_export($entity); // exit; return $this->save_process($entity); } final protected function modify_process($entity, array $formDatas) { foreach ($this->allowedFields as $field) { if ($field != $this->primaryKey) { $entity = $this->changeFormData($field, $formDatas, $entity); } } $entity->updated_at = time(); return $this->save_process($entity); } //Index관련 public function setIndexWordFilter(string $word) { } public function setIndexDateFilterTrit($start, $end) { $this->where("created_at >=", $start); $this->where("created_at <=", $end); } public function setIndexOrderBy($field, $order = "ASC") { $this->orderBy($field, $order); } }