2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2021-10-08 16:23:45 +00:00
|
|
|
use App\Casts\SmartPlaylistRulesCast;
|
|
|
|
use App\Values\SmartPlaylistRuleGroup;
|
2022-08-10 14:56:01 +00:00
|
|
|
use Carbon\Carbon;
|
2022-07-18 11:00:37 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2020-11-14 16:57:25 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2015-12-14 13:22:39 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2017-08-05 18:55:02 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2021-10-08 16:23:45 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2021-01-05 16:52:16 +00:00
|
|
|
use Laravel\Scout\Searchable;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-08-03 10:42:11 +00:00
|
|
|
/**
|
2020-12-23 10:53:00 +00:00
|
|
|
* @property int $id
|
2022-08-10 14:56:01 +00:00
|
|
|
* @property string $name
|
|
|
|
* @property bool $is_smart
|
|
|
|
* @property int $user_id
|
|
|
|
* @property User $user
|
|
|
|
* @property ?string $folder_id
|
|
|
|
* @property ?PlaylistFolder $folder
|
|
|
|
* @property Collection|array<array-key, Song> $songs
|
2022-07-18 11:00:37 +00:00
|
|
|
* @property Collection|array<array-key, SmartPlaylistRuleGroup> $rule_groups
|
|
|
|
* @property Collection|array<array-key, SmartPlaylistRuleGroup> $rules
|
2022-08-10 14:56:01 +00:00
|
|
|
* @property Carbon $created_at
|
2016-08-03 10:42:11 +00:00
|
|
|
*/
|
2015-12-13 04:42:28 +00:00
|
|
|
class Playlist extends Model
|
|
|
|
{
|
2021-01-05 16:52:16 +00:00
|
|
|
use Searchable;
|
2020-11-14 16:57:25 +00:00
|
|
|
use HasFactory;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
protected $hidden = ['user_id', 'created_at', 'updated_at'];
|
|
|
|
protected $guarded = ['id'];
|
2021-10-08 16:23:45 +00:00
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
protected $casts = [
|
2021-10-08 16:23:45 +00:00
|
|
|
'rules' => SmartPlaylistRulesCast::class,
|
2015-12-13 04:42:28 +00:00
|
|
|
];
|
2021-10-08 16:23:45 +00:00
|
|
|
|
2018-11-25 21:21:46 +00:00
|
|
|
protected $appends = ['is_smart'];
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function songs(): BelongsToMany
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
return $this->belongsToMany(Song::class);
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function user(): BelongsTo
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
2018-11-03 23:25:08 +00:00
|
|
|
|
2022-08-10 14:56:01 +00:00
|
|
|
public function folder(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(PlaylistFolder::class);
|
|
|
|
}
|
|
|
|
|
2022-07-18 11:00:37 +00:00
|
|
|
protected function isSmart(): Attribute
|
2018-11-03 23:25:08 +00:00
|
|
|
{
|
2022-07-18 11:00:37 +00:00
|
|
|
return Attribute::get(fn (): bool => $this->rule_groups->isNotEmpty());
|
2021-10-08 16:23:45 +00:00
|
|
|
}
|
|
|
|
|
2022-07-18 11:00:37 +00:00
|
|
|
protected function ruleGroups(): Attribute
|
2021-10-08 16:23:45 +00:00
|
|
|
{
|
2022-07-18 11:00:37 +00:00
|
|
|
// aliasing the attribute to avoid confusion
|
|
|
|
return Attribute::get(fn () => $this->rules);
|
2018-11-03 23:25:08 +00:00
|
|
|
}
|
2021-01-05 16:52:16 +00:00
|
|
|
|
|
|
|
/** @return array<mixed> */
|
|
|
|
public function toSearchableArray(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => $this->id,
|
|
|
|
'name' => $this->name,
|
|
|
|
];
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|