Loading .gitignore +1 −0 Original line number Diff line number Diff line Loading @@ -23,3 +23,4 @@ /public/assets/ /assets/vendor/ ###< symfony/asset-mapper ### /.idea No newline at end of file src/Action/Functions.php 0 → 100644 +55 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace App\Action; class Functions { /** * Выполняет сортировку массива по убыванию цены * @param array $array * @return array */ public function sortPrice(array $array): array { $prices = array_column($array, 'price'); $counts = array_column($array, 'count'); array_multisort( $prices, SORT_DESC, $counts, SORT_ASC, $array ); return $array; } /** * На выход должна вернуть отсортированный массив по ключу *price* DESC * и во вторую очередь по *count* ASC: * [['price'=>12, 'count'=>4], ['price'=>10, 'count'=>2], ['price'=>8, 'count'=>4], * ['price'=>8, 'count'=>5], ['price'=>5, 'count'=>5],] */ /** * Найдет элемент с указаным id * @param array $array - массив, содержащий элементы со структурой * [ * 'id' => 30, * 'name' => 'Jhon', * 'age' => 23, * ] * @param $id - ид искомого элемента * @return ?array - найденный элемент */ public function search(array $array, int $id): ?array { $rowId = array_search($id, array_column($array, 'id'), false); if ($rowId !== false) { return $array[$rowId]; } return null; } } src/Controller/HomeController.php +17 −15 Original line number Diff line number Diff line Loading @@ -2,30 +2,32 @@ 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\Routing\Attribute\Route; class HomeController extends AbstractController { #[Route('/search/{id}', name: 'home_search')] public function search(int $id): Response private Functions $functions; public function __construct(Functions $functions) { $array = [ ['id'=>10, 'name'=>'Jhon', 'age'=>23], ['id'=>32, 'name'=>'Alex', 'age'=>34], ['id'=>54, 'name'=>'Bob', 'age'=>45], ['id'=>6, 'name'=>'Mike', 'age'=>61], ]; $rowId = array_search($id, array_column($array, 'id')); return $this->json($rowId); $this->functions = $functions; } #[Route('/', name: 'home')] public function home(): Response #[Route('/', name: 'home', methods: ['POST'])] public function home(Request $request): Response { return $this->redirectToRoute('home_search', [ 'id' => 32 ]); $id = $request->query->getInt('id'); $array = $request->get('arr'); $errors = ArrayValidation::validate($array); if (count($errors) > 0) { return new Response((string)$errors); } $result = $this->functions->search($array, $id); return $this->json($result); } } src/Validation/ArrayValidation.php 0 → 100644 +28 −0 Original line number Diff line number Diff line <?php 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 validate(array $array): ConstraintViolationListInterface { $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); } } No newline at end of file Loading
.gitignore +1 −0 Original line number Diff line number Diff line Loading @@ -23,3 +23,4 @@ /public/assets/ /assets/vendor/ ###< symfony/asset-mapper ### /.idea No newline at end of file
src/Action/Functions.php 0 → 100644 +55 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace App\Action; class Functions { /** * Выполняет сортировку массива по убыванию цены * @param array $array * @return array */ public function sortPrice(array $array): array { $prices = array_column($array, 'price'); $counts = array_column($array, 'count'); array_multisort( $prices, SORT_DESC, $counts, SORT_ASC, $array ); return $array; } /** * На выход должна вернуть отсортированный массив по ключу *price* DESC * и во вторую очередь по *count* ASC: * [['price'=>12, 'count'=>4], ['price'=>10, 'count'=>2], ['price'=>8, 'count'=>4], * ['price'=>8, 'count'=>5], ['price'=>5, 'count'=>5],] */ /** * Найдет элемент с указаным id * @param array $array - массив, содержащий элементы со структурой * [ * 'id' => 30, * 'name' => 'Jhon', * 'age' => 23, * ] * @param $id - ид искомого элемента * @return ?array - найденный элемент */ public function search(array $array, int $id): ?array { $rowId = array_search($id, array_column($array, 'id'), false); if ($rowId !== false) { return $array[$rowId]; } return null; } }
src/Controller/HomeController.php +17 −15 Original line number Diff line number Diff line Loading @@ -2,30 +2,32 @@ 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\Routing\Attribute\Route; class HomeController extends AbstractController { #[Route('/search/{id}', name: 'home_search')] public function search(int $id): Response private Functions $functions; public function __construct(Functions $functions) { $array = [ ['id'=>10, 'name'=>'Jhon', 'age'=>23], ['id'=>32, 'name'=>'Alex', 'age'=>34], ['id'=>54, 'name'=>'Bob', 'age'=>45], ['id'=>6, 'name'=>'Mike', 'age'=>61], ]; $rowId = array_search($id, array_column($array, 'id')); return $this->json($rowId); $this->functions = $functions; } #[Route('/', name: 'home')] public function home(): Response #[Route('/', name: 'home', methods: ['POST'])] public function home(Request $request): Response { return $this->redirectToRoute('home_search', [ 'id' => 32 ]); $id = $request->query->getInt('id'); $array = $request->get('arr'); $errors = ArrayValidation::validate($array); if (count($errors) > 0) { return new Response((string)$errors); } $result = $this->functions->search($array, $id); return $this->json($result); } }
src/Validation/ArrayValidation.php 0 → 100644 +28 −0 Original line number Diff line number Diff line <?php 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 validate(array $array): ConstraintViolationListInterface { $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); } } No newline at end of file