96 lines
3.4 KiB
PHP
96 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Rules\CountryCodeExists;
|
|
use App\Rules\DateYMD;
|
|
use App\Services\WorkingDays\WorkingDayDeterminerFactory;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
final class WorkingDay
|
|
{
|
|
public function __construct(
|
|
private readonly WorkingDayDeterminerFactory $workingDayDeterminerFactory
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @OA\Get(
|
|
* path="/api/v1/working-days/{countryCode}/{date}",
|
|
* summary="Get the working day status for a given date and country code",
|
|
* description="Returns whether the given date is a working day for the specified country code",
|
|
* @OA\Parameter(name="countryCode", in="path", description="The ISO 3166-1 alpha-2 country code"),
|
|
* @OA\Parameter(name="date", in="path", description="Date in YYYY-MM-DD format"),
|
|
* @OA\Response(
|
|
* response="200",
|
|
* description="Returns the working day status for the given date and country code",
|
|
* @OA\JsonContent(
|
|
* properties={
|
|
* @OA\Property(property="date", type="string", format="date"),
|
|
* @OA\Property(property="isWorkingDay", type="boolean"),
|
|
* }
|
|
* )
|
|
* ),
|
|
* @OA\Response(
|
|
* response="400",
|
|
* description="Invalid request parameters",
|
|
* @OA\JsonContent(
|
|
* type="object",
|
|
* properties={
|
|
* @OA\Property (
|
|
* type="object",
|
|
* property="errors",
|
|
* anyOf={
|
|
* @OA\Property(type="object", description="Error", properties={@OA\Property (property="date", type="string", example="Invalid date reason")}),
|
|
* @OA\Property(type="object", description="Error", properties={@OA\Property (property="countryCode", type="string", example="Invalid countryCode reason")}),
|
|
* }
|
|
* )
|
|
* })
|
|
* )
|
|
* )
|
|
*
|
|
*
|
|
* @param Request $request
|
|
* @param string $countryCode
|
|
* @param string $date
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getWorkingDays(Request $request, string $countryCode, string $date): \Illuminate\Http\JsonResponse
|
|
{
|
|
$validator = Validator::make(
|
|
[
|
|
'date' => $date,
|
|
'countryCode' => $countryCode,
|
|
],
|
|
[
|
|
'date' => ['required', new DateYMD()],
|
|
'countryCode' => ['required', 'string', 'min:2', 'max:2', new CountryCodeExists()],
|
|
]
|
|
);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json(['errors' => $validator->errors()], 400);
|
|
}
|
|
|
|
try {
|
|
$workingDayDeterminer = $this->workingDayDeterminerFactory->createForCountry($countryCode);
|
|
} catch (\InvalidArgumentException $e) {
|
|
return response()->json(['errors' => ['countryCode' => $e->getMessage()]], 400);
|
|
}
|
|
|
|
$date = \DateTimeImmutable::createFromFormat("Y-m-d", $date);
|
|
|
|
if ($date === false) {
|
|
return response()->json(['errors' => ['date' => ['validation.dateymdhis']]], 400);
|
|
}
|
|
|
|
return response()->json([
|
|
'date' => $date->format('Y-m-d'),
|
|
'isWorkingDay' => $workingDayDeterminer->isWorkingDay($date)
|
|
]);
|
|
}
|
|
}
|