Newer
Older

Yassine Doghri
committed
declare(strict_types=1);
/**
* @copyright 2021 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Entities;

Yassine Doghri
committed
use App\Models\PersonModel;

Yassine Doghri
committed
use CodeIgniter\Entity\Entity;

Yassine Doghri
committed
use RuntimeException;
/**
* @property int $id
* @property string $full_name
* @property string $unique_name
* @property string|null $information_url
* @property int $avatar_id

Yassine Doghri
committed
* @property Image $avatar
* @property int $created_by
* @property int $updated_by
* @property object[]|null $roles
class Person extends Entity
{
protected ?Image $avatar = null;

Yassine Doghri
committed
/**
* @var object[]|null

Yassine Doghri
committed
*/
protected ?array $roles = null;

Yassine Doghri
committed
/**
* @var array<string, string>
*/
protected $casts = [
'id' => 'integer',
'full_name' => 'string',
'unique_name' => 'string',
'information_url' => '?string',
'avatar_id' => '?int',
'podcast_id' => '?integer',
'episode_id' => '?integer',
'created_by' => 'integer',
'updated_by' => 'integer',
];
/**

Yassine Doghri
committed
* Saves the person avatar in `public/media/persons/`

Yassine Doghri
committed
public function setAvatar(?Image $avatar = null): static

Yassine Doghri
committed
if ($avatar === null) {
return $this;
}
helper('media');

Yassine Doghri
committed
$avatar->saveImage(config('Images')->personAvatarSizes, 'persons', $this->attributes['unique_name']);

Yassine Doghri
committed
$this->attributes['avatar_mimetype'] = $avatar->mimetype;
$this->attributes['avatar_path'] = $avatar->path;
return $this;
}

Yassine Doghri
committed
public function getAvatar(): Image

Yassine Doghri
committed
if ($this->attributes['avatar_path'] === null) {

Yassine Doghri
committed
return new Image(null, '/castopod-avatar-default.jpg', 'image/jpeg', config('Images')->personAvatarSizes);

Yassine Doghri
committed
return new Image(null, $this->attributes['avatar_path'], $this->attributes['avatar_mimetype'], config(
'Images'
)->personAvatarSizes);

Yassine Doghri
committed
/**
* @return object[]

Yassine Doghri
committed
*/
public function getRoles(): array
{
if ($this->attributes['podcast_id'] === null) {

Yassine Doghri
committed
throw new RuntimeException('Person must have a podcast_id before getting roles.');

Yassine Doghri
committed
}
if ($this->roles === null) {
$this->roles = (new PersonModel())->getPersonRoles(
$this->id,

Yassine Doghri
committed
(int) $this->attributes['podcast_id'],
array_key_exists('episode_id', $this->attributes) ? (int) $this->attributes['episode_id'] : null
);

Yassine Doghri
committed
}
return $this->roles;
}