Skip to content
Snippets Groups Projects
Commit e8304f56 authored by Адлан Шамавов's avatar Адлан Шамавов
Browse files

refactoring

parent 8d956fe9
No related branches found
No related tags found
1 merge request!4Ptps controller 4
...@@ -5,36 +5,56 @@ namespace App\Controller; ...@@ -5,36 +5,56 @@ namespace App\Controller;
use App\Action\Functions; use App\Action\Functions;
use App\Validation\ArrayValidation; use App\Validation\ArrayValidation;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
class HomeController extends AbstractController class HomeController extends AbstractController
{ {
private function uniqElements(array $array): array { private Functions $functions;
return array_unique($array, SORT_REGULAR);
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')] #[Route('/func2', name: 'func2', methods: ['POST'])]
public function home(): Response public function func2(Request $request): Response
{ {
$arr = [ $id = $request->query->getInt('id');
['laravel', 'php'], $array = $request->get('arr');
['codeigniter', 'php'], if (!ArrayValidation::validateFunc2($array)) {
['laravel', 'php'], return new Response("Invalid array");
['c++', 'java'], }
]; $result = $this->functions->search($array, $id);
return $this->json($this->uniqElements($arr)); return $this->json($result);
} }
#[Route('/', name: 'home', methods: ['POST'])] #[Route('/func3', name: 'home', methods: ['POST'])]
public function home(Request $request): Response public function home(Request $request): Response
{ {
$array = $request->get('arr'); $array = $request->get('arr');
$errors = ArrayValidation::validate($array); $result = $this->functions->uniqElements($array);
if (count($errors) > 0) { return $this->json($result);
return new Response((string)$errors); }
#[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); $result = $this->functions->prepareMenu($array);
return $this->json($result); return $this->json($result);
......
...@@ -18,20 +18,9 @@ class ArrayValidation ...@@ -18,20 +18,9 @@ class ArrayValidation
return ctype_digit(implode('', $ids)) && ctype_digit(implode('', $ages)); 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(); $depths = array_column($array, 'depth');
$constraints = new Assert\Optional([ return ctype_digit(implode('', $depths));
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);
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment