diff --git a/src/Action/Functions.php b/src/Action/Functions.php index dad8f178b82453b88c5164c0869b905af9b4bbbe..cf39542d7c5af531de7b17e84a0cc09eeda01c38 100644 --- a/src/Action/Functions.php +++ b/src/Action/Functions.php @@ -4,6 +4,9 @@ declare(strict_types=1); namespace App\Action; +use Exception; +use DateTimeImmutable; + class Functions { /** @@ -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 diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index d935b915124a1acc54eb63fdc5f61aecabacb48c..1b2695df759e9b8dc5cd4adfca949a12cb252a20 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -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; @@ -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]); + } } diff --git a/src/Requests/HowDaysToNyRequest.php b/src/Requests/HowDaysToNyRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..c786942ac2e6cf8c00e98738d45f4e13ed52b3bb --- /dev/null +++ b/src/Requests/HowDaysToNyRequest.php @@ -0,0 +1,11 @@ +<?php + +namespace App\Requests; + +use Symfony\Component\Validator\Constraints as Assert; + +class HowDaysToNyRequest extends BaseRequest +{ + #[Assert\Type('date')] + public $date; +} diff --git a/templates/home.html.twig b/templates/home.html.twig index 9baaf00b56202c0678d0032d0311ab647a9fe3a5..6ce69c6ff8ec4817cb0d43c5b94b8741627c4edc 100644 --- a/templates/home.html.twig +++ b/templates/home.html.twig @@ -1,12 +1,3 @@ {% 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