mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
- added a new parameter: "continuing" - continuing items from previous seasons are now excluded by default - should fix #521
61 lines
1.7 KiB
PHP
61 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Features;
|
|
|
|
use App\Contracts\RequestHandler;
|
|
use App\Dto\QueryCurrentAnimeSeasonCommand;
|
|
use App\Enums\AnimeSeasonEnum;
|
|
use App\Enums\AnimeTypeEnum;
|
|
use Exception;
|
|
use Illuminate\Contracts\Database\Query\Builder;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
/**
|
|
* @implements RequestHandler<QueryCurrentAnimeSeasonCommand, JsonResponse>
|
|
*/
|
|
final class QueryCurrentAnimeSeasonHandler extends QueryAnimeSeasonHandlerBase
|
|
{
|
|
public function requestClass(): string
|
|
{
|
|
return QueryCurrentAnimeSeasonCommand::class;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
private function getCurrentSeason() : array
|
|
{
|
|
$date = new \DateTime('now', new \DateTimeZone('Asia/Tokyo'));
|
|
|
|
$year = (int) $date->format('Y');
|
|
$month = (int) $date->format('n');
|
|
|
|
return match ($month) {
|
|
1, 2, 3 => [AnimeSeasonEnum::winter(), $year],
|
|
4, 5, 6 => [AnimeSeasonEnum::spring(), $year],
|
|
7, 8, 9 => [AnimeSeasonEnum::summer(), $year],
|
|
10, 11, 12 => [AnimeSeasonEnum::fall(), $year],
|
|
default => throw new Exception('Could not generate seasonal string'),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
protected function getSeasonItems($request, ?AnimeTypeEnum $type): Builder
|
|
{
|
|
[$season, $year] = $this->getCurrentSeason();
|
|
/**
|
|
* @var Carbon $from
|
|
* @var Carbon $to
|
|
*/
|
|
[$from, $to] = $this->getSeasonRange($year, $season);
|
|
$premiered = ucfirst($season)." {$year}";
|
|
$includeContinuingItems = $request->continuing;
|
|
|
|
return $this->repository->getItemsBySeason($from, $to, $type, $premiered, $includeContinuingItems);
|
|
}
|
|
}
|