diff --git a/.gitignore b/.gitignore index 4daae382023496e2a12c957fca3f95d695ff6bf6..930e1e21e27cc2916f9dd36aa9e577d0fb0b2ee0 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ /public/assets/ /assets/vendor/ ###< symfony/asset-mapper ### +/.idea \ No newline at end of file diff --git a/src/Action/Functions.php b/src/Action/Functions.php new file mode 100644 index 0000000000000000000000000000000000000000..cdabbc38cd25243186aaacebeeb580639d8fe2de --- /dev/null +++ b/src/Action/Functions.php @@ -0,0 +1,55 @@ +<?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; + } +} diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index bcab10207d3fa9bc906cb16eb3676134e67d1dc5..8623303715e779eca04cb98ae2218e34cdc0dc41 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -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); } } diff --git a/src/Validation/ArrayValidation.php b/src/Validation/ArrayValidation.php new file mode 100644 index 0000000000000000000000000000000000000000..739cea17449e518852708f36bbe0fe196b3efbd1 --- /dev/null +++ b/src/Validation/ArrayValidation.php @@ -0,0 +1,28 @@ +<?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