diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index a897848b3987cf7b8ab47720e14aa738349dc317..64b1b61e50f081a36199c0a6682e093fc776fb6e 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -5,36 +5,56 @@ namespace App\Controller; use App\Action\Functions; use App\Validation\ArrayValidation; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Attribute\Route; class HomeController extends AbstractController { - private function uniqElements(array $array): array { - return array_unique($array, SORT_REGULAR); + private Functions $functions; + + public function __construct(Functions $functions) + { + $this->functions = $functions; + } + + #[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); } - #[Route('/', name: 'home')] - public function home(): Response + #[Route('/func2', name: 'func2', methods: ['POST'])] + public function func2(Request $request): Response { - $arr = [ - ['laravel', 'php'], - ['codeigniter', 'php'], - ['laravel', 'php'], - ['c++', 'java'], - ]; - return $this->json($this->uniqElements($arr)); + $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); } - #[Route('/', name: 'home', methods: ['POST'])] + #[Route('/func3', 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->uniqElements($array); + return $this->json($result); + } + + #[Route('/func4', name: 'home', methods: ['POST'])] + public function func4(Request $request): Response + { + $array = $request->get('arr'); + if (!ArrayValidation::validateFunc4($array)) { + return new Response("Invalid array"); } $result = $this->functions->prepareMenu($array); return $this->json($result); diff --git a/src/Validation/ArrayValidation.php b/src/Validation/ArrayValidation.php index e160dfbc9930c0b407ccd8f80d2315af5f1cc015..5807a447814593f64ecf069980c81099b8be861d 100644 --- a/src/Validation/ArrayValidation.php +++ b/src/Validation/ArrayValidation.php @@ -18,20 +18,9 @@ class ArrayValidation return ctype_digit(implode('', $ids)) && ctype_digit(implode('', $ages)); } - public static function validate(array $array): ConstraintViolationListInterface + public static function validateFunc4(array $array): bool { - $validator = Validation::createValidator(); - $constraints = new Assert\Optional([ - new Assert\Collection([ - new Assert\Optional([ - new Assert\Type('array'), - new Assert\Collection([ - 'name' => new Assert\Type('string'), - 'depth' => new Assert\Type('int'), - ]) - ]) - ]) - ]); - return $validator->validate($array, $constraints); + $depths = array_column($array, 'depth'); + return ctype_digit(implode('', $depths)); } } \ No newline at end of file