Commit 8b931829 authored by Nikita Chernykh's avatar Nikita Chernykh
Browse files

Merge branch 'PTPS|Controller_5' into 'main'

make controller

See merge request !5
parents 0743d784 b89d0c56
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
<?php

namespace App\Actions;

use DateTimeImmutable;

class HowDaysToNYAction
{
    /**
     * Функция рассчитывает кол-во дней до нового года
     * @param DateTime $date дата от которой, необходимо рассчитать кол-во дней
     * @return int
     */
    public function act(DateTimeImmutable $date): int
    {
        $dateOfNY = $date->modify('first day of Jan +1 year');

        return (int) $dateOfNY->diff($date)->format('%a');
    }
}
 No newline at end of file
+25 −0
Original line number Diff line number Diff line
<?php

namespace App\Controller;

use App\Actions\HowDaysToNYAction;
use App\Requests\BeforeNYDateRequest;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class HowDaysToNYController extends AbstractController
{
    /**
     * Контролер рассчитывает кол-во дней до нового года используюя howDaysToNy
     * @param BeforeNYDateRequest $request
     * @param HowDaysToNYAction $action
     * @return Response
     */
    #[Route('/howdaystony', name: 'app_how_days_to_n_y')]
    public function index(BeforeNYDateRequest $request, HowDaysToNYAction $action): Response
    {
        return new JsonResponse($action->act($request->serialise()));
    }
}
+19 −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\Type;

class BeforeNYDateRequest extends BaseRequest
{
    #[Date()]
    #[Type('string')]
    public $date;

    public function serialise(): mixed
    {
        return new DateTimeImmutable($this->date);
    }
}