table; } final public function getPKField(): string { return $this->primaryKey; } // final public function getFields(array $except_fields = []): array // { // return array_diff($this->allowedFields, $except_fields); //제외한 fields // } public function getFieldRule(string $action, string $field, array $rules): array { if (is_array($field)) { throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true)); } switch ($field) { case $this->getPKField(): //수동입력인경우 if (!$this->useAutoIncrement) { $rules[$field] = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]"; $rules[$field] .= $action == "create" ? "|is_unique[{$this->table}.{$field}]" : ""; } else { $rules[$field] = "required|numeric"; }; break; case $this->getTitleField(): $rules[$field] = "required|string"; break; case "passwd": $rules[$field] = $action == "create" ? "required" : "if_exist" . "|trim|string"; break; case "confirmpassword": $rules["confirmpassword"] = $action == "create" ? "required" : "if_exist" . "|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 "status": $rules[$field] = "if_exist|in_list[use,unuse]"; 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(string $action, array $fields, array $rules = []): array { foreach ($fields as $field) { if (is_array($field)) { throw new \Exception(__FUNCTION__ . "에서 field array 입니다.\n" . var_export($field, true)); } $rules = $this->getFieldRule($action, $field, $rules); } return $rules; } public function getFormFieldOption(string $field, array $options = []): array { switch ($field) { default: foreach ($this->getEntitys() as $entity) { $options[$entity->getPK()] = $entity->getTitle(); } break; } // dd($options); // exit; return $options; } final public function getEntity(): array|object|null { return $this->asObject($this->returnType)->first(); } final public function getEntitys(): array { return $this->asObject($this->returnType)->findAll(); } //create , modify 직전 작업용 작업 final protected function convertEntityData(string $field, array $formDatas): mixed { switch ($field) { case $this->getPKField(): //$formDatas에 전달된 값이 없는경우 if (!array_key_exists($field, $formDatas)) { $randomBytes = bin2hex(random_bytes(32)); $value = 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) ); } else { $value = $formDatas[$field]; } break; case "passwd": $value = password_hash($formDatas[$field], PASSWORD_DEFAULT); break; case "confirmpassword": $value = password_hash($formDatas[$field], PASSWORD_DEFAULT); break; case "content": $value = htmlentities($formDatas[$field], ENT_QUOTES); break; default: $value = $formDatas[$field]; break; } return $value; } private function save_process($entity): mixed { //최종 변경사항이 있으면 저장 if ($entity->hasChanged()) { if (!$this->save($entity)) { throw new \Exception(sprintf( "\n------%s SQL오류-----\n%s\n%s\n------------------------------\n", __FUNCTION__, $this->getLastQuery(), var_export($this->errors(), true) )); } } return $entity; } final protected function create_process($entity, array $formDatas): mixed { //Field에 맞는 Validation Rule 재정의 $this->setValidationRules($this->getFieldRules('create', $this->allowedFields)); //저장하기 전에 데이터 값 변경이 필요한 Field foreach (array_keys($formDatas) as $field) { $entity->$field = $this->convertEntityData($field, $formDatas); } $entity = $this->save_process($entity); //primaryKey가 자동입력이면 if ($this->useAutoIncrement) { $pkField = $this->getPKField(); $entity->$pkField = $this->getInsertID(); } log_message("debug", $this->table . "=> " . __FUNCTION__ . " 작업 완료"); return $entity; } final protected function modify_process($entity, array $formDatas): mixed { //Field에 맞는 Validation Rule 재정의 $this->setValidationRules($this->getFieldRules('modify', $this->allowedFields)); //저장하기 전에 데이터 값 변경이 필요한 Field foreach (array_keys($formDatas) as $field) { $entity->$field = $this->convertEntityData($field, $formDatas); } $this->save_process($entity); log_message("debug", $this->table . "=> " . __FUNCTION__ . " 작업 완료"); return $entity; } //List용 final public function setList_FieldFilter(string $field, int|string $value): void { $this->where($field, $value); } public function setList_WordFilter(string $word, string $field = null): void { $this->like($field ?? $this->getTitleField(), $word, 'both'); //befor , after , both } final public function setList_DateFilter(string $start, string $end, $field = "created_at"): void { if ($start !== DEFAULTS['EMPTY']) { $this->where("{$field} >= '{$start} 00:00:00'"); } if ($end !== DEFAULTS['EMPTY']) { $this->where("{$field} <= '{$end} 23:59:59'"); } } public function setList_OrderBy(string $order = "") { if ($order) { $this->orderBy($order); } } }