Skip to content
HomeController.php 2.33 KiB
Newer Older
Адлан Шамавов's avatar
Адлан Шамавов committed
<?php

namespace App\Controller;

Адлан Шамавов's avatar
Адлан Шамавов committed
use App\Action\Functions;
Адлан Шамавов's avatar
Адлан Шамавов committed
use App\Validation\ArrayValidation;
Адлан Шамавов's avatar
Адлан Шамавов committed
use DateTimeImmutable;
Адлан Шамавов's avatar
Адлан Шамавов committed
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
Адлан Шамавов's avatar
Адлан Шамавов committed
use Symfony\Component\HttpFoundation\Request;
Адлан Шамавов's avatar
Адлан Шамавов committed
use Symfony\Component\Routing\Attribute\Route;

class HomeController extends AbstractController
{
Адлан Шамавов's avatar
Адлан Шамавов committed
    private Functions $functions;
Адлан Шамавов's avatar
Адлан Шамавов committed

Адлан Шамавов's avatar
Адлан Шамавов committed
    public function __construct(Functions $functions)
Адлан Шамавов's avatar
Адлан Шамавов committed
    {
Адлан Шамавов's avatar
Адлан Шамавов committed
        $this->functions = $functions;
    }
Адлан Шамавов's avatar
Адлан Шамавов committed
    #[Route('/func1', name: 'home', methods: ['POST'])]
    public function func1(Request $request): Response
    {
        $array = $request->get('arr');
        if (!ArrayValidation::validateFunc1($array)) {
            return new Response("Invalid array");
        }
        $array = $this->functions->sortPrice($array);
        return $this->json($array);
Адлан Шамавов's avatar
Адлан Шамавов committed
    }
Адлан Шамавов's avatar
Адлан Шамавов committed
    #[Route('/func2', name: 'func2', methods: ['POST'])]
    public function func2(Request $request): Response
Адлан Шамавов's avatar
Адлан Шамавов committed
    {
Адлан Шамавов's avatar
Адлан Шамавов committed
        $id = $request->query->getInt('id');
        $array = $request->get('arr');
        if (!ArrayValidation::validateFunc2($array)) {
            return new Response("Invalid array");
        }
        $result = $this->functions->search($array, $id);
        return $this->json($result);
Адлан Шамавов's avatar
Адлан Шамавов committed
    }
Адлан Шамавов's avatar
Адлан Шамавов committed
    #[Route('/func3', name: 'func3', methods: ['POST'])]
Адлан Шамавов's avatar
Адлан Шамавов committed
    public function home(Request $request): Response
    {
        $array = $request->get('arr');
Адлан Шамавов's avatar
Адлан Шамавов committed
        $result = $this->functions->uniqElements($array);
        return $this->json($result);
    }

Адлан Шамавов's avatar
Адлан Шамавов committed
    #[Route('/func4', name: 'func4', methods: ['POST'])]
Адлан Шамавов's avatar
Адлан Шамавов committed
    public function func4(Request $request): Response
    {
        $array = $request->get('arr');
        if (!ArrayValidation::validateFunc4($array)) {
            return new Response("Invalid array");
Адлан Шамавов's avatar
Адлан Шамавов committed
        }
        $result = $this->functions->prepareMenu($array);
        return $this->json($result);
Адлан Шамавов's avatar
Адлан Шамавов committed
    }
Адлан Шамавов's avatar
Адлан Шамавов committed
    #[Route('/func5/{day}/{month}/{year}', name: 'func5')]
    public function func5(int $day, int $month, int $year): Response
Адлан Шамавов's avatar
Адлан Шамавов committed
    {
        $dateAsString = $year . "-" . $month . "-" . $day;
        try {
            $result = $this->functions->howDaysToNy(new DateTimeImmutable($dateAsString));
        } catch (\Exception $e) {
            return new Response($e->getMessage());
        }
        return $this->json(["Days before NY:" => $result]);
Адлан Шамавов's avatar
Адлан Шамавов committed
    }
}