jikan-rest/app/Character.php

102 lines
2.3 KiB
PHP
Raw Normal View History

2020-07-05 12:45:12 +05:00
<?php
namespace App;
use App\Concerns\FilteredByLetter;
2020-07-05 12:45:12 +05:00
use Jikan\Jikan;
use Jikan\Request\Character\CharacterRequest;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2020-07-05 12:45:12 +05:00
class Character extends JikanApiSearchableModel
2020-07-05 12:45:12 +05:00
{
use HasFactory, FilteredByLetter;
protected array $filters = ["order_by", "sort", "letter"];
2020-07-05 12:45:12 +05:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'mal_id', 'url', 'name', 'name_kanji', 'nicknames', 'about', 'member_favorites',
'images', 'animeography', 'mangaography', 'voice_actors',
'createdAt', 'modifiedAt'
2020-07-05 12:45:12 +05:00
];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
2020-07-14 18:27:12 +05:00
protected $appends = ['images', 'favorites'];
2020-07-05 12:45:12 +05:00
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'characters';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'_id', 'trailer_url', 'premiered', 'opening_themes', 'ending_themes', 'member_favorites'
2020-07-05 12:45:12 +05:00
];
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->displayNameFieldName = "name";
}
/** @noinspection PhpUnused */
2020-07-14 18:27:12 +05:00
public function getFavoritesAttribute()
{
return $this->attributes['member_favorites'];
}
/** @noinspection PhpUnused */
public function getImagesAttribute()
{
return $this->attributes['images'];
}
2020-07-05 12:45:12 +05:00
public static function scrape(int $id)
{
$data = app('JikanParser')->getCharacter(new CharacterRequest($id));
return json_decode(
app('SerializerV4')
->serialize($data, 'json'),
true
);
}
public function toSearchableArray(): array
{
return [
'id' => (string) $this->mal_id,
'mal_id' => (int) $this->mal_id,
'name' => $this->name,
'name_kanji' => $this->name_kanji,
'member_favorites' => $this->member_favorites
];
}
public function typesenseQueryBy(): array
{
return [
'name',
'name_kanji'
];
}
public function getTitleAttributeName(): string
{
return 'name';
}
2022-06-20 22:35:14 +01:00
}