2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2015-12-19 16:36:44 +00:00
|
|
|
use App\Facades\Lastfm;
|
2015-12-17 17:56:48 +00:00
|
|
|
use App\Facades\Util;
|
2015-12-17 18:00:42 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
/**
|
2015-12-19 16:36:44 +00:00
|
|
|
* @property int id The model ID
|
|
|
|
* @property string name The artist name
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
class Artist extends Model
|
|
|
|
{
|
|
|
|
const UNKNOWN_ID = 1;
|
|
|
|
const UNKNOWN_NAME = 'Unknown Artist';
|
|
|
|
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
|
|
|
|
protected $hidden = ['created_at', 'updated_at'];
|
|
|
|
|
|
|
|
public function albums()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Album::class);
|
|
|
|
}
|
|
|
|
|
2015-12-16 05:03:48 +00:00
|
|
|
/**
|
|
|
|
* Sometimes the tags extracted from getID3 are HTML entity encoded.
|
|
|
|
* This makes sure they are always sane.
|
|
|
|
*
|
|
|
|
* @param $value
|
2015-12-17 17:56:48 +00:00
|
|
|
*
|
|
|
|
* @return string
|
2015-12-16 05:03:48 +00:00
|
|
|
*/
|
2015-12-14 13:22:39 +00:00
|
|
|
public function getNameAttribute($value)
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2015-12-16 05:03:48 +00:00
|
|
|
return html_entity_decode($value ?: self::UNKNOWN_NAME);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an Artist object from their name.
|
|
|
|
* If such is not found, a new artist will be created.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return Artist
|
|
|
|
*/
|
|
|
|
public static function get($name)
|
|
|
|
{
|
2015-12-17 17:56:48 +00:00
|
|
|
// Remove the BOM from UTF-8/16/32, as it will mess up the database constraints.
|
|
|
|
if ($encoding = Util::detectUTFEncoding($name)) {
|
2015-12-18 02:11:32 +00:00
|
|
|
$name = mb_convert_encoding($name, 'UTF-8', $encoding);
|
2015-12-17 17:56:48 +00:00
|
|
|
}
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
$name = trim($name) ?: self::UNKNOWN_NAME;
|
|
|
|
|
|
|
|
return self::firstOrCreate(compact('name'), compact('name'));
|
|
|
|
}
|
2015-12-19 16:36:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get extra information about the artist from Last.fm.
|
|
|
|
*
|
|
|
|
* @return array|false
|
|
|
|
*/
|
|
|
|
public function getInfo()
|
|
|
|
{
|
|
|
|
if ($this->id === self::UNKNOWN_ID) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$info = Lastfm::getArtistInfo($this->name);
|
|
|
|
|
|
|
|
// TODO: Copy the artist's image for our local use.
|
|
|
|
|
|
|
|
return $info;
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|