trafficmonitor/app/Services/UserService.php
2025-11-06 16:51:52 +09:00

101 lines
4.3 KiB
PHP

<?php
namespace App\Services;
use App\DTOs\UserDTO;
use App\Entities\UserEntity;
use App\Forms\UserForm;
use App\Helpers\UserHelper;
use App\Models\UserModel;
use CodeIgniter\Validation\Exceptions\ValidationException;
use RuntimeException;
class UserService extends CommonService
{
public function __construct(UserModel $model)
{
parent::__construct($model);
$this->addClassPaths('User');
}
public function getFormService(): UserForm
{
if ($this->formServiceInstance === null) {
$this->formServiceInstance = new UserForm();
$this->formServiceInstance->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
'useAutoIncrement' => $this->model->useAutoIncrement(),
'class_path' => $this->getClassPaths(false)
]);
}
return $this->formServiceInstance;
}
public function getHelper(): UserHelper
{
if ($this->helperInstance === null) {
$this->helperInstance = new UserHelper();
$this->helperInstance->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
'useAutoIncrement' => $this->model->useAutoIncrement(),
'class_path' => $this->getClassPaths(false)
]);
}
return $this->helperInstance;
}
//기본 기능부분
public function create(UserDTO $dto): UserEntity
{
// DTO 객체를 배열로 변환하여 검증기에 전달
$formDatas = (array) $dto;
if (!service('validation')->setRules($this->getFormRules(__FUNCTION__))->run($formDatas)) {
// 검증 실패 시, ValidationException을 던집니다.
throw new ValidationException(implode("\n", service('validation')->getErrors()));
}
$formDatas['role'] = implode(DEFAULTS["DELIMITER_ROLE"], $formDatas['role']);
$entity = $this->model->create($formDatas);
return $entity;
}
// protected function modify_process(mixed $entity, array $formDatas): UserEntity
// {
// // die(var_export($formDatas, true));
// //암호를 입력하지 않았을시는 변경하기 않게 하기위함
// if (isset($formDatas['passwd']) && $formDatas['passwd'] == "") {
// unset($formDatas['passwd']);
// unset($formDatas['confirmpassword']);
// }
// //Role을 지정이 있을경우에만 , toggle이나 batcjhjob에서는 없을수도 있으므로
// if (isset($formDatas['role'])) {
// $formDatas['role'] = implode(DEFAULTS["DELIMITER_ROLE"], $formDatas['role']);
// }
// // die(var_export($formDatas, true));
// $entity = parent::modify_process($entity, $formDatas);
// return $entity;
// }
// //List 검색용
// //FormFilter 조건절 처리
// public function index_condition_filterField(string $field, mixed $filter_value): void
// {
// switch ($field) {
// case 'role':
// $where = "FIND_IN_SET(" . $this->model->escape($filter_value) . ", {$this->model->getTable()}.{$field}) > 0";
// //FIND_IN_SET()은 MySQL 함수이므로 CodeIgniter가 이를 일반 컬럼명으로 착각하고 escape하게 되면 오류가 발생합니다. 따라서 ->where($sql, null, false)로 명시하여 escape를 꺼줘야 정상 작동
// $this->model->where($where, null, false);
// break;
// default:
// parent::index_condition_filterField($field, $filter_value);
// break;
// }
// }
// //검색어조건절처리
// public function index_condition_filterWord(string $word): void
// {
// $this->model->orLike($this->model->getTable() . '.id', $word, 'both');
// $this->model->orLike($this->model->getTable() . "." . $this->model->getTitleField(), $word, 'both');
// $this->model->orLike($this->model->getTable() . '.email', $word, 'both');
// parent::index_condition_filterWord($word);
// }
}