Loading .gitignore +2 −0 Original line number Diff line number Diff line Loading @@ -23,3 +23,5 @@ /public/assets/ /assets/vendor/ ###< symfony/asset-mapper ### /.idea src/Action/Functions.php 0 → 100644 +158 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace App\Action; use DateTimeImmutable; use Exception; 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,],]], * ]; */ /** * Функция рассчитывает кол-во дней до нового года * @param DateTimeImmutable $date дата от которой, необходимо рассчитать кол-во дней * @return int * @throws Exception */ public function howDaysToNy(DateTimeImmutable $date): int { $endYear = date("Y-12-31", date_timestamp_get($date)); $dateInterval = date_diff(new DateTimeImmutable($endYear), $date); return (int)$dateInterval->format("%a") + 1; } } src/Controller/HomeController.php +16 −9 Original line number Diff line number Diff line Loading @@ -2,24 +2,31 @@ namespace App\Controller; use App\Action\Functions; use DateTimeImmutable; 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 howDaysToNy(DateTimeImmutable $date): int { $endYear = date("Y-12-31", date_timestamp_get($date)); $dateInterval = date_diff(new DateTimeImmutable($endYear), $date); return $dateInterval->format("%a") + 1; } private Functions $functions; #[Route('/', name: 'home')] public function home(): Response public function __construct(Functions $functions) { $count = $this->howDaysToNy(new DateTimeImmutable()); $this->functions = $functions; } return $this->render('home.html.twig', ['count' => $count]); #[Route('/{day}/{month}/{year}', name: 'home')] public function home(int $day, int $month, int $year): Response { $dateAsString = $year . "-" . $month . "-" . $day; try { $result = $this->functions->howDaysToNy(new DateTimeImmutable($dateAsString)); } catch (\Exception $e) { return new Response($e->getMessage()); } return $this->json(["Days before NY:" => $result]); } } Loading
.gitignore +2 −0 Original line number Diff line number Diff line Loading @@ -23,3 +23,5 @@ /public/assets/ /assets/vendor/ ###< symfony/asset-mapper ### /.idea
src/Action/Functions.php 0 → 100644 +158 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace App\Action; use DateTimeImmutable; use Exception; 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,],]], * ]; */ /** * Функция рассчитывает кол-во дней до нового года * @param DateTimeImmutable $date дата от которой, необходимо рассчитать кол-во дней * @return int * @throws Exception */ public function howDaysToNy(DateTimeImmutable $date): int { $endYear = date("Y-12-31", date_timestamp_get($date)); $dateInterval = date_diff(new DateTimeImmutable($endYear), $date); return (int)$dateInterval->format("%a") + 1; } }
src/Controller/HomeController.php +16 −9 Original line number Diff line number Diff line Loading @@ -2,24 +2,31 @@ namespace App\Controller; use App\Action\Functions; use DateTimeImmutable; 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 howDaysToNy(DateTimeImmutable $date): int { $endYear = date("Y-12-31", date_timestamp_get($date)); $dateInterval = date_diff(new DateTimeImmutable($endYear), $date); return $dateInterval->format("%a") + 1; } private Functions $functions; #[Route('/', name: 'home')] public function home(): Response public function __construct(Functions $functions) { $count = $this->howDaysToNy(new DateTimeImmutable()); $this->functions = $functions; } return $this->render('home.html.twig', ['count' => $count]); #[Route('/{day}/{month}/{year}', name: 'home')] public function home(int $day, int $month, int $year): Response { $dateAsString = $year . "-" . $month . "-" . $day; try { $result = $this->functions->howDaysToNy(new DateTimeImmutable($dateAsString)); } catch (\Exception $e) { return new Response($e->getMessage()); } return $this->json(["Days before NY:" => $result]); } }