135 lines
4.1 KiB
PHP
135 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use CodeIgniter\HTTP\IncomingRequest;
|
|
use App\Libraries\LogCollector;
|
|
|
|
abstract class CommonService
|
|
{
|
|
private $_serviceDatas = [];
|
|
private $_model = null;
|
|
protected ?IncomingRequest $request = null;
|
|
public function __construct(?IncomingRequest $_request = null)
|
|
{
|
|
$this->request = $_request;
|
|
}
|
|
|
|
abstract public function getModelClass(): mixed;
|
|
abstract public function getEntityClass(): mixed;
|
|
abstract public function getClassName(): string;
|
|
final public function __get($name)
|
|
{
|
|
if (!array_key_exists($name, $this->_serviceDatas)) {
|
|
return null;
|
|
}
|
|
return $this->_serviceDatas[$name];
|
|
}
|
|
final public function __set($name, $value): void
|
|
{
|
|
$this->_serviceDatas[$name] = $value;
|
|
}
|
|
final public function getClassPath(): string
|
|
{
|
|
return $this->getClassName();
|
|
}
|
|
final public function getRequest(): IncomingRequest|null
|
|
{
|
|
return $this->request;
|
|
}
|
|
final public function getModel(): mixed
|
|
{
|
|
if (!$this->_model) {
|
|
$this->_model = $this->getModelClass();
|
|
}
|
|
return $this->_model;
|
|
}
|
|
public function getFields(): array
|
|
{
|
|
return $this->getModel()->getFields();
|
|
}
|
|
public function getFilterFields(): array
|
|
{
|
|
return $this->getModel()->getFilterFields();
|
|
}
|
|
public function getBatchJobFields(): array
|
|
{
|
|
return $this->getModel()->getBatchJobFields();
|
|
}
|
|
final public function getEntity(mixed $where, $isThrow = true): mixed
|
|
{
|
|
$entity = is_array($where) ? $this->getModel()->where($where)->first() : $this->getModel()->find($where);
|
|
if (!$entity) {
|
|
if ($isThrow) {
|
|
throw new \Exception(__FUNCTION__ . " => 해당 정보를 찾을수 없습니다.");
|
|
}
|
|
}
|
|
return $entity;
|
|
}
|
|
final public function getEntities(array $columns = ['*']): array
|
|
{
|
|
$entitys = [];
|
|
foreach ($this->getModel()->select(implode(',', $columns))->findAll() as $entity) {
|
|
$entitys[$entity->getPK()] = $entity;
|
|
}
|
|
if (env('app.debug.index')) {
|
|
echo $this->getModel()->getLastQuery();
|
|
// exit;
|
|
}
|
|
return $entitys;
|
|
} //
|
|
//FieldForm관련용
|
|
public function getFieldRule(string $action, string $field): string
|
|
{
|
|
if (is_array($field)) {
|
|
throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
|
|
}
|
|
switch ($field) {
|
|
default:
|
|
$rule = $this->getModel()->getFieldRule($action, $field);
|
|
break;
|
|
}
|
|
return $rule;
|
|
}
|
|
public function getFormFieldOption(string $field, array $options = []): array
|
|
{
|
|
switch ($field) {
|
|
default:
|
|
foreach ($this->getEntities() as $entity) {
|
|
$options[$entity->getPK()] = $entity->getTitle();
|
|
}
|
|
break;
|
|
}
|
|
// dd($options);
|
|
return $options;
|
|
}
|
|
|
|
public function create(array $formDatas, mixed $entity = null): mixed
|
|
{
|
|
$entity = $this->getModel()->create($formDatas, $entity ?? $this->getEntityClass());
|
|
LogCollector::info("[{$entity->getTitle()}]" . MESSAGES["CREATED"] . ":");
|
|
return $entity;
|
|
}
|
|
public function modify(mixed $entity, array $formDatas): mixed
|
|
{
|
|
$entity = $this->getModel()->modify($entity, $formDatas);
|
|
LogCollector::info("[{$entity->getTitle()}]" . MESSAGES["UPDATED"] . ":");
|
|
return $entity;
|
|
}
|
|
final public function delete(mixed $entity): bool
|
|
{
|
|
$result = $this->getModel()->delete($entity->getPK());
|
|
if (!$result) {
|
|
$message = sprintf(
|
|
"\n------%s SQL오류-----\n%s\n------------------------------\n",
|
|
__FUNCTION__,
|
|
$this->getModel()->getLastQuery()
|
|
);
|
|
LogCollector::error($message);
|
|
return false;
|
|
}
|
|
LogCollector::info("[{$entity->getTitle()}]" . MESSAGES["DELETED"] . ":");
|
|
return true;
|
|
}
|
|
}
|