*/ protected $casts = [ 'id' => 'integer', 'podcast_id' => 'integer', 'episode_id' => 'integer', 'label' => 'string', 'start_time' => 'double', 'duration' => 'double', 'type' => 'string', 'media_id' => '?integer', 'metadata' => 'json-array', 'status' => 'string', 'logs' => 'string', 'created_by' => 'integer', 'updated_by' => 'integer', ]; public function __construct($data) { parent::__construct($data); if ($this->start_time && $this->duration) { $this->end_time = $this->start_time + $this->duration; } elseif ($this->start_time && $this->end_time) { $this->duration = $this->end_time - $this->duration; } } public function getPodcast(): ?Podcast { return (new PodcastModel())->getPodcastById($this->podcast_id); } public function getEpisode(): ?Episode { return (new EpisodeModel())->getEpisodeById($this->episode_id); } public function setMedia(string $filePath = null): static { if ($filePath === null || ($file = new File($filePath)) === null) { return $this; } if ($this->media_id !== 0) { $this->getMedia() ->setFile($file); $this->getMedia() ->updated_by = (int) user_id(); (new MediaModel('audio'))->updateMedia($this->getMedia()); } else { $media = new Audio([ 'file_path' => $filePath, 'language_code' => $this->getPodcast() ->language_code, 'uploaded_by' => user_id(), 'updated_by' => user_id(), ]); $media->setFile($file); $this->attributes['media_id'] = (new MediaModel())->saveMedia($media); } return $this; } public function getMedia(): Audio | Video { if ($this->media_id !== null && $this->media === null) { $this->media = (new MediaModel($this->type))->getMediaById($this->media_id); } return $this->media; } }