2022-06-08 17:05:12 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2024-06-23 21:29:05 +01:00
|
|
|
use App\Enums\AnimeRatingEnum;
|
|
|
|
use App\Enums\MangaTypeEnum;
|
2022-06-08 17:05:12 +01:00
|
|
|
use App\Filters\FilterQueryString;
|
2023-01-21 01:53:37 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Jenssegers\Mongodb\Eloquent\Builder;
|
2024-06-23 21:29:05 +01:00
|
|
|
use Jikan\Helper\Constants;
|
2022-06-08 17:05:12 +01:00
|
|
|
|
|
|
|
class JikanApiModel extends \Jenssegers\Mongodb\Eloquent\Model
|
|
|
|
{
|
|
|
|
use FilterQueryString;
|
|
|
|
|
|
|
|
/**
|
2022-06-09 16:01:37 +01:00
|
|
|
* The list of parameters which can be used to filter the result-set from the database.
|
|
|
|
* The available field names and "order_by" is allowed as values. If "order_by" is specified then the field name
|
|
|
|
* from the "order_by" query string parameter will be used to sort the results.
|
2022-06-08 17:05:12 +01:00
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected array $filters = [];
|
2023-01-21 01:53:37 +00:00
|
|
|
|
|
|
|
/** @noinspection PhpUnused */
|
2024-06-23 21:29:05 +01:00
|
|
|
public function scopeRandom(Builder $query, int $numberOfRandomItems = 1, bool $sfw = false, bool $unapproved = false): Collection
|
2023-01-21 01:53:37 +00:00
|
|
|
{
|
2024-06-23 21:29:05 +01:00
|
|
|
return $query->raw(function(\Jenssegers\Mongodb\Collection $collection) use ($numberOfRandomItems, $sfw, $unapproved) {
|
|
|
|
$sfwFilter = [
|
|
|
|
'demographics.mal_id' => [
|
|
|
|
'$nin' => [
|
|
|
|
Constants::GENRE_ANIME_HENTAI,
|
|
|
|
Constants::GENRE_ANIME_EROTICA,
|
|
|
|
Constants::GENRE_MANGA_HENTAI,
|
|
|
|
Constants::GENRE_MANGA_EROTICA
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'rating' => ['$ne' => AnimeRatingEnum::rx()->label],
|
|
|
|
'type' => ['$ne' => MangaTypeEnum::doujin()->label],
|
|
|
|
'genres.mal_id' => ['$nin' => [
|
|
|
|
Constants::GENRE_ANIME_HENTAI,
|
|
|
|
Constants::GENRE_MANGA_HENTAI
|
|
|
|
]]
|
|
|
|
];
|
|
|
|
|
|
|
|
$pipelineParams = [
|
|
|
|
['$sample' => ['size' => $numberOfRandomItems]]
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($sfw && $unapproved) {
|
|
|
|
array_unshift($pipelineParams, [
|
|
|
|
'$match' => [
|
|
|
|
...$sfwFilter,
|
|
|
|
'approved' => false
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
} else if ($sfw) {
|
|
|
|
array_unshift($pipelineParams, ['$match' => $sfwFilter]);
|
|
|
|
} else if ($unapproved) {
|
|
|
|
array_unshift($pipelineParams, ['$match' => ['approved' => false]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $collection->aggregate($pipelineParams);
|
|
|
|
});
|
2023-01-21 01:53:37 +00:00
|
|
|
}
|
2022-06-08 17:05:12 +01:00
|
|
|
}
|