<?php

namespace App\Entities;

use CodeIgniter\Entity\Entity;

abstract class CommonEntity extends Entity
{
    protected $datamap = [];
    protected $dates   = ['created_at', 'updated_at', 'deleted_at'];
    //사용법   : $client->created_at->format('Y-m-d') 
    //비교방법 : if ($client->created_at < new \DateTime('2024-01-01')) {
    protected $casts   = [];

    public function __construct(array|null $data = null)
    {
        parent::__construct($data);
    }

    final public function getPK(): string
    {
        $field = constant("static::PK");
        return $this->attributes[$field];
    }
    public function getTitle(): string
    {
        $field = constant("static::TITLE");
        return $this->attributes[$field];
    }
    final public function isMatched(string $field, string $value): bool
    {
        //Helper의 getListRowColor 함수에서 사용
        if (!array_key_exists($field, $this->attributes)) {
            return true;
        };
        return $this->attributes[$field] == $value;
    }
    final public function getUpdatedAt(): string|null
    {
        return $this->attributes['updated_at'];
    }
    final public function setUpdatedAt(string $value): void
    {
        $this->attributes['updated_at'] = $value;
    }
    final public function getCreatedAt(): string
    {
        return $this->attributes['created_at'];
    }
    public function getStatus(): string|null
    {
        return $this->attributes['status'] ?? null;
    }
}
