139 lines
6.0 KiB
PHP
139 lines
6.0 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;
|
|
// dd($formDatas);
|
|
if (!service('validation')->setRules($this->getValidationRules(__FUNCTION__))->run($formDatas)) {
|
|
throw new ValidationException(implode("\n", service('validation')->getErrors()));
|
|
}
|
|
$entity = new UserEntity($formDatas);
|
|
$result = $this->model->insert($entity);
|
|
if (!$result) {
|
|
throw new \Exception("{$entity->getTitle()} 등록 중 DB 오류가 발생하였습니다.");
|
|
}
|
|
// 💡 PK 타입에 따른 최종 Entity 반환 로직 분기
|
|
if ($this->model->useAutoIncrement) { //정수 PK인 경우
|
|
$savedEntity = $this->model->find($result);
|
|
} else { //문자열 PK인 경우
|
|
$pkValue = $entity->{$this->model->primaryKey};
|
|
$savedEntity = $this->model->find($pkValue);
|
|
}
|
|
if (!$savedEntity instanceof UserEntity) {
|
|
throw new \Exception("등록된 데이터를 찾을 수 없습니다.");
|
|
}
|
|
return $savedEntity;
|
|
}
|
|
public function modify(UserDTO $dto): UserEntity
|
|
{
|
|
// DTO 객체를 배열로 변환하여 검증기에 전달
|
|
$formDatas = (array) $dto;
|
|
// dd($formDatas);
|
|
if (!service('validation')->setRules($this->getValidationRules(__FUNCTION__))->run($formDatas)) {
|
|
throw new ValidationException(implode("\n", service('validation')->getErrors()));
|
|
}
|
|
$entity = new UserEntity($formDatas);
|
|
$result = $this->model->update($entity->getPK(), $formDatas);
|
|
if (!$result) {
|
|
throw new \Exception("{$entity->getTitle()} 등록 중 DB 오류가 발생하였습니다.");
|
|
}
|
|
// 💡 PK 타입에 따른 최종 Entity 반환 로직 분기
|
|
if ($this->model->useAutoIncrement) { //정수 PK인 경우
|
|
$savedEntity = $this->model->find($result);
|
|
} else { //문자열 PK인 경우
|
|
$pkValue = $entity->{$this->model->primaryKey};
|
|
$savedEntity = $this->model->find($pkValue);
|
|
}
|
|
if (!$savedEntity instanceof UserEntity) {
|
|
throw new \Exception("등록된 데이터를 찾을 수 없습니다.");
|
|
}
|
|
return $savedEntity;
|
|
}
|
|
// 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);
|
|
// }
|
|
}
|