Loading src/Action/Functions.php +16 −0 Original line number Diff line number Diff line Loading @@ -4,6 +4,9 @@ declare(strict_types=1); namespace App\Action; use Exception; use DateTimeImmutable; class Functions { /** Loading Loading @@ -84,4 +87,17 @@ class Functions } return $result; } /** * Функция рассчитывает кол-во дней до нового года * @param DateTimeImmutable $date дата от которой, необходимо рассчитать кол-во дней * @return int * @throws Exception */ public function howDaysToNy(DateTimeImmutable $date): int { $endYear = date("Y-12-31", date_timestamp_get($date)); $dateInterval = date_diff(new DateTimeImmutable($endYear), $date); return (int)$dateInterval->format("%a") + 1; } } No newline at end of file src/Controller/HomeController.php +15 −1 Original line number Diff line number Diff line Loading @@ -7,8 +7,10 @@ use App\Requests\{ SortPriceRequest, SearchRequest, UniqElementsRequest, MenuRequest MenuRequest, HowDaysToNyRequest }; use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; Loading Loading @@ -46,4 +48,16 @@ class HomeController extends AbstractController $result = $this->functions->prepareMenu($request->getRequest()->toArray()['items']); return $this->json($result); } #[Route('/howDaysToNy', name: 'howDaysToNy', methods: ['GET'])] public function howDaysToNy(HowDaysToNyRequest $request): Response { $date = $request->getRequest()->get('date'); try { $result = $this->functions->howDaysToNy(new DateTimeImmutable($date)); } catch (\Exception $e) { return new Response($e->getMessage()); } return $this->json(["Days before NY:" => $result]); } } src/Requests/HowDaysToNyRequest.php 0 → 100644 +11 −0 Original line number Diff line number Diff line <?php namespace App\Requests; use Symfony\Component\Validator\Constraints as Assert; class HowDaysToNyRequest extends BaseRequest { #[Assert\Type('date')] public $date; } templates/home.html.twig +1 −10 Original line number Diff line number Diff line {% block body %} <ul> {% for value in menu %} {% if value.depth == 0 %} <h2>{{ value.name }}</h2> {% for val in value.submenu %} <li>{{ val.name }}</li> {% endfor %} {% endif %} {% endfor %} </ul> <h1>Кол-во дней до НГ: {{ count }}</h1> {% endblock %} No newline at end of file Loading
src/Action/Functions.php +16 −0 Original line number Diff line number Diff line Loading @@ -4,6 +4,9 @@ declare(strict_types=1); namespace App\Action; use Exception; use DateTimeImmutable; class Functions { /** Loading Loading @@ -84,4 +87,17 @@ class Functions } return $result; } /** * Функция рассчитывает кол-во дней до нового года * @param DateTimeImmutable $date дата от которой, необходимо рассчитать кол-во дней * @return int * @throws Exception */ public function howDaysToNy(DateTimeImmutable $date): int { $endYear = date("Y-12-31", date_timestamp_get($date)); $dateInterval = date_diff(new DateTimeImmutable($endYear), $date); return (int)$dateInterval->format("%a") + 1; } } No newline at end of file
src/Controller/HomeController.php +15 −1 Original line number Diff line number Diff line Loading @@ -7,8 +7,10 @@ use App\Requests\{ SortPriceRequest, SearchRequest, UniqElementsRequest, MenuRequest MenuRequest, HowDaysToNyRequest }; use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; Loading Loading @@ -46,4 +48,16 @@ class HomeController extends AbstractController $result = $this->functions->prepareMenu($request->getRequest()->toArray()['items']); return $this->json($result); } #[Route('/howDaysToNy', name: 'howDaysToNy', methods: ['GET'])] public function howDaysToNy(HowDaysToNyRequest $request): Response { $date = $request->getRequest()->get('date'); try { $result = $this->functions->howDaysToNy(new DateTimeImmutable($date)); } catch (\Exception $e) { return new Response($e->getMessage()); } return $this->json(["Days before NY:" => $result]); } }
src/Requests/HowDaysToNyRequest.php 0 → 100644 +11 −0 Original line number Diff line number Diff line <?php namespace App\Requests; use Symfony\Component\Validator\Constraints as Assert; class HowDaysToNyRequest extends BaseRequest { #[Assert\Type('date')] public $date; }
templates/home.html.twig +1 −10 Original line number Diff line number Diff line {% block body %} <ul> {% for value in menu %} {% if value.depth == 0 %} <h2>{{ value.name }}</h2> {% for val in value.submenu %} <li>{{ val.name }}</li> {% endfor %} {% endif %} {% endfor %} </ul> <h1>Кол-во дней до НГ: {{ count }}</h1> {% endblock %} No newline at end of file