84 lines
2.8 KiB
PHP
84 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Controllers\BaseController;
|
|
|
|
abstract class CommonController extends BaseController
|
|
{
|
|
private $_options = [];
|
|
final public function __get($name)
|
|
{
|
|
if (!array_key_exists($name, $this->_options)) {
|
|
return null;
|
|
}
|
|
return $this->_options[$name];
|
|
}
|
|
|
|
final public function __set($name, $value): void
|
|
{
|
|
$this->_options[$name] = $value;
|
|
}
|
|
|
|
//전송된 값 검증 및 임시저장
|
|
final public function getFormFieldDatas(array $fields, array $fieldRules, array $formDatas = []): array
|
|
{
|
|
foreach ($fields as $field) {
|
|
$formDatas[$field] = rtrim($this->request->getVar($field));
|
|
log_message("debug", "{$field} : {$formDatas[$field]}");
|
|
}
|
|
//변경할 값 확인
|
|
if (!$this->validate($fieldRules)) {
|
|
throw new \Exception("데이터 검증 오류발생\n" . implode("\n", $this->validator->getErrors()));
|
|
}
|
|
return $formDatas;
|
|
}
|
|
//Field별 Form Option용
|
|
protected function getFormFieldOption(string $field, array $options): array
|
|
{
|
|
switch ($field) {
|
|
default:
|
|
$temps = lang($this->_className . '.' . strtoupper($field));
|
|
if (!is_array($temps)) {
|
|
throw new \Exception(__FUNCTION__ . "에서 {$field}의 데이터가 array가 아닙니다.\n" . var_export($temps, true));
|
|
}
|
|
$options[$field] = [
|
|
["" => lang($this->_className . '.label.' . $field) . ' 선택'],
|
|
lang($this->_className . '.' . strtoupper($field))
|
|
];
|
|
break;
|
|
}
|
|
return $options;
|
|
}
|
|
//Field별 Form Option용
|
|
final public function getFormFieldOptions(array $fields, array $options = []): array
|
|
{
|
|
foreach ($fields as $field) {
|
|
if (is_array($field)) {
|
|
throw new \Exception(__FUNCTION__ . "에서 field가 array 입니다.\n" . var_export($field, true));
|
|
}
|
|
$options = $this->getFormFieldOption($field, $options);
|
|
}
|
|
return $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 $fields, array $rules = []): array
|
|
{
|
|
foreach ($fields as $field) {
|
|
$rules = $this->getFormFieldRule($field, $rules);
|
|
}
|
|
return $rules;
|
|
}
|
|
}
|