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..d453447b3d12891707bcda1895237a22a62e3203 --- /dev/null +++ b/src/Action/Functions.php @@ -0,0 +1,141 @@ +<?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; + } + + /** + * Удалить дубликаты, оÑтавив только уникальные Ð·Ð½Ð°Ñ‡ÐµÐ½Ð¸Ñ + * @param array $array + * @return array + */ + + public function uniqElements(array $array): array + { + return array_unique($array, SORT_REGULAR); + } + + /** + * Выходной маÑÑив: + * Array ( + * [0] => Array([0] => laravel, [1] => php) + * [1] => Array([0] => codeigniter, [1] => php) + * [3] => Array([0] => c++, [1] => java)) + * ) + */ + + /** + * Сгруппировать подразедлы в верхние разделы меню + * Дочерние Ñлементы помеÑтить в маÑÑив Ñ€Ð¾Ð´Ð¸Ñ‚ÐµÐ»Ñ Ñ ÐºÐ»ÑŽÑ‡Ð¾Ð¼ submenu + * Значение под ключом depth определÑет уровень раздела + * МаÑÑив $aMenu вÑегда начинаетÑÑ Ñ Ñлемента depth = 0, + * вÑе поÑледующие Ñлементы Ñ depth = 1 ÑвлÑÑŽÑ‚ÑÑ ÐµÐ³Ð¾ дочерними + * Ñлементами + * @param array $aMenu + * @return array + */ + + public function prepareMenu(array $aMenu): array + { + $result = []; + foreach ($aMenu as $arr) { + if ($arr['depth'] == 0) { + $result[] = array( + 'name' => $arr['name'], + 'depth' => $arr['depth'], + 'submenu' => [] + ); + continue; + } + $result[array_key_last($result)]['submenu'][] = array( + 'name' => $arr['name'], + 'depth' => $arr['depth'], + ); + } + return $result; + } + + /** + * Выходные данные: + * $aMenu = [ + * [ + * 'name' => 'Смартфоны и гаджеты', + * 'depth' => 0, + * 'submenu' => [ + * ['name' => 'Смартфоны, мобильные телефоны','depth' => 1,], + * ['name' => 'Планшеты','depth' => 1,], + * ['name' => 'Ðаушники и гарнитуры','depth' => 1,],], + * ], + * [ + * 'name' => 'Компьютеры и ноутбуки', + * 'depth' => 0, + * 'submenu' => [ + * ['name' => 'Ðоутбуки и акÑеÑÑуары','depth' => 1,], + * ['name' => 'Компьютеры и мониторы','depth' => 1,], + * ['name' => 'Компьютерные комплектующие','depth' => 1,],]], + * [ + * 'name' => 'Техника Ð´Ð»Ñ Ð´Ð¾Ð¼Ð°', + * 'depth' => 0, + * 'submenu' => [ + * ['name' => 'Техника Ð´Ð»Ñ ÑƒÐ±Ð¾Ñ€ÐºÐ¸','depth' => 1,], + * ['name' => 'Товары Ð´Ð»Ñ ÑƒÑ…Ð¾Ð´Ð° за одеждой','depth' => 1,], + * ['name' => 'ÐкÑеÑÑуары Ð´Ð»Ñ Ñ‚ÐµÑ…Ð½Ð¸ÐºÐ¸','depth' => 1,],] + * ], + * [ + * 'name' => 'Товары Ð´Ð»Ñ Ð´Ð¾Ð¼Ð° и кухни', + * 'depth' => 0, + * 'submenu' => [ + * ['name' => 'ПоÑуда','depth' => 1,],]], + * ]; + */ +} + diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index ce5e43e9d94c60bbf5f8e35fab910dfcba1f0890..92bf56a7abd23a36f3b9a278e3dc8f401fb1c61d 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -2,52 +2,31 @@ namespace App\Controller; +use App\Action\Functions; +use App\Validation\ArrayValidation; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; class HomeController extends AbstractController { - private function prepareMenu(array $aMenu): array { - $result = []; - foreach ($aMenu as $arr) { - if ($arr['depth'] === 0) { - $result[] = array( - 'name' => $arr['name'], - 'depth' => $arr['depth'], - 'submenu' => [] - ); - continue; - } - $result[array_key_last($result)]['submenu'][] = array( - 'name' => $arr['name'], - 'depth' => $arr['depth'], - ); - } - return $result; - } - + private Functions $functions; - #[Route('/', name: 'home')] - public function home(): Response + public function __construct(Functions $functions) { - $aMenu = [ - ['name' => 'Смартфоны и гаджеты','depth' => 0,], - ['name' => 'Смартфоны, мобильные телефоны','depth' => 1,], - ['name' => 'Планшеты','depth' => 1,], - ['name' => 'Ðаушники и гарнитуры','depth' => 1,], - ['name' => 'Компьютеры и ноутбуки','depth' => 0,], - ['name' => 'Ðоутбуки и акÑеÑÑуары','depth' => 1,], - ['name' => 'Компьютеры и мониторы','depth' => 1,], - ['name' => 'Компьютерные комплектующие','depth' => 1,], - ['name' => 'Техника Ð´Ð»Ñ Ð´Ð¾Ð¼Ð°','depth' => 0,], - ['name' => 'Техника Ð´Ð»Ñ ÑƒÐ±Ð¾Ñ€ÐºÐ¸','depth' => 1,], - ['name' => 'Товары Ð´Ð»Ñ ÑƒÑ…Ð¾Ð´Ð° за одеждой','depth' => 1,], - ['name' => 'ÐкÑеÑÑуары Ð´Ð»Ñ Ñ‚ÐµÑ…Ð½Ð¸ÐºÐ¸','depth' => 1,], - ['name' => 'Товары Ð´Ð»Ñ Ð´Ð¾Ð¼Ð° и кухни','depth' => 0,], - ['name' => 'ПоÑуда','depth' => 1,], - ]; + $this->functions = $functions; + } - return $this->render('home.html.twig', ['menu' => $this->prepareMenu($aMenu)]); + #[Route('/', name: 'home', methods: ['POST'])] + public function home(Request $request): Response + { + $array = $request->get('arr'); + $errors = ArrayValidation::validate($array); + if (count($errors) > 0) { + return new Response((string)$errors); + } + $result = $this->functions->prepareMenu($array); + return $this->json($result); } } diff --git a/src/Validation/ArrayValidation.php b/src/Validation/ArrayValidation.php new file mode 100644 index 0000000000000000000000000000000000000000..83567bdcb11f0d1fac13954fc54e7ddf09179ac0 --- /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([ + 'name' => new Assert\Type('string'), + 'depth' => new Assert\Type('int'), + ]) + ]) + ]) + ]); + return $validator->validate($array, $constraints); + } +} \ No newline at end of file