Commit e94777b5 authored by Akex's avatar Akex Committed by Александр Плохих
Browse files

make controller

parent 4ebf14ce
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
<?php

namespace App\Controller;

use DateTimeImmutable;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use function App\Repository\howDaysToNy;

class HowDaysToNYController extends AbstractController
{
    /**
     * Контролер рассчитывает кол-во дней до нового года используюя howDaysToNy
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    #[Route('/howdaystony', name: 'app_how_days_to_n_y')]
    public function index(Request $request): Response
    {
        $date = $request->toArray()['date'];

        $return = howDaysToNy(new DateTimeImmutable($date, null));

        return new JsonResponse($return, Response::HTTP_OK);
    }
}
+19 −0
Original line number Diff line number Diff line
<?php
namespace App\Repository;

use DateTime;
use DateTimeImmutable;

/**
 * Функция рассчитывает кол-во дней до нового года
 * @param DateTimeImmutable $date дата от которой, необходимо рассчитать кол-во дней
 * @return int
 */
function howDaysToNy(DateTimeImmutable $date): int {

    $nextYear = (int)$date->format('Y') + 1;
    $dateOfNY = (new DateTime())
        ->setDate($nextYear, 01, 01);

    return $dateOfNY->diff($date)->format('%a');
}