From 69e9ac929510dbf5783614c3cea8bd34dd4da658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0git=20config=20git=20config=20-?= =?UTF-8?q?-helpgit=20config=20--global=20user=2Ename=20=EC=B5=9C=EC=A4=80?= =?UTF-8?q?=ED=9D=A0?= Date: Sun, 23 Jul 2023 10:31:52 +0900 Subject: [PATCH] servermgrv2 init... --- app/Config/Constants.php | 3 +- app/Config/Filters.php | 3 +- app/Config/Routes.php | 40 ++++++------ app/Controllers/Admin/BoardController.php | 15 ++--- app/Controllers/Admin/LoggerController.php | 26 ++++---- app/Controllers/BaseController.php | 22 +++---- app/Helpers/Admin/BoardConfig_helper.php | 2 +- app/Helpers/Admin/Board_helper.php | 6 +- app/Helpers/Admin/Logger_helper.php | 2 +- app/Helpers/Admin/UserSNS_helper.php | 2 +- app/Helpers/Admin/User_helper.php | 2 +- app/Models/BaseHierarchyModel.php | 51 ++++++++++++++++ app/Models/BaseModel.php | 67 ++++++++++---------- app/Models/BoardConfigModel.php | 60 ++++++++---------- app/Models/BoardModel.php | 71 ++++++++-------------- app/Models/LoggerModel.php | 45 ++++++-------- app/Models/UserModel.php | 52 +++++++--------- app/Models/UserSNSModel.php | 51 +++++++--------- tests/OOPExampleTest.php | 31 ++++++++++ 19 files changed, 286 insertions(+), 265 deletions(-) create mode 100644 app/Models/BaseHierarchyModel.php create mode 100644 tests/OOPExampleTest.php diff --git a/app/Config/Constants.php b/app/Config/Constants.php index eea5e28..37964c0 100644 --- a/app/Config/Constants.php +++ b/app/Config/Constants.php @@ -182,7 +182,7 @@ foreach (PATHS as $key => $path) { //아이콘 및 Sound관련 define('ICONS', [ 'NEW' => '', - 'REPLY' => '', + 'REPLY' => '', 'DELETE' => '', 'RELOAD' => '', 'SETTING' => '', @@ -199,6 +199,7 @@ define('DEFAULTS', [ 'STATUS' => getenv('default.status') ?: "use", 'EMPTY' => getenv('default.empty') ?: "", 'PERPAGE' => getenv('default.perpage') ?: 20, + 'HIERARCHY_GRPDEPTH' => getenv('default. hierarchy_grpdepth') ?: 20, ]); //API Adapter초기갑 정의 diff --git a/app/Config/Filters.php b/app/Config/Filters.php index 3bcb14d..0eb81de 100644 --- a/app/Config/Filters.php +++ b/app/Config/Filters.php @@ -8,6 +8,7 @@ use CodeIgniter\Filters\DebugToolbar; use CodeIgniter\Filters\Honeypot; use CodeIgniter\Filters\InvalidChars; use CodeIgniter\Filters\SecureHeaders; +use App\Filters\AuthFilter; class Filters extends BaseConfig { @@ -21,7 +22,7 @@ class Filters extends BaseConfig 'honeypot' => Honeypot::class, 'invalidchars' => InvalidChars::class, 'secureheaders' => SecureHeaders::class, - 'authFilter' => \App\Filters\AuthFilter::class, + 'authFilter' => AuthFilter::class, ]; /** diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 3d09778..20ce5db 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -50,55 +50,55 @@ $routes->group('admin', ['namespace' => 'App\Controllers\Admin', 'filter' => 'au $routes->group('user', static function ($routes) { $routes->get('', 'UserController::index'); $routes->get('excel', 'UserController::excel'); - $routes->get('insert', 'UserController::insert_form', ['filter' => 'authFilter:master,director']); - $routes->post('insert', 'UserController::insert', ['filter' => 'authFilter:master,director']); + $routes->get('insert', 'UserController::insert_form'); + $routes->post('insert', 'UserController::insert'); $routes->get('update/(:uuid)', 'UserController::update_form/$1'); $routes->post('update/(:uuid)', 'UserController::update/$1'); $routes->get('view/(:uuid)', 'UserController::view/$1'); - $routes->get('delete/(:uuid)', 'UserController::delete/$1', ['filter' => 'authFilter:master,director']); - $routes->get('toggle/(:uuid)/(:hash)', 'UserController::toggle/$1/$2', ['filter' => 'authFilter:master,director']); - $routes->post('batchjob', 'UserController::batchjob', ['filter' => 'authFilter:master,director']); + $routes->get('delete/(:uuid)', 'UserController::delete/$1', ['filter' => 'authFilter:master']); + $routes->get('toggle/(:uuid)/(:hash)', 'UserController::toggle/$1/$2'); + $routes->post('batchjob', 'UserController::batchjob'); }); $routes->group('usersns', static function ($routes) { $routes->get('', 'UserSNSController::index'); $routes->get('excel', 'UserSNSController::excel'); $routes->get('view/(:num)', 'UserSNSController::view/$1'); - $routes->get('delete/(:num)', 'UserSNSController::delete/$1', ['filter' => 'authFilter:master,director']); - $routes->get('toggle/(:num)/(:hash)', 'UserSNSController::toggle/$1/$2', ['filter' => 'authFilter:master,director']); + $routes->get('delete/(:num)', 'UserSNSController::delete/$1'); + $routes->get('toggle/(:num)/(:hash)', 'UserSNSController::toggle/$1/$2'); }); - $routes->group('logger', static function ($routes) { + $routes->group('logger', ['filter' => 'authFilter:master,director,cloudflare,manager'], static function ($routes) { $routes->get('', 'LoggerController::index'); $routes->get('excel', 'LoggerController::excel'); $routes->get('view/(:num)', 'LoggerController::view/$1'); $routes->get('delete/(:num)', 'LoggerController::delete/$1', ['filter' => 'authFilter:master']); - $routes->get('toggle/(:num)/(:hash)', 'LoggerController::toggle/$1/$2', ['filter' => 'authFilter:master,director']); - $routes->post('batchjob', 'LoggerController::batchjob', ['filter' => 'authFilter:master']); + $routes->get('toggle/(:num)/(:hash)', 'LoggerController::toggle/$1/$2'); + $routes->post('batchjob', 'LoggerController::batchjob'); }); $routes->group('boardconfig', static function ($routes) { $routes->get('', 'BoardConfigController::index'); $routes->get('excel', 'BoardConfigController::excel/$1'); - $routes->get('insert', 'BoardConfigController::insert_form', ['filter' => 'authFilter:master,director']); - $routes->post('insert', 'BoardConfigController::insert', ['filter' => 'authFilter:master,director']); + $routes->get('insert', 'BoardConfigController::insert_form'); + $routes->post('insert', 'BoardConfigController::insert'); $routes->get('update/(:uuid)', 'BoardConfigController::update_form/$1'); $routes->post('update/(:uuid)', 'BoardConfigController::update/$1'); $routes->get('view/(:uuid)', 'BoardConfigController::view/$1'); - $routes->get('delete/(:uuid)', 'BoardConfigController::delete/$1', ['filter' => 'authFilter:master,director']); - $routes->get('toggle/(:uuid)/(:hash)', 'BoardConfigController::toggle/$1/$2', ['filter' => 'authFilter:master,director']); - $routes->post('batchjob', 'BoardConfigController::batchjob', ['filter' => 'authFilter:master,director']); + $routes->get('delete/(:uuid)', 'BoardConfigController::delete/$1', ['filter' => 'authFilter:master']); + $routes->get('toggle/(:uuid)/(:hash)', 'BoardConfigController::toggle/$1/$2'); + $routes->post('batchjob', 'BoardConfigController::batchjob'); }); $routes->group('board', static function ($routes) { $routes->get('', 'BoardController::index'); $routes->get('excel', 'BoardController::excel/$1'); - $routes->get('insert', 'BoardController::insert_form', ['filter' => 'authFilter:master,director']); - $routes->post('insert', 'BoardController::insert', ['filter' => 'authFilter:master,director']); + $routes->get('insert', 'BoardController::insert_form'); + $routes->post('insert', 'BoardController::insert'); $routes->get('update/(:num)', 'BoardController::update_form/$1'); $routes->post('update/(:num)', 'BoardController::update/$1'); $routes->get('view/(:num)', 'BoardController::view/$1'); $routes->get('reply/(:num)', 'BoardController::reply_form/$1'); $routes->post('reply/(:num)', 'BoardController::reply/$1'); - $routes->get('delete/(:num)', 'BoardController::delete/$1', ['filter' => 'authFilter:master,director']); - $routes->get('toggle/(:num)/(:hash)', 'BoardController::toggle/$1/$2', ['filter' => 'authFilter:master,director']); - $routes->post('batchjob', 'BoardController::batchjob', ['filter' => 'authFilter:master,director']); + $routes->get('delete/(:num)', 'BoardController::delete/$1', ['filter' => 'authFilter:master']); + $routes->get('toggle/(:num)/(:hash)', 'BoardController::toggle/$1/$2'); + $routes->post('batchjob', 'BoardController::batchjob'); }); }); /* diff --git a/app/Controllers/Admin/BoardController.php b/app/Controllers/Admin/BoardController.php index cd933fa..d6519a2 100644 --- a/app/Controllers/Admin/BoardController.php +++ b/app/Controllers/Admin/BoardController.php @@ -78,15 +78,13 @@ class BoardController extends \App\Controllers\Admin\AdminController { switch ($field) { case 'board_config_uid': - if (is_null($this->_board_config_uids)) { - //모든 필요한 FormOption등 조기화작업 필요 - $this->_board_config_uids = $this->getBoardConfigModel()->getFieldFormOptions(['status' => 'use']); - } - return $this->_board_config_uids; + $options = $this->_board_config_uids = $this->_board_config_uids ?: $this->getBoardConfigModel()->getFieldFormOptions(['status' => 'use']); + break; default: - return parent::getFieldFormOption($field); + $options = parent::getFieldFormOption($field); break; } + return $options; } //Field별 Form Datas 처리용 @@ -104,9 +102,8 @@ class BoardController extends \App\Controllers\Admin\AdminController ////Action 모음 //Insert관련 - protected function insert_process() + final protected function insert_process() { - // upload처리 return parent::insert_process(); } final public function insert() @@ -119,7 +116,7 @@ class BoardController extends \App\Controllers\Admin\AdminController return $this->update_procedure($uid); } //Reply 관련 - final public function reply(int $uid) + final public function reply($uid) { return $this->reply_procedure($uid); } diff --git a/app/Controllers/Admin/LoggerController.php b/app/Controllers/Admin/LoggerController.php index 7151fb1..425804d 100644 --- a/app/Controllers/Admin/LoggerController.php +++ b/app/Controllers/Admin/LoggerController.php @@ -51,20 +51,20 @@ class LoggerController extends \App\Controllers\Admin\AdminController ////Action 모음 //Insert관련 - final public function insert() - { - return $this->insert_procedure(); - } + // final public function insert() + // { + // return $this->insert_procedure(); + // } //Update관련 - final public function update($uid) - { - return $this->update_procedure($uid); - } - //Toggle관련 - final public function toggle($uid, string $field) - { - return $this->toggle_procedure($uid, $field); - } + // final public function update($uid) + // { + // return $this->update_procedure($uid); + // } + // //Toggle관련 + // final public function toggle($uid, string $field) + // { + // return $this->toggle_procedure($uid, $field); + // } //Batchjob 관련 // final public function batchjob() // { diff --git a/app/Controllers/BaseController.php b/app/Controllers/BaseController.php index 2dbb43a..3c88512 100644 --- a/app/Controllers/BaseController.php +++ b/app/Controllers/BaseController.php @@ -88,20 +88,16 @@ abstract class BaseController extends Controller { switch ($field) { case 'user_uid': - if (is_null($this->_user_uids)) { - //모든 필요한 FormOption등 조기화작업 필요 - $this->_user_uids = $this->getUserModel()->getFieldFormOptions(['status' => 'use']); - } - return $this->_user_uids; + $options = $this->_user_uids = $this->_user_uids ?: $this->getUserModel()->getFieldFormOptions(['status' => 'use']); break; default: - $formOptionDatas = lang($this->_className . '.' . strtoupper($field)); - if (!is_array($formOptionDatas)) { - throw new \Exception("{$this->_className}에서 Field:{$field}의 FormOptionData가 array가 아닙니다.\n" . var_export($formOptionDatas, true)); - } - return $formOptionDatas; + $options = lang($this->_className . '.' . strtoupper($field)); break; } + if (!is_array($options)) { + throw new \Exception(__FUNCTION__ . "에서 {$this->_className}의 Field:{$field}의 FormOptionData가 array가 아닙니다.\n" . var_export($options, true)); + } + return $options; } //Field별 Form Option용 final protected function getFieldFormOptions(array $fields): array @@ -121,7 +117,7 @@ abstract class BaseController extends Controller $tempRules = $this->_model->getValidationRules(['only' => $fields]); foreach ($fields as $field) { if (!is_string($field)) { - throw new \Exception(__FUNCTION__ . "에서 {$field}가 string 아닙니다.\n" . var_export($fieldRules, true)); + throw new \Exception(__FUNCTION__ . "에서 {$this->_className}의 Field:{$field}가 string 아닙니다.\n" . var_export($fieldRules, true)); } if (array_key_exists($field, $fieldRules)) { $tempRules[$field] = $fieldRules[$field]; @@ -332,7 +328,7 @@ abstract class BaseController extends Controller } protected function reply_form_process($entity) { - return $entity; + return $this->update_form_process($entity); } final public function reply_form($uid) { @@ -340,7 +336,7 @@ abstract class BaseController extends Controller $entity = $this->_model->getEntity($uid); $this->reply_init(); $this->reply_form_init(); - $this->_viewDatas['entity'] = $this->update_form_process($entity); + $this->_viewDatas['entity'] = $this->reply_form_process($entity); return view($this->_viewPath . '/reply', $this->_viewDatas); } catch (\Exception $e) { return alert_CommonHelper($e->getMessage(), 'back'); diff --git a/app/Helpers/Admin/BoardConfig_helper.php b/app/Helpers/Admin/BoardConfig_helper.php index c3610f1..5278374 100644 --- a/app/Helpers/Admin/BoardConfig_helper.php +++ b/app/Helpers/Admin/BoardConfig_helper.php @@ -107,7 +107,7 @@ function getFieldIndex_Row_BoardConfigHelper($field, array $row, array $fieldFil { switch ($field) { case 'name': - return anchor(current_url() . '/view/' . $row['uid'], $row[$field], ["target" => "_self"]); + return anchor(current_url() . '/view/' . $row['uid'], $row[$field], [...$attributes, "target" => "_self"]); break; case 'isaccess': case 'isread': diff --git a/app/Helpers/Admin/Board_helper.php b/app/Helpers/Admin/Board_helper.php index ec41ee4..1c74bac 100644 --- a/app/Helpers/Admin/Board_helper.php +++ b/app/Helpers/Admin/Board_helper.php @@ -81,9 +81,9 @@ function getFieldIndex_Row_BoardHelper($field, array $row, array $fieldFilters, case 'title': return sprintf( '
%s  %s
', - $row['grpdepth'] * 30, - anchor(current_url() . '/view/' . $row['uid'], $row[$field], ["target" => "_self"]), - anchor(current_url() . '/reply/' . $row['uid'], ICONS['REPLY'], ["target" => "_self"]) + $row['grpdepth'] * DEFAULTS['HIERARCHY_GRPDEPTH'], + anchor(current_url() . '/view/' . $row['uid'], $row[$field], [...$attributes, "target" => "_self"]), + anchor(current_url() . '/reply/' . $row['uid'], ICONS['REPLY'], [...$attributes, "target" => "_self"]) ); break; case 'updated_at': diff --git a/app/Helpers/Admin/Logger_helper.php b/app/Helpers/Admin/Logger_helper.php index 8de3288..2f0dcaa 100644 --- a/app/Helpers/Admin/Logger_helper.php +++ b/app/Helpers/Admin/Logger_helper.php @@ -68,7 +68,7 @@ function getFieldIndex_Row_LoggerHelper($field, array $row, array $fieldFilters, { switch ($field) { case 'title': - return anchor(current_url() . '/view/' . $row['uid'], $row[$field], ["target" => "_self"]); + return anchor(current_url() . '/view/' . $row['uid'], $row[$field], [...$attributes, "target" => "_self"]); break; case 'user_uid': case 'status': diff --git a/app/Helpers/Admin/UserSNS_helper.php b/app/Helpers/Admin/UserSNS_helper.php index a462692..37353c9 100644 --- a/app/Helpers/Admin/UserSNS_helper.php +++ b/app/Helpers/Admin/UserSNS_helper.php @@ -65,7 +65,7 @@ function getFieldIndex_Row_UserSNSHelper($field, array $row, array $fieldFilters { switch ($field) { case 'name': - return anchor(current_url() . '/view/' . $row['uid'], $row[$field], ["target" => "_self"]); + return anchor(current_url() . '/view/' . $row['uid'], $row[$field], [...$attributes, "target" => "_self"]); break; case 'updated_at': case 'created_at': diff --git a/app/Helpers/Admin/User_helper.php b/app/Helpers/Admin/User_helper.php index 22699c8..e460e45 100644 --- a/app/Helpers/Admin/User_helper.php +++ b/app/Helpers/Admin/User_helper.php @@ -69,7 +69,7 @@ function getFieldIndex_Row_UserHelper($field, array $row, array $fieldFilters, $ { switch ($field) { case 'id': - return anchor(current_url() . '/view/' . $row['uid'], $row[$field], ["target" => "_self"]); + return anchor(current_url() . '/view/' . $row['uid'], $row[$field], [...$attributes, "target" => "_self"]); break; case 'updated_at': case 'created_at': diff --git a/app/Models/BaseHierarchyModel.php b/app/Models/BaseHierarchyModel.php new file mode 100644 index 0000000..8975c63 --- /dev/null +++ b/app/Models/BaseHierarchyModel.php @@ -0,0 +1,51 @@ +allowedFields = [...$this->allowedFields, 'grpno', 'grporder', 'grpdepth']; + $this->validationRules = [ + ...$this->validationRules, + 'grpno' => 'if_exist|numeric', + 'grporder' => 'if_exist|numeric', + 'grpdepth' => 'if_exist|numeric', + ]; + } + abstract public function getEntity($uid): BaseEntity; + abstract public function getEntitys($where): array; + abstract function reply($parent_entity, array $formDatas): BaseEntity; + + final protected function create_process($entity, array $formDatas) + { + $entity = parent::create_process($entity, $formDatas); + //생성시는 grpno가 primarykey와 같음 + $this->builder()->set('grpno', $entity->getPrimaryKey()); + $this->builder()->where($this->primaryKey, $entity->getPrimaryKey()); + $this->builder()->update(); + return $entity; + } + + final protected function reply_process($parent_entity, $entity, array $formDatas) + { + //부모의 그룹과 grpno가 같고, 부모의 grporder보다 1 큰것을 grporder+1을 해서 update + //escape -> false옵션 반드시 있어야함 + $this->builder()->set('grporder', 'grporder+1', false); + $this->builder()->where([ + 'grpno' => $parent_entity->grpno, + 'grporder >' => $parent_entity->grporder + ]); + $this->builder()->update(); + //reply용 설정 + $entity->grpno = $parent_entity->grpno; + $entity->grporder = $parent_entity->grporder + 1; + $entity->grpdepth = $parent_entity->grpdepth + 1; + return parent::create_process($entity, $formDatas); + } +} diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php index d73676d..bcbadd1 100644 --- a/app/Models/BaseModel.php +++ b/app/Models/BaseModel.php @@ -9,7 +9,7 @@ use App\Entities\BaseEntity; abstract class BaseModel extends Model { protected $DBGroup = 'default'; - // protected $table = 'user'; + protected $table = 'default'; protected $primaryKey = 'uid'; protected $useAutoIncrement = true; protected $insertID = 0; @@ -41,9 +41,25 @@ abstract class BaseModel extends Model protected $beforeDelete = []; protected $afterDelete = []; - abstract public function getEntityByField($field, $value): ?BaseEntity; - abstract public function getEntity($uid): ?BaseEntity; - abstract public function getFieldFormOptions(): array; + protected function __construct() + { + parent::__construct(); + $this->allowedFields = ['updated_at', 'created_at']; + $this->validationRules = [ + 'updated_at' => 'if_exist|valid_date', + 'created_at' => 'if_exist|valid_date', + ]; + } + abstract public function getEntity($uid): BaseEntity; + abstract public function getEntitys($where): array; + + final public function getFieldFormOptions($where, $options = array()): array + { + foreach ($this->getEntitys($where) as $entity) { + $options[$entity->getPrimaryKey()] = $entity->getTitle(); + } + return $options; + } final public function getUUID() { @@ -58,39 +74,15 @@ abstract class BaseModel extends Model ); } - //계층형구조구현 - final protected function create_hierarchy($entity) - { - //생성시는 grpno가 primarykey와 같음 - //escape -> false옵션 반드시 있어야함 - $this->builder()->set('grpno', $entity->getPrimaryKey(), false); - $this->builder()->where($this->primaryKey, $entity->getPrimaryKey()); - $this->builder()->update(); - return $entity; - } - final protected function reply_hierarchy($entity, $parent_entity) - { - //부모의 그룹과 grpno가 같고, 부모의 grporder보다 1 큰것을 grporder+1을 해서 update - //escape -> false옵션 반드시 있어야함 - $this->builder()->set('grporder', 'grporder+1', false); - $this->builder()->where([ - 'grpno' => $parent_entity->grpno, - 'grporder >' => $parent_entity->grporder - ]); - $this->builder()->update(); - //reply용 설정 - $entity->grpno = $parent_entity->grpno; - $entity->grporder = $parent_entity->grporder + 1; - $entity->grpdepth = $parent_entity->grpdepth + 1; - return $entity; - } - + //create , modify 직전 작업용 작업 protected function changeFormData($field, array $formDatas, $entity) { switch ($field) { case $this->primaryKey: + //primaryKey가 자동입력이 아니면 if (!$this->useAutoIncrement) { - $entity->$field = $this->getUUID(); + $pk = $this->primaryKey; + $entity->$pk = $this->getUUID(); } break; case 'user_uid': @@ -128,19 +120,24 @@ abstract class BaseModel extends Model Log::add("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; } - final protected function create_process($entity, array $formDatas) + protected function create_process($entity, array $formDatas) { foreach ($this->allowedFields as $field) { $entity = $this->changeFormData($field, $formDatas, $entity); } return $this->save_process($entity); } - final protected function modify_process($entity, array $formDatas) + protected function modify_process($entity, array $formDatas) { foreach ($this->allowedFields as $field) { if ($field != $this->primaryKey) { @@ -158,8 +155,6 @@ abstract class BaseModel extends Model $this->builder()->set($field, "{$field}+{$cnt}", false); $this->builder()->where($this->primaryKey, $uid); $this->builder()->update(); - // echo $this->getLastQuery(); - // exit; } //Index관련 diff --git a/app/Models/BoardConfigModel.php b/app/Models/BoardConfigModel.php index b0daa4a..ed071c7 100644 --- a/app/Models/BoardConfigModel.php +++ b/app/Models/BoardConfigModel.php @@ -6,44 +6,36 @@ use App\Entities\BoardConfigEntity; class BoardConfigModel extends BaseModel { - protected $table = 'tw_board_config'; - protected $primaryKey = 'uid'; - protected $useAutoIncrement = false; - protected $allowedFields = ['uid', 'name', 'isaccess', 'isread', 'iswrite', 'isreply', 'isupload', 'isdownload', 'head', 'tail', 'status', 'updated_at']; - protected $validationRules = [ - 'uid' => 'required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]', - 'name' => 'required|string', - 'isaccess' => 'required', - 'isread' => 'required', - 'iswrite' => 'required', - 'isreply' => 'required', - 'isupload' => 'required', - 'isdownload' => 'required', - 'head' => 'if_exist|string', - 'tail' => 'if_exist|string', - 'status' => 'if_exist|string', - 'updated_at' => 'if_exist|valid_date', - 'created_at' => 'if_exist|valid_date', - ]; + public function __construct() + { + parent::__construct(); + $this->table = 'tw_board_config'; + $this->useAutoIncrement = false; + $this->allowedFields = [...$this->allowedFields, 'uid', 'name', 'isaccess', 'isread', 'iswrite', 'isreply', 'isupload', 'isdownload', 'head', 'tail', 'status']; + $this->validationRules = [ + ...$this->validationRules, + 'uid' => 'required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]', + 'name' => 'required|string', + 'isaccess' => 'required', + 'isread' => 'required', + 'iswrite' => 'required', + 'isreply' => 'required', + 'isupload' => 'required', + 'isdownload' => 'required', + 'head' => 'if_exist|string', + 'tail' => 'if_exist|string', + 'status' => 'if_exist|string', + ]; + } - public function getEntityByField($field, $value): ?BoardConfigEntity + public function getEntity($uid): BoardConfigEntity { - $entity = $this->asObject(BoardConfigEntity::class)->where($field, $value)->first(); - if (is_null($entity)) { - throw new \Exception("해당 데이터가 없습니다.\n {$field}->{$value}"); - } - return $entity; + $entity = $this->asObject(BoardConfigEntity::class)->where([$this->primaryKey => $uid])->first(); + return $entity ?: throw new \Exception("{$uid}의 해당 데이터가 없습니다.\n "); } - public function getEntity($uid): ?BoardConfigEntity + public function getEntitys($where): array { - return $this->getEntityByField($this->primaryKey, $uid); - } - public function getFieldFormOptions(array $wheres = array(), $temps = array()): array - { - foreach ($this->asObject(BoardConfigEntity::class)->where($wheres)->findAll() as $entity) { - $temps[$entity->getPrimaryKey()] = $entity->getTitle(); - } - return $temps; + return $this->asObject(BoardConfigEntity::class)->where($where)->findAll(); } protected function changeFormData($field, array $formDatas, $entity) diff --git a/app/Models/BoardModel.php b/app/Models/BoardModel.php index 3c52fe4..ee2178f 100644 --- a/app/Models/BoardModel.php +++ b/app/Models/BoardModel.php @@ -4,62 +4,45 @@ namespace App\Models; use App\Entities\BoardEntity; -class BoardModel extends BaseModel +class BoardModel extends BaseHierarchyModel { - protected $table = 'tw_board'; - protected $primaryKey = 'uid'; - protected $useAutoIncrement = true; - protected $allowedFields = ['grpno', 'grporder', 'grpdepth', 'board_config_uid', 'user_uid', 'title', 'content', 'passwd', 'view_cnt', 'status', 'updated_at']; - protected $validationRules = [ - 'grpno' => 'if_exist|numeric', - 'grporder' => 'if_exist|numeric', - 'grpdepth' => 'if_exist|numeric', - 'board_config_uid' => 'required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]', - 'user_uid' => 'if_exist|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]', - 'title' => 'required|string', - 'content' => 'required|string', - 'passwd' => 'if_exist|trim|string', - 'view_cnt' => 'if_exist|numeric', - 'status' => 'if_exist|string', - 'updated_at' => 'if_exist|valid_date', - 'created_at' => 'if_exist|valid_date', - ]; - - public function getEntityByField($field, $value): ?BoardEntity + public function __construct() { - $entity = $this->asObject(BoardEntity::class)->where($field, $value)->first(); - if (is_null($entity)) { - throw new \Exception("해당 데이터가 없습니다.\n {$field}->{$value}"); - } - return $entity; - } - public function getEntity($uid): ?BoardEntity - { - return $this->getEntityByField($this->primaryKey, $uid); - } - public function getFieldFormOptions(array $wheres = array(), $temps = array()): array - { - foreach ($this->asObject(BoardEntity::class)->where($wheres)->findAll() as $entity) { - $temps[$entity->getPrimaryKey()] = $entity->getTitle(); - } - return $temps; + parent::__construct(); + $this->table = 'tw_board'; + $this->allowedFields = [...$this->allowedFields, 'board_config_uid', 'user_uid', 'title', 'content', 'passwd', 'view_cnt', 'status']; + $this->validationRules = [ + ...$this->validationRules, + 'board_config_uid' => 'required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]', + 'user_uid' => 'if_exist|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]', + 'title' => 'required|string', + 'content' => 'required|string', + 'passwd' => 'if_exist|trim|string', + 'view_cnt' => 'if_exist|numeric', + 'status' => 'if_exist|string', + ]; } + public function getEntity($uid): BoardEntity + { + $entity = $this->asObject(BoardEntity::class)->where([$this->primaryKey => $uid])->first(); + return $entity ?: throw new \Exception("{$uid}의 해당 데이터가 없습니다.\n "); + } + public function getEntitys($where): array + { + return $this->asObject(BoardEntity::class)->where($where)->findAll(); + } public function create(array $formDatas): BoardEntity { - //계층형 Create - $entity = $this->create_process(new BoardEntity(), $formDatas); - return $this->create_hierarchy($entity); + return $this->create_process(new BoardEntity(), $formDatas); } public function modify(BoardEntity $entity, array $formDatas): BoardEntity { return $this->modify_process($entity, $formDatas); } - public function reply(BoardEntity $parent_entity, array $formDatas): BoardEntity + public function reply($parent_entity, array $formDatas): BoardEntity { - //계층형 Reply - $entity = $this->reply_hierarchy(new BoardEntity(), $parent_entity); - return $this->create_process($entity, $formDatas); + return $this->reply_process($parent_entity, new BoardEntity(), $formDatas); } //Index관련 diff --git a/app/Models/LoggerModel.php b/app/Models/LoggerModel.php index 04c35b9..39e9673 100644 --- a/app/Models/LoggerModel.php +++ b/app/Models/LoggerModel.php @@ -6,37 +6,28 @@ use App\Entities\LoggerEntity; class LoggerModel extends BaseModel { - protected $table = 'tw_logger'; - protected $primaryKey = 'uid'; - // protected $useAutoIncrement = true; - protected $allowedFields = ['user_uid', 'title', 'content', 'status', 'updated_at']; - protected $validationRules = [ - 'user_uid' => 'required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]', - 'title' => 'required|string', - 'content' => 'if_exist|string', - 'status' => 'required|string', - 'updated_at' => 'if_exist|valid_date', - 'created_at' => 'if_exist|valid_date', - ]; + public function __construct() + { + parent::__construct(); + $this->table = 'tw_logger'; + $this->allowedFields = [...$this->allowedFields, 'user_uid', 'title', 'content', 'status']; + $this->validationRules = [ + ...$this->validationRules, + 'user_uid' => 'required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]', + 'title' => 'required|string', + 'content' => 'if_exist|string', + 'status' => 'required|string', + ]; + } - public function getEntityByField($field, $value): ?LoggerEntity + public function getEntity($uid): LoggerEntity { - $entity = $this->asObject(LoggerEntity::class)->where($field, $value)->first(); - if (is_null($entity)) { - throw new \Exception("해당 데이터가 없습니다.\n {$field}->{$value}"); - } - return $entity; + $entity = $this->asObject(LoggerEntity::class)->where([$this->primaryKey => $uid])->first(); + return $entity ?: throw new \Exception("{$uid}의 해당 데이터가 없습니다.\n "); } - public function getEntity($uid): ?LoggerEntity + public function getEntitys($where): array { - return $this->getEntityByField($this->primaryKey, $uid); - } - public function getFieldFormOptions(array $wheres = array(), $temps = array()): array - { - foreach ($this->asObject(LoggerEntity::class)->where($wheres)->findAll() as $entity) { - $temps[$entity->getPrimaryKey()] = $entity->getTitle(); - } - return $temps; + return $this->asObject(LoggerEntity::class)->where($where)->findAll(); } public function create(array $formDatas): LoggerEntity { diff --git a/app/Models/UserModel.php b/app/Models/UserModel.php index b9b6b34..acad26e 100644 --- a/app/Models/UserModel.php +++ b/app/Models/UserModel.php @@ -6,40 +6,32 @@ use App\Entities\UserEntity; class UserModel extends BaseModel { - protected $table = 'tw_user'; - protected $primaryKey = 'uid'; - protected $useAutoIncrement = false; - protected $allowedFields = ['uid', 'id', 'email', 'passwd', 'name', 'role', 'status', 'updated_at']; - protected $validationRules = [ - 'uid' => 'required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]', - 'id' => 'required|min_length[4]|max_length[20]', - 'passwd' => 'required|trim|min_length[4]|max_length[150]', - 'name' => 'required|min_length[2]|max_length[20]', - 'email' => 'required|valid_email', - 'role' => 'required|string', - 'status' => 'if_exist|string', - 'updated_at' => 'if_exist|valid_date', - 'created_at' => 'if_exist|valid_date', - ]; + public function __construct() + { + parent::__construct(); + $this->table = 'tw_user'; + $this->useAutoIncrement = false; + $this->allowedFields = [...$this->allowedFields, 'uid', 'id', 'email', 'passwd', 'name', 'role', 'status']; + $this->validationRules = [ + ...$this->validationRules, + 'uid' => 'required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]', + 'id' => 'required|min_length[4]|max_length[20]', + 'passwd' => 'required|trim|min_length[4]|max_length[150]', + 'name' => 'required|min_length[2]|max_length[20]', + 'email' => 'required|valid_email', + 'role' => 'required|string', + 'status' => 'if_exist|string', + ]; + } - public function getEntityByField($field, $value): ?UserEntity + public function getEntity($uid): UserEntity { - $entity = $this->asObject(UserEntity::class)->where($field, $value)->first(); - if (is_null($entity)) { - throw new \Exception("해당 데이터가 없습니다.\n {$field}->{$value}"); - } - return $entity; + $entity = $this->asObject(UserEntity::class)->where([$this->primaryKey => $uid])->first(); + return $entity ?: throw new \Exception("{$uid}의 해당 데이터가 없습니다.\n "); } - public function getEntity($uid): ?UserEntity + public function getEntitys($where): array { - return $this->getEntityByField($this->primaryKey, $uid); - } - public function getFieldFormOptions(array $wheres = array(), $temps = array()): array - { - foreach ($this->asObject(UserEntity::class)->where($wheres)->findAll() as $entity) { - $temps[$entity->getPrimaryKey()] = $entity->getTitle(); - } - return $temps; + return $this->asObject(UserEntity::class)->where($where)->findAll(); } public function create(array $formDatas): UserEntity { diff --git a/app/Models/UserSNSModel.php b/app/Models/UserSNSModel.php index f82a16e..fa2923b 100644 --- a/app/Models/UserSNSModel.php +++ b/app/Models/UserSNSModel.php @@ -6,40 +6,31 @@ use App\Entities\UserSNSEntity; class UserSNSModel extends BaseModel { - protected $table = 'tw_user_sns'; - protected $primaryKey = 'uid'; - // protected $useAutoIncrement = true; - protected $allowedFields = ['user_uid', 'site', 'id', 'name', 'email', 'detail', 'status', 'updated_at', 'created_at']; - protected $validationRules = [ - 'user_uid' => 'if_exist|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]', - 'site' => 'required|string', - 'id' => 'required|string', - 'name' => 'required|string', - 'email' => 'required|valid_email', - 'detail' => 'if_exist|string', - 'status' => 'required|string', - 'updated_at' => 'if_exist|valid_date', - 'created_at' => 'if_exist|valid_date', - ]; + public function __construct() + { + parent::__construct(); + $this->table = 'tw_user_sns'; + $this->allowedFields = [...$this->allowedFields, 'user_uid', 'site', 'id', 'name', 'email', 'detail', 'status']; + $this->validationRules = [ + ...$this->validationRules, + 'user_uid' => 'if_exist|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]', + 'site' => 'required|string', + 'id' => 'required|string', + 'name' => 'required|string', + 'email' => 'required|valid_email', + 'detail' => 'if_exist|string', + 'status' => 'required|string', + ]; + } - public function getEntityByField($field, $value): ?UserSNSEntity + public function getEntity($uid): UserSNSEntity { - $entity = $this->asObject(UserSNSEntity::class)->where($field, $value)->first(); - if (is_null($entity)) { - throw new \Exception("해당 데이터가 없습니다.\n {$field}->{$value}"); - } - return $entity; + $entity = $this->asObject(UserSNSEntity::class)->where([$this->primaryKey => $uid])->first(); + return $entity ?: throw new \Exception("{$uid}의 해당 데이터가 없습니다.\n "); } - public function getEntity($uid): ?UserSNSEntity + public function getEntitys($where): array { - return $this->getEntityByField($this->primaryKey, $uid); - } - public function getFieldFormOptions(array $wheres = array(), $temps = array()): array - { - foreach ($this->asObject(UserSNSEntity::class)->where($wheres)->findAll() as $entity) { - $temps[$entity->getPrimaryKey()] = $entity->getTitle(); - } - return $temps; + return $this->asObject(UserSNSEntity::class)->where($where)->findAll(); } public function create(array $formDatas): UserSNSEntity diff --git a/tests/OOPExampleTest.php b/tests/OOPExampleTest.php new file mode 100644 index 0000000..1c0100c --- /dev/null +++ b/tests/OOPExampleTest.php @@ -0,0 +1,31 @@ +doFoo($arrayOfFoo, 'Success'); +// Success + +echo $myBar->doFoo($notArrayOfFoo, 'Success'); +// Uncaught TypeError: Argument 2 passed to Bar::{closure}() must be an instance of Foo, instance of NotFoo given... \ No newline at end of file