Commit 217fc744 authored by Nikita Chernykh's avatar Nikita Chernykh
Browse files

Merge branch 'PTPS_Controller_5' into 'main'

Ptps controller 5

See merge request !5
parents 2de0012a ea083e60
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -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
+15 −1
Original line number Diff line number Diff line
@@ -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]);
    }
}
+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;
}
+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