146 lines
4.7 KiB
PHP
146 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Entities\FormOptionEntity;
|
|
use App\Libraries\LogCollector;
|
|
|
|
abstract class CommonService
|
|
{
|
|
private $_serviceDatas = [];
|
|
private $_model = null;
|
|
private $_classNames = [];
|
|
private $_myAuth = null;
|
|
protected function __construct() {}
|
|
abstract public function getModelClass(): mixed;
|
|
abstract public function getEntityClass(): mixed;
|
|
abstract public function getFormFields(): array;
|
|
abstract public function getFilterFields(): array;
|
|
abstract public function getBatchJobFields(): array;
|
|
final protected function getMyAuth(): mixed
|
|
{
|
|
if (!$this->_myAuth) {
|
|
$this->_myAuth = service('myauth');
|
|
}
|
|
return $this->_myAuth;
|
|
}
|
|
public function getIndexFields(): array
|
|
{
|
|
return $this->getFormFields();
|
|
}
|
|
public function getViewFields(): array
|
|
{
|
|
return $this->getFormFields();
|
|
}
|
|
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 protected function addClassName(string $className): void
|
|
{
|
|
$this->_classNames[] = $className;
|
|
}
|
|
final public function getClassName($delimeter = DIRECTORY_SEPARATOR): string
|
|
{
|
|
return implode($delimeter, $this->_classNames);
|
|
}
|
|
final public function getModel(): mixed
|
|
{
|
|
if (!$this->_model) {
|
|
$this->_model = $this->getModelClass();
|
|
}
|
|
return $this->_model;
|
|
}
|
|
final public function getEntity(mixed $where, ?string $message = null): mixed
|
|
{
|
|
$entity = is_array($where) ? $this->getModel()->where($where)->first() : $this->getModel()->find($where);
|
|
if (!$entity) {
|
|
throw new \Exception($message ?? __METHOD__ . "에서 해당 정보가 존재하지 않습니다");
|
|
}
|
|
return $entity;
|
|
}
|
|
protected function findAllDatas(array $columns = ['*']): mixed
|
|
{
|
|
return $this->getModel()->select(implode(',', $columns))->findAll();
|
|
}
|
|
final public function getEntities(mixed $where = null, array $columns = ['*']): array
|
|
{
|
|
if ($where) {
|
|
$this->getModel()->where($where);
|
|
}
|
|
$entities = [];
|
|
foreach ($this->findAllDatas($columns) as $entity) {
|
|
$entities[$entity->getPK()] = $entity;
|
|
}
|
|
if (env('app.debug.index')) {
|
|
echo $this->getModel()->getLastQuery() . "<BR>";
|
|
}
|
|
return $entities;
|
|
} //
|
|
//기본 기능부분
|
|
|
|
//FieldForm관련용
|
|
public function getFormFieldOption(string $field, array $options = []): array
|
|
{
|
|
switch ($field) {
|
|
default:
|
|
$options = [];
|
|
foreach (lang($this->getClassName() . '.' . strtoupper($field)) as $key => $value) {
|
|
$options[$key] = new FormOptionEntity(['uid' => $key, 'title' => $value]);
|
|
}
|
|
// dd($options);
|
|
break;
|
|
}
|
|
if (!is_array($options)) {
|
|
throw new \Exception(__FUNCTION__ . "에서 field의 options 값이 array가 아닙니다.\n" . var_export($options, true));
|
|
}
|
|
return $options;
|
|
}
|
|
public function getFormFieldRule(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()->getFormFieldRule($action, $field);
|
|
break;
|
|
}
|
|
return $rule;
|
|
}
|
|
public function create(array $formDatas): mixed
|
|
{
|
|
$entity = $this->getModel()->create($formDatas);
|
|
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;
|
|
}
|
|
public function delete(mixed $entity): mixed
|
|
{
|
|
$result = $this->getModel()->delete($entity->getPK());
|
|
if (!$result) {
|
|
$message = sprintf(
|
|
"\n------%s SQL오류-----\n%s\n------------------------------\n",
|
|
__FUNCTION__,
|
|
$this->getModel()->getLastQuery()
|
|
);
|
|
LogCollector::error($message);
|
|
throw new \Exception($message);
|
|
}
|
|
LogCollector::info("[{$entity->getTitle()}]" . MESSAGES["DELETED"] . ":");
|
|
return $entity;
|
|
}
|
|
}
|