From 514a0a5d442848519b6ec858e403e6410a482872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Fri, 6 Sep 2024 18:00:03 +0900 Subject: [PATCH] Automation init...1 --- app/Controllers/CLI/Crawler.php | 1 - app/Libraries/Mangboard/UserLibrary.php | 2 +- app/Models/CommonModel.php | 32 +++++++--------------- app/Models/Mangboard/BoardModel.php | 27 ++++++++++++------ app/Models/Mangboard/GalleryBoardModel.php | 4 +-- app/Models/Mangboard/UserModel.php | 8 +++--- 6 files changed, 35 insertions(+), 39 deletions(-) diff --git a/app/Controllers/CLI/Crawler.php b/app/Controllers/CLI/Crawler.php index f02a261..f7a5e5a 100644 --- a/app/Controllers/CLI/Crawler.php +++ b/app/Controllers/CLI/Crawler.php @@ -50,7 +50,6 @@ class Crawler extends BaseController //미디어관련정보 entity에 넣기 $entity = new BoardEntity(); $entity->setTitle($nickname); - $entity->setText($nickname); $entity->setContent(is_array($mediaTags) ? implode("\n", $mediaTags) : $mediaTags); //망보드에 넣기 diff --git a/app/Libraries/Mangboard/UserLibrary.php b/app/Libraries/Mangboard/UserLibrary.php index 812c8f6..ed443b1 100644 --- a/app/Libraries/Mangboard/UserLibrary.php +++ b/app/Libraries/Mangboard/UserLibrary.php @@ -87,7 +87,7 @@ class UserLibrary extends CommonLibrary } return $this->checkLevel($entity); } - public function setLevel() + public function setLevel(): void { foreach ($this->getModel()->getEntitys() as $entity) { $entity = $this->checkLevel($entity); diff --git a/app/Models/CommonModel.php b/app/Models/CommonModel.php index c4bcac7..2dd6771 100644 --- a/app/Models/CommonModel.php +++ b/app/Models/CommonModel.php @@ -130,11 +130,11 @@ abstract class CommonModel extends Model } //create , modify 직전 작업용 작업 - protected function setEntityData($entity, string $field, $value = null): mixed + protected function setEntityData($entity, $field): mixed { switch ($field) { case $this->getPKField(): - if ($value === null) { + if ($$entity->$field === null) { $randomBytes = bin2hex(random_bytes(32)); $entity->$field = sprintf( '%08s-%04s-%04x-%04x-%12s', @@ -144,18 +144,13 @@ abstract class CommonModel extends Model substr($randomBytes, 16, 4), substr($randomBytes, 20, 12) ); - } else { - $entity->$field = $value; } break; case "passwd": - $entity->$field = password_hash($value, PASSWORD_DEFAULT); + $entity->$field = password_hash($entity->$field, PASSWORD_DEFAULT); break; case "content": - $entity->$field = htmlspecialchars($value, ENT_QUOTES); //htmlentities($value); - break; - default: - $entity->$field = $value; + $entity->$field = htmlentities($entity->$field, ENT_QUOTES); break; } return $entity; @@ -163,6 +158,9 @@ abstract class CommonModel extends Model private function save_process($entity): mixed { + foreach ($this->getFields() as $field) { + $entity = $this->setEntityData($entity, $field); + } if ($entity->hasChanged()) { if (!$this->save($entity)) { log_message("error", __FUNCTION__ . "에서 호출:" . $this->getLastQuery()); @@ -174,13 +172,8 @@ abstract class CommonModel extends Model } return $entity; } - final protected function create_process($entity, array $formDatas = []): mixed + final protected function create_process($entity): mixed { - foreach ($this->getFields() as $field) { - if (array_key_exists($field, $formDatas) && $formDatas[$field] !== $entity->$field) { - $entity = $this->setEntityData($entity, $field, $formDatas[$field]); - } - } //primaryKey가 수동입력이면 $pkField = $this->getPKField(); if (!$this->useAutoIncrement && $entity->$pkField === null) { @@ -191,17 +184,12 @@ abstract class CommonModel extends Model $entity = $this->save_process($entity); //primaryKey가 자동입력이면 if ($this->useAutoIncrement) { - $entity = $this->setEntityData($entity, $this->getPKField(), $this->getInsertID()); + $entity->$pkField = $this->getInsertID(); } return $entity; } - final protected function modify_process($entity, array $formDatas = []): mixed + final protected function modify_process($entity): mixed { - foreach ($this->getFields() as $field) { - if (array_key_exists($field, $formDatas) && $formDatas[$field] !== $entity->$field) { - $entity = $this->setEntityData($entity, $field, $formDatas[$field]); - } - } return $this->save_process($entity); } } diff --git a/app/Models/Mangboard/BoardModel.php b/app/Models/Mangboard/BoardModel.php index 2ef7ee6..be04be7 100644 --- a/app/Models/Mangboard/BoardModel.php +++ b/app/Models/Mangboard/BoardModel.php @@ -12,7 +12,7 @@ abstract class BoardModel extends CommonModel protected function __construct(array $fields = []) { - $fields = ["title", "text", "content", ...$fields]; + $fields = ["title", "is_show", "content", ...$fields]; parent::__construct($fields); } public function getTitleField(): string @@ -25,7 +25,18 @@ abstract class BoardModel extends CommonModel case 'gid': $rules[$field] = "required|numeric"; break; - case "text": + case 'is_show': + $rules[$field] = "if_exist|numeric"; + break; + case "data_type": + $rules[$field] = "if_exist|trim|in_list[html,text]"; + break; + case "image_path": + $rules[$field] = "if_exist|trim|string"; + break; + case "editor_type": + $rules[$field] = "if_exist|trim|in_list[N,S]"; + break; case "content": $rules[$field] = "required|trim|string"; break; @@ -48,19 +59,17 @@ abstract class BoardModel extends CommonModel } //create용 - public function create(BoardEntity $entity, array $formDatas = []): BoardEntity + public function create(BoardEntity $entity): BoardEntity { - $entity = $this->create_process($entity, $formDatas); + $entity = $this->create_process($entity); //GID값이 PK랑 같은 값 전달 후 Entity 수정 - $this->setFields(["gid"]); - $this->setRules($this->getFields()); $entity->setGID($entity->getPK()); - return $this->modify_process($entity, []); + return $this->modify_process($entity); } //modify용 - public function modify(BoardEntity $entity, array $formDatas = []): BoardEntity + public function modify(BoardEntity $entity): BoardEntity { - return $this->modify_process($entity, $formDatas); + return $this->modify_process($entity); } } diff --git a/app/Models/Mangboard/GalleryBoardModel.php b/app/Models/Mangboard/GalleryBoardModel.php index a32bf45..6d99e95 100644 --- a/app/Models/Mangboard/GalleryBoardModel.php +++ b/app/Models/Mangboard/GalleryBoardModel.php @@ -8,8 +8,8 @@ class GalleryBoardModel extends BoardModel { protected $table = 'mb_gallery_general'; - public function __construct(array $fields = []) + public function __construct() { - parent::__construct($fields); + parent::__construct([]); } } diff --git a/app/Models/Mangboard/UserModel.php b/app/Models/Mangboard/UserModel.php index eed049f..807cc2c 100644 --- a/app/Models/Mangboard/UserModel.php +++ b/app/Models/Mangboard/UserModel.php @@ -56,17 +56,17 @@ class UserModel extends CommonModel } //create용 - public function create(UserEntity $entity, array $formDatas = []): UserEntity + public function create(UserEntity $entity): UserEntity { - $entity = $this->create_process($entity, $formDatas); + $entity = $this->create_process($entity); //GID값이 PK랑 같은 값 전달 후 Entity 수정 $entity->setGID($entity->getPK()); return $this->modify_process($entity); } //modify용 - public function modify(UserEntity $entity, array $formDatas = []): UserEntity + public function modify(UserEntity $entity): UserEntity { - return $this->modify_process($entity, $formDatas); + return $this->modify_process($entity); } }