-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArtist.php
108 lines (90 loc) · 2.76 KB
/
Artist.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace App\Models;
use App\Facades\Util;
use App\Traits\SupportsDeleteWhereIDsNotIn;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
/**
* @property int $id
* @property string $name
* @property string $image
* @property bool $is_unknown
* @property bool $is_various
* @property Collection $songs
* @property bool $has_image
*
* @method static self find(int $id)
* @method static self firstOrCreate(array $where, array $params = [])
*/
class Artist extends Model
{
use SupportsDeleteWhereIDsNotIn;
const UNKNOWN_ID = 1;
const UNKNOWN_NAME = 'Unknown Artist';
const VARIOUS_ID = 2;
const VARIOUS_NAME = 'Various Artists';
protected $guarded = ['id'];
protected $hidden = ['created_at', 'updated_at'];
public function albums(): HasMany
{
return $this->hasMany(Album::class);
}
/**
* An artist can have many songs.
* Unless he is Rick Astley.
*/
public function songs(): HasManyThrough
{
return $this->hasManyThrough(Song::class, Album::class);
}
public function getIsUnknownAttribute(): bool
{
return $this->id === self::UNKNOWN_ID;
}
public function getIsVariousAttribute(): bool
{
return $this->id === self::VARIOUS_ID;
}
public static function getVariousArtist(): self
{
return static::find(self::VARIOUS_ID);
}
/**
* 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 ?: self::UNKNOWN_NAME);
}
/**
* Get an Artist object from their name.
* If such is not found, a new artist will be created.
*/
public static function get(string $name): self
{
// Remove the BOM from UTF-8/16/32, as it will mess up the database constraints.
if ($encoding = Util::detectUTFEncoding($name)) {
$name = mb_convert_encoding($name, 'UTF-8', $encoding);
}
$name = trim($name) ?: self::UNKNOWN_NAME;
return static::firstOrCreate(compact('name'));
}
/**
* Turn the image name into its absolute URL.
*/
public function getImageAttribute(?string $value): ?string
{
return $value ? app()->staticUrl("public/img/artists/$value") : null;
}
public function getHasImageAttribute(): bool
{
$image = array_get($this->attributes, 'image');
if (!$image) {
return false;
}
return file_exists(public_path("public/img/artists/$image"));
}
}