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 +34 −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],] */ } No newline at end of file src/Controller/HomeController.php +15 −19 Original line number Diff line number Diff line Loading @@ -2,35 +2,31 @@ namespace App\Controller; use App\Action\Functions; use App\Validation\ArrayValidation; 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 { private function sortPrice(array $array): array private Functions $functions; public function __construct(Functions $functions) { $price = []; $count = []; foreach ($array as $key => $row) { $price[$key] = $row['price']; $count[$key] = $row['count']; } array_multisort($price, SORT_DESC, $count, SORT_ASC, $array); return $array; $this->functions = $functions; } #[Route('/', name: 'home')] public function home(): Response #[Route('/', name: 'home', methods: ['POST'])] public function home(Request $request): Response { $array = array( ['price'=>10, 'count'=>2], ['price'=>5, 'count'=>5], ['price'=>8, 'count'=>5], ['price'=>12, 'count'=>4], ['price'=>8, 'count'=>4], ); $array = $this->sortPrice($array); $array = $request->get('arr'); $errors = ArrayValidation::validate($array); if (count($errors) > 0) { return new Response((string)$errors); } $array = $this->functions->sortPrice($array); return $this->json($array); } } src/Validation/ArrayValidation.php 0 → 100644 +27 −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([ 'price' => new Assert\Type('int'), 'count' => 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 +34 −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],] */ } No newline at end of file
src/Controller/HomeController.php +15 −19 Original line number Diff line number Diff line Loading @@ -2,35 +2,31 @@ namespace App\Controller; use App\Action\Functions; use App\Validation\ArrayValidation; 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 { private function sortPrice(array $array): array private Functions $functions; public function __construct(Functions $functions) { $price = []; $count = []; foreach ($array as $key => $row) { $price[$key] = $row['price']; $count[$key] = $row['count']; } array_multisort($price, SORT_DESC, $count, SORT_ASC, $array); return $array; $this->functions = $functions; } #[Route('/', name: 'home')] public function home(): Response #[Route('/', name: 'home', methods: ['POST'])] public function home(Request $request): Response { $array = array( ['price'=>10, 'count'=>2], ['price'=>5, 'count'=>5], ['price'=>8, 'count'=>5], ['price'=>12, 'count'=>4], ['price'=>8, 'count'=>4], ); $array = $this->sortPrice($array); $array = $request->get('arr'); $errors = ArrayValidation::validate($array); if (count($errors) > 0) { return new Response((string)$errors); } $array = $this->functions->sortPrice($array); return $this->json($array); } }
src/Validation/ArrayValidation.php 0 → 100644 +27 −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([ 'price' => new Assert\Type('int'), 'count' => new Assert\Type('int') ]) ]) ]) ]); return $validator->validate($array, $constraints); } } No newline at end of file