120 lines
3.0 KiB
PHP
120 lines
3.0 KiB
PHP
<?php
|
||
|
||
namespace App\Entities;
|
||
|
||
use CodeIgniter\Entity\Entity;
|
||
use DateTime;
|
||
|
||
abstract class CommonEntity extends Entity
|
||
{
|
||
/**
|
||
* 날짜 필드
|
||
*/
|
||
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
|
||
|
||
/**
|
||
* 빈 문자열을 NULL로 변환할 필드
|
||
* 각 Entity에서 override 해서 사용
|
||
*/
|
||
protected array $nullableFields = [];
|
||
|
||
/**
|
||
* 생성자
|
||
*/
|
||
public function __construct(array|null $data = null)
|
||
{
|
||
parent::__construct($data);
|
||
}
|
||
|
||
/*
|
||
|--------------------------------------------------------------------------
|
||
| Magic Getter
|
||
|--------------------------------------------------------------------------
|
||
| Entity 내부에서도 $this->field 스타일 사용 가능
|
||
|--------------------------------------------------------------------------
|
||
*/
|
||
|
||
final public function __get(string $key)
|
||
{
|
||
return $this->attributes[$key] ?? null;
|
||
}
|
||
|
||
/*
|
||
|--------------------------------------------------------------------------
|
||
| Magic Setter
|
||
|--------------------------------------------------------------------------
|
||
*/
|
||
|
||
final public function __set(string $key, $value = null)
|
||
{
|
||
// 1️⃣ 문자열 trim
|
||
if (is_string($value)) {
|
||
$value = trim($value);
|
||
}
|
||
|
||
// 2️⃣ nullable 처리
|
||
if (in_array($key, $this->nullableFields, true)) {
|
||
if ($value === '' || $value === null) {
|
||
$value = null;
|
||
}
|
||
}
|
||
|
||
// 3️⃣ 날짜 자동 변환
|
||
// if (in_array($key, $this->dates, true)) {
|
||
// if (is_string($value) && $value !== '') {
|
||
// $value = new DateTime($value);
|
||
// }
|
||
// }
|
||
|
||
// ✅ 반드시 부모 __set 호출
|
||
$this->attributes[$key] = $value;
|
||
}
|
||
|
||
/*
|
||
|--------------------------------------------------------------------------
|
||
| 공통 Getter들 (모두 property 스타일 사용)
|
||
|--------------------------------------------------------------------------
|
||
*/
|
||
final public function getPK(): int|string|null
|
||
{
|
||
$field = constant('static::PK');
|
||
return $this->$field;
|
||
}
|
||
|
||
final public function getTitle(): ?string
|
||
{
|
||
$field = constant('static::TITLE');
|
||
return $this->$field;
|
||
}
|
||
|
||
final public function getStatus(): string
|
||
{
|
||
return $this->status;
|
||
}
|
||
|
||
final public function getCreatedAt(): string
|
||
{
|
||
return $this->created_at;
|
||
}
|
||
|
||
final public function getUpdatedAt(): ?string
|
||
{
|
||
return $this->updated_at;
|
||
}
|
||
|
||
final public function getDeletedAt(): ?string
|
||
{
|
||
return $this->deleted_at;
|
||
}
|
||
|
||
/*
|
||
|--------------------------------------------------------------------------
|
||
| Override Getter들 (모두 property 스타일 사용)
|
||
|--------------------------------------------------------------------------
|
||
*/
|
||
|
||
public function getCustomTitle(): string
|
||
{
|
||
return (string) $this->getTitle();
|
||
}
|
||
} |