Automation init...1
This commit is contained in:
parent
305c5fd460
commit
115abc18ff
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"php.version": "8.3"
|
||||
}
|
||||
@ -23,7 +23,6 @@ class FreeboardLibrary extends MangboardLibrary
|
||||
|
||||
public function create(FreeboardEntity $entity, array $formDatas = []): FreeboardEntity
|
||||
{
|
||||
$this->getModel()->setCreateField(); //Create용 Field,Rule지정
|
||||
$entity = $this->getModel()->create($entity, $formDatas);
|
||||
log_message("debug", __FUNCTION__ . "=>등록이 완료되었습니다.");
|
||||
return $entity;
|
||||
|
||||
@ -2,9 +2,7 @@
|
||||
|
||||
namespace App\Libraries\Mangboard;
|
||||
|
||||
use App\Entities\Mangboard\UserEntity;
|
||||
use App\Libraries\CommonLibrary;
|
||||
use App\Models\Mangboard\UserModel;
|
||||
|
||||
abstract class MangboardLibrary extends CommonLibrary
|
||||
{
|
||||
|
||||
@ -152,7 +152,7 @@ class YamapLibrary extends CommonLibrary
|
||||
if ($this->isContainsHttpOrHttps($image['orignal'])) {
|
||||
$mediaTags[] = $images['orignal'];
|
||||
} else {
|
||||
$mediaTags[] = sprintf("<img src=\"%s/%s\" alt=\"%s\">", $image["path"], $image["fileName"], $image["fileName"]);
|
||||
$mediaTags[] = sprintf("<img src=\"/uploads/%s/%s\" alt=\"%s\">", $image["path"], $image["fileName"], $image["fileName"]);
|
||||
};
|
||||
}
|
||||
|
||||
@ -162,7 +162,7 @@ class YamapLibrary extends CommonLibrary
|
||||
if ($this->isContainsHttpOrHttps($video['orignal'])) {
|
||||
$mediaTags[] = $videos['orignal'];
|
||||
} else {
|
||||
$mediaTags[] = sprintf("<video src=\"%s/%s\" alt=\"%s\">", $video["path"], $video["fileName"], $video["fileName"]);
|
||||
$mediaTags[] = sprintf("<video src=\"/uploads/%s/%s\" alt=\"%s\">", $video["path"], $video["fileName"], $video["fileName"]);
|
||||
};
|
||||
}
|
||||
$mediaInfos = array_merge($images, $videos);
|
||||
|
||||
@ -45,27 +45,34 @@ abstract class CommonModel extends Model
|
||||
protected $beforeDelete = [];
|
||||
protected $afterDelete = [];
|
||||
|
||||
protected function __construct()
|
||||
protected function __construct(array $fields)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->setFields($fields);
|
||||
$this->setRules($this->getFields());
|
||||
}
|
||||
|
||||
abstract public function getPK(): string;
|
||||
abstract public function getTitleField(): string;
|
||||
abstract public function setCreateField();
|
||||
abstract public function setModifyField();
|
||||
|
||||
final public function getFields(): array
|
||||
public function getPKField(): string
|
||||
{
|
||||
return $this->primaryKey;
|
||||
}
|
||||
final public function getFields(array $options = []): array //options=>except or only
|
||||
{
|
||||
if (isset($options['except'])) {
|
||||
return array_diff_key($this->allowedFields, array_flip($options['except']));
|
||||
} elseif (isset($options['only'])) {
|
||||
return array_intersect_key($this->allowedFields, array_flip($options['only']));
|
||||
}
|
||||
return $this->allowedFields;
|
||||
}
|
||||
final public function setFields(array $fields): void
|
||||
{
|
||||
$this->allowedFields = $fields;
|
||||
}
|
||||
final public function getRules(array $options): array
|
||||
final public function getRules(array $options): array //options=>except or only
|
||||
{
|
||||
return $this->getValidationRules($options); //options=>except or only
|
||||
return $this->getValidationRules($options);
|
||||
}
|
||||
final public function setRules(array $fields, $rules = []): void
|
||||
{
|
||||
@ -75,29 +82,16 @@ abstract class CommonModel extends Model
|
||||
$this->setValidationRules($rules);
|
||||
}
|
||||
|
||||
|
||||
final public function getUUID(): string
|
||||
{
|
||||
$randomBytes = bin2hex(random_bytes(32));
|
||||
return sprintf(
|
||||
'%08s-%04s-%04x-%04x-%12s',
|
||||
substr($randomBytes, 0, 8),
|
||||
substr($randomBytes, 8, 4),
|
||||
substr($randomBytes, 12, 4),
|
||||
substr($randomBytes, 16, 4),
|
||||
substr($randomBytes, 20, 12)
|
||||
);
|
||||
}
|
||||
protected function getUUIDFieldRule($condition = 'required'): string
|
||||
{
|
||||
return "{$condition}|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
|
||||
}
|
||||
protected function getFieldRule(string $field, array $rules): array
|
||||
{
|
||||
switch ($field) {
|
||||
case $this->getPK():
|
||||
case $this->getPKField():
|
||||
//수동입력인경우
|
||||
$rules[$field] = !$this->useAutoIncrement ? $this->getUUIDFieldRule() . "|is_unique[{$this->table}.{$field}]" : "required|numeric";
|
||||
if (!$this->useAutoIncrement) {
|
||||
$rules[$field] = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]|is_unique[{$this->table}.{$field}]";
|
||||
} else {
|
||||
$rules[$field] = "required|numeric";
|
||||
};
|
||||
break;
|
||||
case $this->getTitleField():
|
||||
$rules[$field] = "required|trim|string";
|
||||
@ -126,7 +120,7 @@ abstract class CommonModel extends Model
|
||||
return $rules;
|
||||
}
|
||||
|
||||
final public function getEntity()
|
||||
final public function getEntity(): array|object|null
|
||||
{
|
||||
return $this->asObject($this->returnType)->first();
|
||||
}
|
||||
@ -135,7 +129,7 @@ abstract class CommonModel extends Model
|
||||
return $this->asObject($this->returnType)->findAll();
|
||||
}
|
||||
|
||||
private function save_process($entity)
|
||||
private function save_process($entity): mixed
|
||||
{
|
||||
// echo var_export($entity, true);
|
||||
// exit;
|
||||
@ -152,45 +146,64 @@ abstract class CommonModel extends Model
|
||||
}
|
||||
|
||||
//create , modify 직전 작업용 작업
|
||||
protected function changeFormData(string $field, array $formDatas, $entity)
|
||||
protected function setEntityData($entity, string $field, $value = null): mixed
|
||||
{
|
||||
switch ($field) {
|
||||
case "passwd":
|
||||
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
|
||||
$entity->$field = password_hash($formDatas[$field], PASSWORD_DEFAULT);
|
||||
case $this->getPKField():
|
||||
if ($value === null) {
|
||||
$randomBytes = bin2hex(random_bytes(32));
|
||||
$entity->$field = sprintf(
|
||||
'%08s-%04s-%04x-%04x-%12s',
|
||||
substr($randomBytes, 0, 8),
|
||||
substr($randomBytes, 8, 4),
|
||||
substr($randomBytes, 12, 4),
|
||||
substr($randomBytes, 16, 4),
|
||||
substr($randomBytes, 20, 12)
|
||||
);
|
||||
} else {
|
||||
$entity->$field = $value;
|
||||
}
|
||||
break;
|
||||
case "passwd":
|
||||
$entity->$field = password_hash($value, PASSWORD_DEFAULT);
|
||||
break;
|
||||
case "content":
|
||||
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
|
||||
$entity->$field = htmlentities($formDatas[$field]);
|
||||
}
|
||||
$entity->$field = htmlentities($value);
|
||||
break;
|
||||
default:
|
||||
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
|
||||
$entity->$field = $formDatas[$field];
|
||||
}
|
||||
$entity->$field = $value;
|
||||
break;
|
||||
}
|
||||
return $entity;
|
||||
}
|
||||
|
||||
protected function create_process($entity, array $formDatas = [])
|
||||
protected function create_process($entity, array $formDatas = []): mixed
|
||||
{
|
||||
foreach ($this->getFields() as $field) {
|
||||
$entity = $this->changeFormData($field, $formDatas, $entity);
|
||||
if (array_key_exists($field, $formDatas) && $formDatas[$field] !== $entity->$field) {
|
||||
$entity = $this->setEntityData($entity, $field, $formDatas[$field]);
|
||||
}
|
||||
}
|
||||
//primaryKey가 수동입력이면
|
||||
$pkField = $this->getPKField();
|
||||
if (!$this->useAutoIncrement && $entity->$pkField === null) {
|
||||
//PrimayKey Field를 allowedFields의 맨앞에 넣기 -> array_unshif
|
||||
array_unshift($this->allowedFields, $this->getPKField());
|
||||
$entity = $this->setEntityData($entity, $this->getPKField());
|
||||
}
|
||||
$entity = $this->save_process($entity);
|
||||
//primaryKey가 자동입력이면
|
||||
if ($this->useAutoIncrement) {
|
||||
$pk = $this->getPK();
|
||||
$entity->$pk = $this->getInsertID();
|
||||
$entity = $this->setEntityData($entity, $this->getPKField(), $this->getInsertID());
|
||||
}
|
||||
return $entity;
|
||||
}
|
||||
protected function modify_process($entity, array $formDatas = [])
|
||||
protected function modify_process($entity, array $formDatas = []): mixed
|
||||
{
|
||||
foreach ($this->getFields() as $field) {
|
||||
$entity = $this->changeFormData($field, $formDatas, $entity);
|
||||
if (array_key_exists($field, $formDatas) && $formDatas[$field] !== $entity->$field) {
|
||||
$entity = $this->setEntityData($entity, $field, $formDatas[$field]);
|
||||
}
|
||||
}
|
||||
return $this->save_process($entity);
|
||||
}
|
||||
|
||||
@ -11,14 +11,9 @@ class FreeboardModel extends CommonModel
|
||||
protected $primaryKey = 'pid';
|
||||
protected $returnType = FreeboardEntity::class;
|
||||
|
||||
public function __construct()
|
||||
public function __construct(array $fields = ["title", "text", "content"])
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function getPK(): string
|
||||
{
|
||||
return $this->primaryKey;
|
||||
parent::__construct($fields);
|
||||
}
|
||||
public function getTitleField(): string
|
||||
{
|
||||
@ -43,7 +38,7 @@ class FreeboardModel extends CommonModel
|
||||
|
||||
public function getEntityByPK(int $uid): null|FreeboardEntity
|
||||
{
|
||||
$this->where($this->getPK(), $uid);
|
||||
$this->where($this->getPKField(), $uid);
|
||||
return $this->getEntity();
|
||||
}
|
||||
public function getEntityByID(string $id): null|FreeboardEntity
|
||||
@ -53,11 +48,6 @@ class FreeboardModel extends CommonModel
|
||||
}
|
||||
|
||||
//create용
|
||||
public function setCreateField()
|
||||
{
|
||||
$this->setFields(["title", "text", 'content']);
|
||||
$this->setRules($this->getFields());
|
||||
}
|
||||
public function create(FreeboardEntity $entity, array $formDatas = []): FreeboardEntity
|
||||
{
|
||||
$entity = $this->create_process($entity, $formDatas);
|
||||
@ -69,11 +59,6 @@ class FreeboardModel extends CommonModel
|
||||
}
|
||||
|
||||
//modify용
|
||||
public function setModifyField()
|
||||
{
|
||||
$this->setFields(["title", "text", 'content']);
|
||||
$this->setRules($this->getFields());
|
||||
}
|
||||
public function modify(FreeboardEntity $entity, array $formDatas = []): FreeboardEntity
|
||||
{
|
||||
return $this->modify_process($entity, $formDatas);
|
||||
|
||||
@ -11,15 +11,9 @@ class UserModel extends CommonModel
|
||||
protected $primaryKey = 'pid';
|
||||
protected $returnType = UserEntity::class;
|
||||
|
||||
public function __construct()
|
||||
public function __construct(array $fields = ["user_id", "passwd", "user_name", "user_email", "user_state", "user_level", "user_point"])
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
public function getPK(): string
|
||||
{
|
||||
return $this->primaryKey;
|
||||
parent::__construct($fields);
|
||||
}
|
||||
public function getTitleField(): string
|
||||
{
|
||||
@ -51,7 +45,7 @@ class UserModel extends CommonModel
|
||||
|
||||
public function getEntityByPK(int $uid): null|UserEntity
|
||||
{
|
||||
$this->where($this->getPK(), $uid);
|
||||
$this->where($this->getPKField(), $uid);
|
||||
return $this->getEntity();
|
||||
}
|
||||
public function getEntityByID(string $id): null|UserEntity
|
||||
@ -61,11 +55,6 @@ class UserModel extends CommonModel
|
||||
}
|
||||
|
||||
//create용
|
||||
public function setCreateField()
|
||||
{
|
||||
$this->setFields(["user_id", "passwd", "user_name", "user_email", "user_state", "user_level", "user_point"]);
|
||||
$this->setRules($this->getFields());
|
||||
}
|
||||
public function create(UserEntity $entity, array $formDatas = []): UserEntity
|
||||
{
|
||||
$entity = $this->create_process($entity, $formDatas);
|
||||
@ -75,11 +64,6 @@ class UserModel extends CommonModel
|
||||
}
|
||||
|
||||
//modify용
|
||||
public function setModifyField()
|
||||
{
|
||||
$this->setFields(["user_id", "passwd", "user_name", "user_email", "user_state", "user_level", "user_point"]);
|
||||
$this->setRules($this->getFields());
|
||||
}
|
||||
public function modify(UserEntity $entity, array $formDatas = []): UserEntity
|
||||
{
|
||||
return $this->modify_process($entity, $formDatas);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user