Skip to content
Snippets Groups Projects
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
No related branches found
No related tags found
1 merge request!5make controller
<?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
<?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()));
}
}
<?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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment