2020-06-19 11:59:25 +05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2020-07-05 12:26:49 +05:00
|
|
|
use App\Http\HttpHelper;
|
2020-06-19 11:59:25 +05:00
|
|
|
use Jikan\Jikan;
|
2020-07-05 12:26:49 +05:00
|
|
|
use Jikan\Request\Manga\MangaRequest;
|
2022-12-04 11:38:11 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2020-06-19 11:59:25 +05:00
|
|
|
|
2022-06-08 17:05:12 +01:00
|
|
|
class Manga extends JikanApiSearchableModel
|
2020-06-19 11:59:25 +05:00
|
|
|
{
|
2022-12-04 11:38:11 +00:00
|
|
|
use HasFactory;
|
|
|
|
|
2022-06-08 17:05:12 +01:00
|
|
|
// note that here we skip "score", "min_score", "max_score", "rating" and others because they need special logic
|
|
|
|
// to set the correct filtering on the ORM.
|
2022-06-20 22:35:14 +01:00
|
|
|
protected array $filters = ["order_by", "status", "type", "sort"];
|
2020-06-19 11:59:25 +05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
2022-10-03 23:20:39 +05:00
|
|
|
'mal_id', 'url', 'title', 'title_english', 'title_japanese', 'title_synonyms', 'titles', 'images', 'status', 'type', 'volumes', 'chapters', 'publishing', 'published', 'rank', 'score',
|
2021-12-27 20:18:25 +05:00
|
|
|
'scored_by', 'popularity', 'members', 'favorites', 'synopsis', 'background', 'related', 'genres', 'explicit_genres', 'themes', 'demographics', 'authors', 'serializations',
|
2020-06-19 11:59:25 +05:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The accessors to append to the model's array form.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-07-23 03:00:16 +05:00
|
|
|
protected $appends = [];
|
2020-06-19 11:59:25 +05:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The table associated with the model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $table = 'manga';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes excluded from the model's JSON form.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = [
|
2020-07-23 03:00:16 +05:00
|
|
|
'_id', 'expiresAt', 'request_hash'
|
2020-06-19 11:59:25 +05:00
|
|
|
];
|
|
|
|
|
2020-07-05 12:26:49 +05:00
|
|
|
public static function scrape(int $id)
|
|
|
|
{
|
|
|
|
$data = app('JikanParser')->getManga(new MangaRequest($id));
|
|
|
|
|
|
|
|
return HttpHelper::serializeEmptyObjectsControllerLevel(
|
|
|
|
json_decode(
|
|
|
|
app('SerializerV4')
|
|
|
|
->serialize($data, 'json'),
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-06-08 17:05:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the model to an index-able data array.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function toSearchableArray(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => (string) $this->mal_id,
|
|
|
|
'mal_id' => (string) $this->mal_id,
|
|
|
|
'start_date' => $this->convertToTimestamp($this->published['from']),
|
|
|
|
'end_date' => $this->convertToTimestamp($this->published['to']),
|
|
|
|
'title' => $this->title,
|
2022-10-03 20:04:39 +01:00
|
|
|
'title_transformed' => $this->simplifyStringForSearch($this->title),
|
2022-10-03 23:20:39 +05:00
|
|
|
'title_english' => $this->title_english ?? "",
|
2022-10-03 20:04:39 +01:00
|
|
|
'title_english_transformed' => $this->simplifyStringForSearch($this->title_english),
|
2022-06-08 17:05:12 +01:00
|
|
|
'title_japanese' => $this->title_japanese,
|
2022-10-03 20:04:39 +01:00
|
|
|
'title_japanese_transformed' => $this->simplifyStringForSearch($this->title_japanese),
|
2022-06-08 17:05:12 +01:00
|
|
|
'title_synonyms' => $this->title_synonyms,
|
|
|
|
'type' => $this->type,
|
|
|
|
'chapters' => $this->chapters,
|
|
|
|
'volumes' => $this->volumes,
|
|
|
|
'status' => $this->status,
|
|
|
|
'publishing' => $this->publishing,
|
|
|
|
'score' => $this->score,
|
|
|
|
'rank' => $this->rank,
|
|
|
|
'popularity' => $this->popularity,
|
|
|
|
'members' => $this->members,
|
|
|
|
'favorites' => $this->favorites,
|
|
|
|
'synopsis' => $this->synopsis,
|
|
|
|
'season' => $this->season,
|
|
|
|
'magazines' => $this->getMalIdsOfField($this->magazines),
|
|
|
|
'genres' => $this->getMalIdsOfField($this->genres),
|
|
|
|
'explicit_genres' => $this->getMalIdsOfField($this->explicit_genres)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getThemesAttribute(): array
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function typesenseQueryBy(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'title',
|
2022-10-03 23:20:39 +05:00
|
|
|
'title_transformed',
|
2022-06-08 17:05:12 +01:00
|
|
|
'title_english',
|
2022-10-03 23:20:39 +05:00
|
|
|
'title_english_transformed',
|
2022-06-08 17:05:12 +01:00
|
|
|
'title_japanese',
|
2022-10-03 23:20:39 +05:00
|
|
|
'title_japanese_transformed',
|
2022-06-08 17:05:12 +01:00
|
|
|
];
|
|
|
|
}
|
2022-06-18 15:03:50 +01:00
|
|
|
|
|
|
|
public function getTypeSenseQueryByWeights(): string|null
|
|
|
|
{
|
2022-10-03 23:20:39 +05:00
|
|
|
return "2,2,1,1,2,2";
|
2022-06-18 15:03:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns which fields the search index should sort on when searching
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
public function getSearchIndexSortBy(): array|null
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
2022-10-03 23:20:39 +05:00
|
|
|
"field" => "_text_match",
|
|
|
|
"direction" => "desc"
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"field" => "members",
|
|
|
|
"direction" => "desc"
|
|
|
|
],
|
2022-06-18 15:03:50 +01:00
|
|
|
];
|
|
|
|
}
|
2022-06-08 17:05:12 +01:00
|
|
|
}
|