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..3afc8044baf0c90ba0e1f3821ef981feef680efe --- /dev/null +++ b/src/Action/Functions.php @@ -0,0 +1,34 @@ +<?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 diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 0a6be9d3f89c7791168103d130cd7a64f291dd45..175e9bcf869e7181bb6421fec6b14ce6b05ec56d 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -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); } } diff --git a/src/Validation/ArrayValidation.php b/src/Validation/ArrayValidation.php new file mode 100644 index 0000000000000000000000000000000000000000..ea6eb8a28cf09ed56e544435ad065364db734b6c --- /dev/null +++ b/src/Validation/ArrayValidation.php @@ -0,0 +1,27 @@ +<?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