92 lines
2.5 KiB
PHP
92 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Entities;
|
|
|
|
use CodeIgniter\Entity\Entity;
|
|
|
|
abstract class CommonEntity extends Entity
|
|
{
|
|
protected $datamap = [];
|
|
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
|
|
|
|
/**
|
|
* 이 엔티티에서 "빈문자/공백 입력은 NULL로 저장"해야 하는 필드 목록.
|
|
* 기본은 빈 배열이고, 각 Entity에서 필요한 것만 override해서 채우면 됨.
|
|
*/
|
|
protected array $nullableFields = [];
|
|
public function __construct(array|null $data = null)
|
|
{
|
|
parent::__construct($data);
|
|
}
|
|
|
|
final public function __get(string $key)
|
|
{
|
|
if (array_key_exists($key, $this->attributes)) {
|
|
return $this->attributes[$key];
|
|
}
|
|
return parent::__get($key);
|
|
}
|
|
|
|
/**
|
|
* 중요: Entity에 값이 들어오는 "모든 경로" (new Entity($data), fill(), $entity->field=...)
|
|
* 에서 공통 규칙을 적용하기 위해 __set을 정의.
|
|
*/
|
|
final public function __set(string $key, $value = null)
|
|
{
|
|
if (array_key_exists($key, $this->attributes)) {
|
|
|
|
// 이 엔티티에서 NULL로 보정할 필드만 처리 (화이트리스트)
|
|
if (!empty($this->nullableFields) && in_array($key, $this->nullableFields, true)) {
|
|
if (is_string($value)) {
|
|
$value = trim($value);
|
|
}
|
|
$this->attributes[$key] = ($value === '' || $value === null) ? null : $value;
|
|
return;
|
|
}
|
|
|
|
// 기본: 그대로 저장
|
|
$this->attributes[$key] = $value;
|
|
return;
|
|
}
|
|
|
|
parent::__set($key, $value);
|
|
}
|
|
|
|
final public function getPK(): int|string
|
|
{
|
|
$field = constant("static::PK");
|
|
return $this->attributes[$field] ?? "";
|
|
}
|
|
|
|
final public function getTitle(): string
|
|
{
|
|
$field = constant("static::TITLE");
|
|
return $this->attributes[$field] ?? "";
|
|
}
|
|
|
|
final public function getStatus(): string
|
|
{
|
|
return $this->attributes['status'] ?? "";
|
|
}
|
|
|
|
final public function getUpdatedAt(): string
|
|
{
|
|
return $this->attributes['updated_at'] ?? "";
|
|
}
|
|
|
|
final public function getCreatedAt(): string
|
|
{
|
|
return $this->attributes['created_at'] ?? "";
|
|
}
|
|
|
|
final public function getDeletedAt(): string
|
|
{
|
|
return $this->attributes['deleted_at'] ?? "";
|
|
}
|
|
|
|
public function getCustomTitle(): string
|
|
{
|
|
return $this->getTitle();
|
|
}
|
|
}
|