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

namespace App\Controller;

Адлан Шамавов's avatar
Адлан Шамавов committed
use App\Action\Functions;
use App\Validation\ArrayValidation;
Адлан Шамавов's avatar
Адлан Шамавов committed
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Адлан Шамавов's avatar
Адлан Шамавов committed
use Symfony\Component\HttpFoundation\Request;
Адлан Шамавов's avatar
Адлан Шамавов committed
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
{
    private function uniqElements(array $array): array {
        return array_unique($array, SORT_REGULAR);
Адлан Шамавов's avatar
Адлан Шамавов committed
    }
    #[Route('/', name: 'home')]
    public function home(): Response
Адлан Шамавов's avatar
Адлан Шамавов committed
    {
        $arr = [
            ['laravel', 'php'],
            ['codeigniter', 'php'],
            ['laravel', 'php'],
            ['c++', 'java'],
        ];
        return $this->json($this->uniqElements($arr));
Адлан Шамавов's avatar
Адлан Шамавов committed
    }
Адлан Шамавов's avatar
Адлан Шамавов committed
    #[Route('/', name: 'home', methods: ['POST'])]
    public function home(Request $request): Response
    {
        $array = $request->get('arr');
        $errors = ArrayValidation::validate($array);
        if (count($errors) > 0) {
            return new Response((string)$errors);
        }
        $result = $this->functions->prepareMenu($array);
        return $this->json($result);
Адлан Шамавов's avatar
Адлан Шамавов committed
    }
}