dbmsv4/app/Services/Customer/ClientService.php
2026-02-24 11:05:05 +09:00

103 lines
3.4 KiB
PHP

<?php
namespace App\Services\Customer;
use RuntimeException;
use App\Entities\CommonEntity;
use App\Forms\Customer\ClientForm;
use App\Models\Customer\ClientModel;
use App\Helpers\Customer\ClientHelper;
use App\Entities\Customer\ClientEntity;
class ClientService extends CustomerService
{
protected string $formClass = ClientForm::class;
protected string $helperClass = ClientHelper::class;
public function __construct(ClientModel $model)
{
parent::__construct($model);
$this->addClassPaths('Client');
}
public function getEntityClass(): string
{
return ClientEntity::class;
}
//기본 기능부분
protected function getEntity_process(mixed $entity): ClientEntity
{
return $entity;
}
//List 검색용
//FormFilter 조건절 처리
//검색어조건절처리
//OrderBy 처리
public function setOrderBy(mixed $field = null, mixed $value = null): void
{
$this->model->orderBy("site ASC,name ASC");
parent::setOrderBy($field, $value);
}
protected function validation_fieldhook(string $field, $value, array $formDatas): array
{
switch ($field) {
case 'role':
if (is_string($value)) {
$value = ($value === '') ? [] : explode(DEFAULTS["DELIMITER_COMMA"], $value);
} elseif (!is_array($value)) {
$value = [];
}
$value = array_values(array_filter(array_map(
fn($v) => trim((string) ($v ?? ''), " \t\n\r\0\x0B\""),
$value
)));
$formDatas[$field] = $value;
break;
default:
$formDatas = parent::validation_fieldhook($field, $value, $formDatas);
break;
}
return $formDatas;
}
protected function modify_process_fieldhook(array $formDatas): array
{
// 1) DB 컬럼 아닌 값 제거
unset($formDatas['confirmpassword']);
// 2) role은 무조건 문자열로
if (array_key_exists('role', $formDatas)) {
$arr = is_array($formDatas['role'])
? $formDatas['role']
: explode(',', (string) $formDatas['role']);
$arr = array_values(array_filter(array_map('trim', $arr)));
sort($arr);
$formDatas['role'] = implode(',', $arr);
}
// 3) passwd는 빈 값이면 업데이트 제외 (원하면)
if (array_key_exists('passwd', $formDatas) && $formDatas['passwd'] === '') {
unset($formDatas['passwd']);
}
return $formDatas;
}
public function history(string|int $uid, string $history): CommonEntity
{
return $this->dbTransaction(function () use ($uid, $history) {
$entity = $this->getEntity($uid);
if (!$entity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid}에 해당하는 정보을 찾을수 없습니다.");
}
$formDatas['user_uid'] = (int) $this->getAuthContext()->getUID();
$formDatas['history'] = $history;
// 검증 통과 후 엔티티 반영
$entity->fill($formDatas);
if (!$entity->hasChanged()) {
return $entity;
}
return $this->save_process($entity);
}, __FUNCTION__);
}
}