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 @@ + - {% for value in menu %} - {% if value.depth == 0 %} -

{{ value.name }}

- {% for val in value.submenu %} -
  • {{ val.name }}
  • - {% endfor %} - {% endif %} - {% endfor %} - +

    Кол-во дней до НГ: {{ count }}

    {% endblock %} \ No newline at end of file