castopod/app/Entities/Credit.php
Yassine Doghri 231d578d64
refactor: add phpstan and update code to adhere to level 5
- move and refactor Image.php from Libraries to Entities folder
- update some database field names
/ types
- update composer packages
2021-05-12 14:00:25 +00:00

145 lines
3.1 KiB
PHP

<?php
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Entities;
use RuntimeException;
use App\Models\PersonModel;
use App\Models\PodcastModel;
use App\Models\EpisodeModel;
use CodeIgniter\Entity\Entity;
/**
* @property int $podcast_id
* @property Podcast $podcast
* @property int|null $episode_id
* @property Episode|null $episode
* @property string $full_name
* @property string $person_group
* @property string $group_label
* @property string $person_role
* @property string $role_label
* @property int $person_id
* @property Person $person
*/
class Credit extends Entity
{
/**
* @var Person
*/
protected $person;
/**
* @var Podcast
*/
protected $podcast;
/**
* @var Episode|null
*/
protected $episode;
/**
* @var string
*/
protected $group_label;
/**
* @var string
*/
protected $role_label;
/**
* @var array<string, string>
*/
protected $casts = [
'podcast_id' => 'integer',
'episode_id' => '?integer',
'person_id' => 'integer',
'full_name' => 'string',
'person_group' => 'string',
'person_role' => 'string',
];
public function getPerson(): Person
{
if ($this->person_id === null) {
throw new RuntimeException(
'Credit must have person_id before getting person.',
);
}
if ($this->person === null) {
$this->person = (new PersonModel())->getPersonById(
$this->person_id,
);
}
return $this->person;
}
public function getPodcast(): Podcast
{
if ($this->podcast_id === null) {
throw new RuntimeException(
'Credit must have podcast_id before getting podcast.',
);
}
if ($this->podcast === null) {
$this->podcast = (new PodcastModel())->getPodcastById(
$this->podcast_id,
);
}
return $this->podcast;
}
public function getEpisode(): ?Episode
{
if ($this->episode_id === null) {
throw new RuntimeException(
'Credit must have episode_id before getting episode.',
);
}
if ($this->episode === null) {
$this->episode = (new EpisodeModel())->getPublishedEpisodeById(
$this->podcast_id,
$this->episode_id,
);
}
return $this->episode;
}
public function getGroupLabel(): string
{
if ($this->person_group === null) {
return '';
}
return lang("PersonsTaxonomy.persons.{$this->person_group}.label");
}
public function getRoleLabel(): string
{
if ($this->person_group === '') {
return '';
}
if ($this->person_role === '') {
return '';
}
return lang(
"PersonsTaxonomy.persons.{$this->person_group}.roles.{$this->person_role}.label",
);
}
}