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

Merge branch 'PTPS_Controller_2' into PTPS_Controller_3

parents 2c754901 86264a54
No related branches found
No related tags found
1 merge request!3Ptps controller 3
This commit is part of merge request !6. Comments created here will be created in the context of that merge request.
......@@ -65,12 +65,11 @@ class Functions
}
/**
* Выходной массив:
* Array (
* [0] => Array([0] => laravel, [1] => php)
* [1] => Array([0] => codeigniter, [1] => php)
* [3] => Array([0] => c++, [1] => java))
* )
*/
}
* Выходной массив:
* Array (
* [0] => Array([0] => laravel, [1] => php)
* [1] => Array([0] => codeigniter, [1] => php)
* [3] => Array([0] => c++, [1] => java))
* )
*/
}
\ No newline at end of file
......@@ -4,9 +4,9 @@ namespace App\Controller;
use App\Action\Functions;
use App\Validation\ArrayValidation;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
class HomeController extends AbstractController
......@@ -18,6 +18,29 @@ class HomeController extends AbstractController
$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('/func2', name: 'func2', methods: ['POST'])]
public function func2(Request $request): Response
{
$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'])]
public function home(Request $request): Response
{
......
......@@ -2,12 +2,22 @@
namespace App\Validation;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ConstraintViolationListInterface;
class ArrayValidation
{
public static function validateFunc1(array $array): bool
{
$prices = array_column($array, 'price');
$counts = array_column($array, 'count');
return ctype_digit(implode('',$prices)) && ctype_digit(implode('', $counts));
}
public static function validateFunc2(array $array): bool
{
$ids = array_column($array, 'id');
$ages = array_column($array, 'age');
return ctype_digit(implode('', $ids)) && ctype_digit(implode('', $ages));
}
public static function validate(array $array): ConstraintViolationListInterface
{
$validator = Validation::createValidator();
......@@ -16,7 +26,7 @@ class ArrayValidation
new Assert\Optional([
new Assert\Type('array'),
new Assert\Collection([
new Assert\Type('string'),
new Assert\Type('string'),
new Assert\Type('string'),
])
])
......
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