*/ 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', ]; /** * Saves the person avatar in `public/media/persons/` */ public function setAvatar(UploadedFile | File $file = null): static { if (! $file instanceof File || ($file instanceof UploadedFile && ! $file->isValid())) { return $this; } if (array_key_exists('avatar_id', $this->attributes) && $this->attributes['avatar_id'] !== null) { $this->getAvatar() ->setFile($file); $this->getAvatar() ->updated_by = (int) user_id(); (new MediaModel('image'))->updateMedia($this->getAvatar()); } else { $avatar = new Image([ 'file_key' => 'persons/' . $this->attributes['unique_name'] . '.' . $file->getExtension(), 'sizes' => config('Images') ->personAvatarSizes, 'uploaded_by' => user_id(), 'updated_by' => user_id(), ]); $avatar->setFile($file); $this->attributes['avatar_id'] = (new MediaModel('image'))->saveMedia($avatar); } return $this; } public function getAvatar(): ?Image { if ($this->avatar_id === null) { return null; } if (! $this->avatar instanceof Image) { $this->avatar = (new MediaModel('image'))->getMediaById($this->avatar_id); } return $this->avatar; } /** * @return object[] */ public function getRoles(): array { if ($this->attributes['podcast_id'] === null) { throw new RuntimeException('Person must have a podcast_id before getting roles.'); } if ($this->roles === null) { $this->roles = (new PersonModel())->getPersonRoles( $this->id, (int) $this->attributes['podcast_id'], array_key_exists('episode_id', $this->attributes) ? (int) $this->attributes['episode_id'] : null ); } return $this->roles; } }