127 lines
4.5 KiB
PHP
127 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Forms;
|
|
|
|
use App\Entities\CommonEntity;
|
|
use RuntimeException;
|
|
|
|
abstract class CommonForm
|
|
{
|
|
private array $_attributes = [];
|
|
protected function __construct() {}
|
|
abstract public function getFormFields(string $action, ?array $fields = null): array;
|
|
final public function setAttributes(array $attributes): void
|
|
{
|
|
$this->_attributes = $attributes;
|
|
}
|
|
final public function getAttribute(string $key): string
|
|
{
|
|
if (!array_key_exists($key, $this->_attributes)) {
|
|
throw new RuntimeException(__METHOD__ . "에서 오류발생: {$key}에 해당하는 속성이 정의되지 않았습니다.");
|
|
}
|
|
return $this->_attributes[$key];
|
|
}
|
|
final public function getFormRules(string $action, array $rules = [], ?array $fields = null): array
|
|
{
|
|
foreach ($fields ?? $this->getFormFields($action) as $field) {
|
|
$rules = $this->getFormRule($action, $field, $rules);
|
|
}
|
|
return $rules;
|
|
}
|
|
final public function getFormOptions(string $action, array $options = [], ?array $fields = null): array
|
|
{
|
|
foreach ($fields ?? $this->getFormFilters($action) as $field) {
|
|
$options = $this->getFormOption($action, $field, $options);
|
|
}
|
|
return $options;
|
|
}
|
|
//필수함수
|
|
public function getFormFilters(string $action, ?array $fields = null): array
|
|
{
|
|
return $fields ?? [];
|
|
}
|
|
public function getViewFields(string $action = 'view', ?array $fields = null): array
|
|
{
|
|
return $fields ?? $this->getFormFields($action);
|
|
}
|
|
public function getViewFilters(string $action = 'view', ?array $fields = null): array
|
|
{
|
|
return $fields ?? $this->getFormFilters($action);
|
|
}
|
|
public function getIndexFields(string $action = 'index', ?array $fields = null): array
|
|
{
|
|
return $fields ?? $this->getFormFields($action);
|
|
}
|
|
public function getIndexFilters(string $action = 'index', ?array $fields = null): array
|
|
{
|
|
return $fields ?? $this->getFormFilters($action);
|
|
}
|
|
public function getBatchjobFields(string $action = 'index', ?array $fields = null): array
|
|
{
|
|
return $fields ?? $this->getIndexFilters($action);
|
|
}
|
|
public function getBatchjobButtons(string $action = 'index', ?array $buttions = null): array
|
|
{
|
|
return $buttions ?? [
|
|
'batchjob' => '일괄 처리',
|
|
'batchjob_delete' => '일괄 삭제',
|
|
];
|
|
}
|
|
public function getFormRule(string $action, string $field, array $rules = []): array
|
|
{
|
|
switch ($field) {
|
|
case $this->getAttribute('pk_field'):
|
|
if (!$this->$this->getAttribute('useAutoIncrement')) {
|
|
$rule = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
|
|
$rule .= in_array($action, ["create"]) ? "|is_unique[{$this->getAttribute('table')}.{$field}]" : "";
|
|
} else {
|
|
$rule = "required|numeric";
|
|
}
|
|
$rules[$field] = $rule;
|
|
break;
|
|
case $this->getAttribute('title_field'):
|
|
$rules[$field] = "required|trim|string";
|
|
break;
|
|
case "code":
|
|
// a-zA-Z → 영문 대소문자,0-9 → 숫자,가-힣 → 한글 완성형,\- → 하이픈
|
|
$rule = "required|regex_match[/^[a-zA-Z0-9가-힣\-]+$/]|min_length[4]|max_length[20]";
|
|
$rule .= in_array($action, ["create"]) ? "|is_unique[{$this->getAttribute('table')}.{$field}]" : "";
|
|
$rules[$field] = $rule;
|
|
break;
|
|
case 'picture':
|
|
$rules[$field] = "is_image[{$field}]|mime_in[{$field},image/jpg,image/jpeg,image/gif,image/png,image/webp]|max_size[{$field},300]|max_dims[{$field},2048,768]";
|
|
break;
|
|
case "updated_at":
|
|
case "created_at":
|
|
case "deleted_at":
|
|
$rules[$field] = "permit_empty|valid_date";
|
|
break;
|
|
default:
|
|
$rules[$field] = "permit_empty|trim|string";
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
public function getFormOption(string $action, string $field, array $options = []): array
|
|
{
|
|
switch ($field) {
|
|
case 'user_uid':
|
|
$userService = service('UserService');
|
|
$option = $userService->getEntities();
|
|
break;
|
|
case 'clientinfo_uid':
|
|
$clientService = service('ClientService');
|
|
$option = $clientService->getEntities();
|
|
break;
|
|
default:
|
|
$option = lang($this->getAttribute('class_path') . "." . strtoupper($field));
|
|
break;
|
|
}
|
|
if (!is_array($option)) {
|
|
throw new \Exception(static::class . DIRECTORY_SEPARATOR . __FUNCTION__ . "에서 {$field}의 options 값들이 배열이 아닙니다.\n" . var_export($option, true));
|
|
}
|
|
$options[$field] = $option;
|
|
return $options;
|
|
}
|
|
}
|