dbms/app/Services/CommonService.php
2025-04-28 19:06:09 +09:00

149 lines
5.9 KiB
PHP

<?php
namespace App\Services;
use CodeIgniter\HTTP\IncomingRequest;
abstract class CommonService
{
private $_serviceDatas = [];
private $_model = null;
public function __construct() {}
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 getModel(): mixed
{
if ($this->_model === null) {
$modelClass = $this->getModelClass();
$this->_model = new $modelClass();
// $this->_model->setDebug(true);
}
return $this->_model;
}
final public function getEntity(): mixed
{
$result = $this->getModel()->get();
if (!$result) { //결과값이 없으면 null
return $result;
}
$entityClass = $this->getEntityClass();
return new $entityClass($result);
}
final public function getEntities(): array
{
$entitys = [];
foreach ($this->getModel()->getAll() as $result) {
$entityClass = $this->getEntityClass();
$entity = new $entityClass($result);
$pairField = $this->getModel()->getPairField();
// echo "pairField:" . $pairField . "<BR>";
$entitys[$entity->$pairField] = $entity;
}
return $entitys;
} //
final public function getCount(string $select = "COUNT(*) as cnt", string $column = 'cnt'): int
{
return $this->getModel()->count($select, $column)->countAllResults();
}
//List 조건절 처리
private function setConditionForList(IncomingRequest $request, array $filter_fields): void
{
//조건절 처리
foreach ($filter_fields as $field) {
$this->$field = $request->getVar($field) ?? DEFAULTS['EMPTY'];
if ($this->$field !== DEFAULTS['EMPTY']) {
$this->getModel()->setList_FieldFilter($field, $this->$field);
}
}
//검색어 처리
$this->word = $request->getVar('word') ?? DEFAULTS['EMPTY'];
if ($this->word !== DEFAULTS['EMPTY']) {
$this->getModel()->setList_WordFilter($this->word);
}
//검색일 처리
$this->start = $request->getVar('start') ?? DEFAULTS['EMPTY'];
$this->end = $request->getVar('end') ?: DEFAULTS['EMPTY'];
$this->getModel()->setList_DateFilter($this->start, $this->end);
}
//PageNation 처리
protected function setPageOptionsByPaginationForList(): void
{
$this->page_options = array("" => "줄수선택");
for ($i = $this->per_page; $i <= $this->total_count; $i += $this->per_page) {
$this->page_options[$i] = $i;
}
$this->page_options[$this->total_count] = $this->total_count;
}
protected function setPaginationForList(IncomingRequest $request, $pager_group = 'default', int $segment = 0, $template = 'bootstrap_full'): void
{
//Page, Per_page필요부분
$this->page = (int) $request->getVar('page') ?: 1;
$this->per_page = (int) $request->getVar('per_page') ?: intval(DEFAULT_LIST_PERPAGE ?? 20);
//줄수 처리용
$this->setPageOptionsByPaginationForList();
// 1.Views/Pagers/에 bootstrap_full.php,bootstrap_simple.php 생성
// 2.app/Config/Pager.php/$templates에 'bootstrap_full => 'Pagers\bootstrap_full',
// 'bootstrap_simple' => 'Pagers\bootstrap_simple', 추가
$pager = service("pager");
// $this->getService()->getModel()->paginate($this->per_page, $pager_group, $this->page, $segment);
$pager->makeLinks($this->page, $this->per_page, $this->total_count, $template, $segment, $pager_group);
$this->page = $pager->getCurrentPage($pager_group);
$this->total_page = $pager->getPageCount($pager_group);
$this->pagination - $pager->links($pager_group, $template);
}
protected function setOrderByForList(): void
{
$this->order_field = $this->request->getVar('order_field') ?: DEFAULTS['EMPTY'];
$this->order_value = $this->request->getVar('order_value') ?: DEFAULTS['EMPTY'];
if ($this->order_field !== DEFAULTS['EMPTY'] && $this->order_value !== DEFAULTS['EMPTY']) {
$this->getModel()->orderBy(sprintf("%s.%s %s", $this->getModel()::TABLE, $this->order_field, $this->order_value));
} else {
$this->getModel()->orderBy(sprintf("%s.%s %s", $this->getModel()::TABLE, $this->getModel()::PK, "DESC"));
}
}
final public function getList(IncomingRequest $request, array $fields, array $filter_fields): array
{
//조건절 처리
$this->setConditionForList($request, $filter_fields);
//TotalCount
$this->total_count = $this->getCount();
//Pagination 처리
$this->setPaginationForList($request);
//limit, offset 설정
$this->getModel()->limit($this->per_page);
$this->getModel()->offset(($this->page - 1) * $this->per_page);
return $this->getEntities();
}
final public function isIPAddress(string $ip, $type = false): bool
{
switch ($type) {
case 'ipv4':
$result = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
break;
case 'ipv6':
$result = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
break;
case 'all':
$result = filter_var($ip, FILTER_VALIDATE_IP);
break;
default:
$result = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
break;
}
return $result;
}
}