Automation/app/Controllers/CommonController.php
2024-09-20 19:51:36 +09:00

117 lines
4.0 KiB
PHP

<?php
namespace App\Controllers;
use App\Controllers\BaseController;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
abstract class CommonController extends BaseController
{
private $_attributes = [];
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
}
final public function __get($name)
{
if (!array_key_exists($name, $this->_attributes)) {
return null;
}
return $this->_attributes[$name];
}
final public function __set($name, $value): void
{
$this->_attributes[$name] = $value;
}
final public function getAttributes(): array
{
return $this->_attributes;
}
//Field별 Form Option용
protected function getFormFilterOption(string $filter_field, array $filter_options): array
{
switch ($filter_field) {
default:
$temps = lang($this->class_name . '.' . strtoupper($filter_field));
if (!is_array($temps)) {
throw new \Exception(__FUNCTION__ . "에서 {$filter_field}의 데이터가 array가 아닙니다.\n" . var_export($temps, true));
}
$filter_options[$filter_field] = [
["" => lang($this->class_name . '.label.' . $filter_field) . ' 선택'],
lang($this->class_name . '.' . strtoupper($filter_field))
];
break;
}
return $filter_options;
}
//Field별 Form Option용
final public function getFormFilterOptions(array $filter_options = []): array
{
foreach ($this->filter_fields as $filter_field) {
if (is_array($filter_field)) {
throw new \Exception(__FUNCTION__ . "에서 filter_field가 array 입니다.\n" . var_export($filter_field, true));
}
$filter_options = $this->getFormFilterOption($filter_field, $filter_options);
}
return $filter_options;
}
protected function getFormFieldRule(string $field, array $rules): array
{
if (is_array($field)) {
throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
}
switch ($field) {
default:
$rules[$field] = $this->_model->getFieldRule($field, $rules);;
break;
}
return $rules;
}
final public function getFormFieldRules(array $rules = []): array
{
foreach ($this->fields as $field) {
$rules = $this->getFormFieldRule($field, $rules);
}
return $rules;
}
//전송된 데이터
protected function getFormData(string $field): void
{
switch ($field) {
default:
$this->formDatas[$field] = rtrim($this->request->getVar($field));
break;
}
}
//전송된 데이터 재정의
protected function convertFormData(string $field): void
{
switch ($field) {
default:
break;
}
}
protected function create_form_process(): void
{
helper(['form']);
}
protected function create_process($model): mixed
{
//변경할 값 확인 : Upload된 파일 검증시 $this->request->getVar()보다 먼처 체크필요
$validation = \Config\Services::validation();
$validation->setRules($model->getFieldRules($this->fields));
if (!$validation->withRequest($this->request)->run()) {
throw new \Exception("데이터 검증 오류발생\n" . implode("\n", $this->validator->getErrors()));
}
foreach ($this->fields as $field) {
$this->getFormData($field);
}
foreach ($this->fields as $field) {
$this->convertFormData($field);
}
return $model->create($this->formDatas);
}
}