koel/app/Models/Song.php

147 lines
4 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Models;
2017-08-05 16:56:11 +00:00
use Illuminate\Database\Eloquent\Builder;
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-06-10 10:47:46 +00:00
use Illuminate\Database\Query\JoinClause;
2018-08-29 06:30:39 +00:00
use Illuminate\Support\Collection;
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
* @property float $length
* @property string $lyrics
* @property int $track
* @property int $disc
* @property int $album_id
* @property string $id
* @property int $artist_id
* @property int $mtime
2020-12-22 23:01:49 +00:00
* @property int $contributing_artist_id
2019-08-05 10:57:36 +00:00
*
2019-08-05 10:56:48 +00:00
* @method static self updateOrCreate(array $where, array $params)
* @method static Builder select(string $string)
* @method static Builder inDirectory(string $path)
* @method static self first()
2021-06-05 10:47:56 +00:00
* @method static Builder orderBy(...$args)
2020-09-06 18:21:39 +00:00
* @method static int count()
* @method static self|Collection|null find($id)
* @method static Builder take(int $count)
2022-06-10 10:47:46 +00:00
* @method static float|int sum(string $column)
2015-12-13 04:42:28 +00:00
*/
class Song extends Model
{
use HasFactory;
2020-12-23 10:53:00 +00:00
use Searchable;
2016-09-26 07:32:16 +00:00
use SupportsDeleteWhereIDsNotIn;
use SupportsS3;
2016-09-26 07:32:16 +00:00
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';
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);
}
public function playlists(): BelongsToMany
{
return $this->belongsToMany(Playlist::class);
}
public function interactions(): HasMany
{
return $this->hasMany(Interaction::class);
}
2016-02-02 07:47:00 +00:00
/**
* Scope a query to only include songs in a given directory.
*/
2018-08-24 15:27:19 +00:00
public function scopeInDirectory(Builder $query, string $path): Builder
2016-02-02 07:47:00 +00:00
{
// Make sure the path ends with a directory separator.
2020-12-22 20:11:22 +00:00
$path = rtrim(trim($path), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
2016-02-02 07:47:00 +00:00
return $query->where('path', 'LIKE', "$path%");
}
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
}
2022-06-10 10:47:46 +00:00
public static function withMeta(User $scopedUser, ?Builder $query = null): Builder
{
$query ??= static::query();
return $query
->with('artist', 'album', 'album.artist')
->leftJoin('interactions', static function (JoinClause $join) use ($scopedUser): void {
$join->on('interactions.song_id', '=', 'songs.id')
->where('interactions.user_id', $scopedUser->id);
})
->join('albums', 'songs.album_id', '=', 'albums.id')
->join('artists', 'songs.artist_id', '=', 'artists.id')
->select(
'songs.*',
'albums.name',
'artists.name',
'interactions.liked',
'interactions.play_count'
);
}
public function scopeWithMeta(Builder $query, User $scopedUser): Builder
2015-12-13 04:42:28 +00:00
{
2022-06-10 10:47:46 +00:00
return static::withMeta($scopedUser, $query);
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
}