jikan-rest/app/Club.php

107 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2020-07-11 11:53:01 +05:00
<?php
namespace App;
use App\Concerns\FilteredByLetter;
use App\Enums\ClubCategoryEnum;
use App\Enums\ClubTypeEnum;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2020-07-11 11:53:01 +05:00
use Jikan\Request\Club\ClubRequest;
class Club extends JikanApiSearchableModel
2020-07-11 11:53:01 +05:00
{
use FilteredByLetter, HasFactory;
protected array $filters = ["order_by", "sort", "letter", "category", "type"];
2020-07-11 11:53:01 +05:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'mal_id', 'url', 'images', 'name', 'members', 'category', 'created', 'access', 'anime', 'manga', 'created_at', 'updated_at', 'characters', 'staff'
2020-07-11 11:53:01 +05:00
];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['images'];
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'clubs';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'_id', 'request_hash', 'expiresAt'
2020-07-11 11:53:01 +05:00
];
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->displayNameFieldName = "name";
}
/** @noinspection PhpUnused */
public function filterByCategory(\Laravel\Scout\Builder|\Illuminate\Database\Eloquent\Builder $query, ClubCategoryEnum $value): \Laravel\Scout\Builder|\Illuminate\Database\Eloquent\Builder
{
return $query->where("category", $value->label);
}
/** @noinspection PhpUnused */
public function filterByType(\Laravel\Scout\Builder|\Illuminate\Database\Eloquent\Builder $query, ClubTypeEnum $value): \Laravel\Scout\Builder|\Illuminate\Database\Eloquent\Builder
{
return $query->where("access", $value->label);
}
/** @noinspection PhpUnused */
public function getImagesAttribute()
{
return $this->attributes['images'];
}
2020-07-11 11:53:01 +05:00
public static function scrape(int $id)
{
$data = app('JikanParser')->getClub(new ClubRequest($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,
2023-01-28 21:13:57 +00:00
'name' => $this->name,
'category' => $this->category,
'created' => $this->convertToTimestamp($this->created),
2023-01-28 21:13:57 +00:00
'access' => $this->type,
'members' => $this->members
];
}
public function typesenseQueryBy(): array
{
2023-01-28 21:13:57 +00:00
return ['name'];
}
public function getTitleAttributeName(): string
{
2023-09-17 20:38:27 +00:00
return 'name';
}
}