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;
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);
......
......@@ -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
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