koel/app/Models/Song.php

158 lines
4.1 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Models;
use App\Builders\SongBuilder;
2022-07-27 15:32:36 +00:00
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2015-12-13 04:42:28 +00:00
use Illuminate\Database\Eloquent\Model;
2017-08-05 16:56:11 +00:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
2022-08-01 10:42:33 +00:00
use Illuminate\Support\Str;
2020-12-23 10:53:00 +00:00
use Laravel\Scout\Searchable;
2015-12-13 04:42:28 +00:00
/**
2020-12-22 20:11:22 +00:00
* @property string $path
* @property string $title
* @property Album $album
* @property Artist $artist
2022-10-12 09:27:35 +00:00
* @property Artist $album_artist
2020-12-22 20:11:22 +00:00
* @property float $length
* @property string $lyrics
* @property int $track
* @property int $disc
* @property int $album_id
* @property int|null $year
* @property string $genre
2020-12-22 20:11:22 +00:00
* @property string $id
* @property int $artist_id
* @property int $mtime
2022-07-27 15:32:36 +00:00
* @property ?bool $liked Whether the song is liked by the current user (dynamically calculated)
* @property ?int $play_count The number of times the song has been played by the current user (dynamically calculated)
* @property Carbon $created_at
* @property array<mixed> $s3_params
2015-12-13 04:42:28 +00:00
*/
class Song extends Model
{
use HasFactory;
2020-12-23 10:53:00 +00:00
use Searchable;
2022-08-01 10:42:33 +00:00
use SupportsDeleteWhereValueNotIn;
2016-09-26 07:32:16 +00:00
2022-08-01 10:42:33 +00:00
public const ID_REGEX = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
2021-06-05 10:47:56 +00:00
public $incrementing = false;
2015-12-13 04:42:28 +00:00
protected $guarded = [];
protected $hidden = ['updated_at', 'path', 'mtime'];
2015-12-13 04:42:28 +00:00
protected $casts = [
2015-12-21 02:18:00 +00:00
'length' => 'float',
2016-03-22 08:22:39 +00:00
'mtime' => 'int',
2016-03-28 13:18:09 +00:00
'track' => 'int',
'disc' => 'int',
];
2020-09-06 18:21:39 +00:00
protected $keyType = 'string';
2022-08-01 10:42:33 +00:00
protected static function booted(): void
{
static::creating(static fn (self $song) => $song->id = Str::uuid()->toString());
}
public static function query(): SongBuilder
{
return parent::query();
}
public function newEloquentBuilder($query): SongBuilder
{
return new SongBuilder($query);
}
2021-06-05 10:47:56 +00:00
public function artist(): BelongsTo
{
return $this->belongsTo(Artist::class);
}
public function album(): BelongsTo
{
return $this->belongsTo(Album::class);
}
2022-10-12 09:27:35 +00:00
public function album_artist(): BelongsTo // @phpcs:ignore
{
return $this->album->artist();
}
2021-06-05 10:47:56 +00:00
public function playlists(): BelongsToMany
{
return $this->belongsToMany(Playlist::class);
}
public function interactions(): HasMany
{
return $this->hasMany(Interaction::class);
}
protected function title(): Attribute
2015-12-13 04:42:28 +00:00
{
return new Attribute(
get: fn (?string $value) => $value ?: pathinfo($this->path, PATHINFO_FILENAME),
set: static fn (string $value) => html_entity_decode($value)
);
2015-12-13 04:42:28 +00:00
}
protected function lyrics(): Attribute
{
$normalizer = static function (?string $value): string {
// Since we're displaying the lyrics using <pre>, replace breaks with newlines and strip all tags.
$value = strip_tags(preg_replace('#<br\s*/?>#i', PHP_EOL, $value));
// also remove the timestamps that often come with LRC files
return preg_replace('/\[\d{2}:\d{2}.\d{2}]\s*/m', '', $value);
};
return new Attribute(get: $normalizer, set: $normalizer);
}
protected function s3Params(): Attribute
2022-06-10 10:47:46 +00:00
{
return Attribute::get(function (): ?array {
if (!preg_match('/^s3:\\/\\/(.*)/', $this->path, $matches)) {
return null;
}
[$bucket, $key] = explode('/', $matches[1], 2);
return compact('bucket', 'key');
});
2022-06-10 10:47:46 +00:00
}
public static function getPathFromS3BucketAndKey(string $bucket, string $key): string
2015-12-13 04:42:28 +00:00
{
return "s3://$bucket/$key";
2015-12-13 04:42:28 +00:00
}
2016-04-17 15:38:06 +00:00
2020-12-23 10:53:00 +00:00
/** @return array<mixed> */
public function toSearchableArray(): array
{
$array = [
'id' => $this->id,
'title' => $this->title,
];
if (!$this->artist->is_unknown && !$this->artist->is_various) {
$array['artist'] = $this->artist->name;
}
return $array;
}
2020-12-22 20:11:22 +00:00
public function __toString(): string
2017-06-24 20:46:55 +00:00
{
return $this->id;
}
2015-12-13 04:42:28 +00:00
}