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

refactoring

parent ca5ff1b7
No related branches found
No related tags found
1 merge request!3Ptps controller 3
......@@ -29,14 +29,13 @@ class HomeController extends AbstractController
return $this->json($array);
}
#[Route('/', name: 'home', methods: ['POST'])]
public function home(Request $request): Response
#[Route('/func2', name: 'func2', methods: ['POST'])]
public function func2(Request $request): Response
{
$id = $request->query->getInt('id');
$array = $request->get('arr');
$errors = ArrayValidation::validate($array);
if (count($errors) > 0) {
return new Response((string)$errors);
if (!ArrayValidation::validateFunc2($array)) {
return new Response("Invalid array");
}
$result = $this->functions->search($array, $id);
return $this->json($result);
......
......@@ -11,21 +11,10 @@ class ArrayValidation
return ctype_digit(implode('',$prices)) && ctype_digit(implode('', $counts));
}
public static function validate(array $array): ConstraintViolationListInterface
public static function validateFunc2(array $array): bool
{
$validator = Validation::createValidator();
$constraints = new Assert\Optional([
new Assert\Collection([
new Assert\Optional([
new Assert\Type('array'),
new Assert\Collection([
'id' => new Assert\Type('int'),
'name' => new Assert\Type('string'),
'age' => new Assert\Type('int'),
])
])
])
]);
return $validator->validate($array, $constraints);
$ids = array_column($array, 'id');
$ages = array_column($array, 'age');
return ctype_digit(implode('', $ids)) && ctype_digit(implode('', $ages));
}
}
\ 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