Commit 48b0e95d authored by Александр Плохих's avatar Александр Плохих 🌔
Browse files

add diff days action | controller | req

parent 19237b0d
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
<?php

namespace App\Actions;

use DateTimeImmutable;

class DiffDaysAction
{
    /**
     * Вернет кол-во дней между датами
     * @param DateTimeImmutable $dateStart дата начала
     * @param DateTimeImmutable $dateEnd дата окончания
     * @return int
     * */
    public function act(
        DateTimeImmutable $dateStart,
        DateTimeImmutable $dateEnd
    ): int {
        return (int) $dateStart->diff($dateEnd)->format('%a');
    }
}
+7 −7
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

namespace App\Controller;

use App\Actions\DiffDaysAction;
use App\Requests\DiffDaysRequest;
use DateTimeImmutable;
use HttpResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -14,16 +16,14 @@ class DiffDaysController extends AbstractController
{
    /**
     * Контроллер вернет кол-во дней между датами
     * @param Request $request
     * @param DiffDaysRequest $request
     * @param DiffDaysAction $action
     * @return Response
     */
    #[Route('/diff/days', name: 'app_diff_days', methods: ['POST'])]
    public function index(Request $request): Response
    public function index(DiffDaysRequest $request, DiffDaysAction $action): Response
    {
        $dateStart = new DateTimeImmutable($request->toArray()['date_start']);
        $dateEnd = new DateTimeImmutable($request->toArray()['date_end']);

        return new JsonResponse(["interval"=>($dateStart->diff($dateEnd)->format("%a"))]
            , Response::HTTP_OK);
        $array = $request->serialise();
        return new JsonResponse($action->act($array[0], $array[1]));
    }
}
+29 −0
Original line number Diff line number Diff line
<?php

namespace App\Requests;

use DateTimeImmutable;
use Symfony\Component\Validator\Constraints\Date;
use Symfony\Component\Validator\Constraints\NotBlank;

class DiffDaysRequest extends BaseRequest
{
    #[Date]
    #[NotBlank]
    public $startDate;

    #[Date]
    #[NotBlank]
    public $endDate;

    /**
     * @return mixed
     */
    public function serialise(): mixed
    {
        return [
            'startDate' => new DateTimeImmutable($this->startDate),
            'endDate' => new DateTimeImmutable($this->endDate),
        ];
    }
}
 No newline at end of file