81 lines
2.2 KiB
PHP
81 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use CodeIgniter\HTTP\IncomingRequest;
|
|
|
|
abstract class CommonService
|
|
{
|
|
private $_serviceDatas = [];
|
|
private $_model = null;
|
|
protected ?IncomingRequest $request = null;
|
|
public function __construct(IncomingRequest $_request)
|
|
{
|
|
$this->request = $_request;
|
|
}
|
|
abstract public function getModelClass(): string;
|
|
abstract public function getEntityClass(): 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 getRequest(): IncomingRequest|null
|
|
{
|
|
return $this->request;
|
|
}
|
|
final public function getModel(): mixed
|
|
{
|
|
if (!$this->_model) {
|
|
$modelClass = $this->getModelClass();
|
|
$this->_model = new $modelClass();
|
|
// $this->_model->setDebug(true);
|
|
}
|
|
return $this->_model;
|
|
}
|
|
final public function getEntity(): mixed
|
|
{
|
|
return $this->getModel()->first();
|
|
}
|
|
final public function getEntities(array $columns = ['*']): array
|
|
{
|
|
$entitys = [];
|
|
foreach ($this->getModel()->select(implode(',', $columns))->findAll() as $entity) {
|
|
$entitys[$entity->getPK()] = $entity;
|
|
}
|
|
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;
|
|
}
|
|
}
|