47 lines
1.4 KiB
PHP
47 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Entities;
|
|
|
|
use App\Entities\CommonEntity;
|
|
use App\Models\UserModel as Model;
|
|
|
|
class UserEntity extends CommonEntity
|
|
{
|
|
const PK = Model::PK;
|
|
const TITLE = Model::TITLE;
|
|
const DEFAULT_STATUS = STATUS['AVAILABLE'];
|
|
|
|
// 💡 1. $casts 속성 추가:
|
|
// DB에 JSON 형태로 저장된 'role' 컬럼을 PHP에서 배열로 자동 변환합니다.
|
|
protected $casts = [
|
|
'role' => 'json-array',
|
|
];
|
|
|
|
public function getID(): string
|
|
{
|
|
return (string) $this->attributes['id'];
|
|
}
|
|
|
|
public function getPassword(): string
|
|
{
|
|
return $this->attributes['passwd'];
|
|
}
|
|
// $formDatas['passwd']에 평문 비밀번호가 들어있으면,
|
|
//Model->insert시 Entity 생성자($formDatas)가 setPasswd()를 자동으로 호출합니다.
|
|
public function setPasswd(string $password)
|
|
{
|
|
$this->attributes['passwd'] = password_hash($password, PASSWORD_BCRYPT);
|
|
}
|
|
/**
|
|
* 사용자의 역할을 배열 형태로 반환합니다.
|
|
* $casts에 의해 DB에서 읽어올 때 이미 배열로 변환되어 있습니다.
|
|
* @return array
|
|
*/
|
|
public function getRole(): array
|
|
{
|
|
// 💡 2. 반환 타입을 array로 변경하고,
|
|
// null일 경우를 대비해 빈 배열을 반환하도록 처리합니다.
|
|
return $this->attributes['role'] ?? [];
|
|
}
|
|
}
|