'integer']; protected $appends = ['is_compilation']; /** * Get an album using some provided information. * If such is not found, a new album will be created using the information. */ public static function getOrCreate(Artist $artist, ?string $name = null, bool $isCompilation = false): self { // If this is a compilation album, its artist must be "Various Artists" if ($isCompilation) { $artist = Artist::getVariousArtist(); } return static::firstOrCreate([ 'artist_id' => $artist->id, 'name' => trim($name) ?: self::UNKNOWN_NAME, ]); } public function artist(): BelongsTo { return $this->belongsTo(Artist::class); } public function songs(): HasMany { return $this->hasMany(Song::class); } public function getIsUnknownAttribute(): bool { return $this->id === self::UNKNOWN_ID; } public function setCoverAttribute(?string $value): void { $this->attributes['cover'] = $value ?: self::UNKNOWN_COVER; } public function getCoverAttribute(?string $value): string { return album_cover_url($value ?: self::UNKNOWN_COVER); } public function getHasCoverAttribute(): bool { $cover = array_get($this->attributes, 'cover'); if (!$cover) { return false; } if ($cover === self::UNKNOWN_COVER) { return false; } return file_exists(album_cover_path($cover)); } public function getCoverPathAttribute(): ?string { $cover = array_get($this->attributes, 'cover'); return $cover ? album_cover_path($cover) : null; } /** * Sometimes the tags extracted from getID3 are HTML entity encoded. * This makes sure they are always sane. */ public function getNameAttribute(string $value): string { return html_entity_decode($value); } public function getIsCompilationAttribute(): bool { return $this->artist_id === Artist::VARIOUS_ID; } public function getThumbnailNameAttribute(): ?string { if (!$this->has_cover) { return null; } $parts = pathinfo($this->cover_path); return sprintf('%s_thumb.%s', $parts['filename'], $parts['extension']); } public function getThumbnailPathAttribute(): ?string { return $this->thumbnail_name ? album_cover_path($this->thumbnail_name) : null; } public function getThumbnailAttribute(): ?string { return $this->thumbnail_name ? album_cover_url($this->thumbnail_name) : null; } /** @return array */ public function toSearchableArray(): array { $array = [ 'id' => $this->id, 'name' => $this->name, ]; if (!$this->artist->is_unknown && !$this->artist->is_various) { $array['artist'] = $this->artist->name; } return $array; } }