From f7fa108692cca9f92b137138d508b26be46ed05f Mon Sep 17 00:00:00 2001 From: "a.shamavov" <a.shamavov@iqdev.digital> Date: Tue, 16 Apr 2024 10:13:54 +0500 Subject: [PATCH] temp commit --- composer.json | 2 +- composer.lock | 107 ++++++++++++++++-------------- src/Action/Functions.php | 65 ++---------------- src/Controller/HomeController.php | 102 ++++++++++++++-------------- 4 files changed, 113 insertions(+), 163 deletions(-) diff --git a/composer.json b/composer.json index be86947..5246c9a 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "symfony/translation": "7.0.*", "symfony/twig-bundle": "7.0.*", "symfony/ux-turbo": "^2.16", - "symfony/validator": "7.0.*", + "symfony/validator": "6.4.*", "symfony/web-link": "7.0.*", "symfony/yaml": "7.0.*", "twig/extra-bundle": "^2.12|^3.0", diff --git a/composer.lock b/composer.lock index 5695357..7c3d7d0 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "168d5f8561d288fe5cd6fd2b406687e2", + "content-hash": "91b0c89268c08e0b881610c8ba320eb8", "packages": [ { "name": "composer/semver", @@ -1375,16 +1375,16 @@ }, { "name": "monolog/monolog", - "version": "3.5.0", + "version": "3.6.0", "source": { "type": "git", "url": "https://github.com/Seldaek/monolog.git", - "reference": "c915e2634718dbc8a4a15c61b0e62e7a44e14448" + "reference": "4b18b21a5527a3d5ffdac2fd35d3ab25a9597654" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Seldaek/monolog/zipball/c915e2634718dbc8a4a15c61b0e62e7a44e14448", - "reference": "c915e2634718dbc8a4a15c61b0e62e7a44e14448", + "url": "https://api.github.com/repos/Seldaek/monolog/zipball/4b18b21a5527a3d5ffdac2fd35d3ab25a9597654", + "reference": "4b18b21a5527a3d5ffdac2fd35d3ab25a9597654", "shasum": "" }, "require": { @@ -1407,7 +1407,7 @@ "phpstan/phpstan": "^1.9", "phpstan/phpstan-deprecation-rules": "^1.0", "phpstan/phpstan-strict-rules": "^1.4", - "phpunit/phpunit": "^10.1", + "phpunit/phpunit": "^10.5.17", "predis/predis": "^1.1 || ^2", "ruflin/elastica": "^7", "symfony/mailer": "^5.4 || ^6", @@ -1460,7 +1460,7 @@ ], "support": { "issues": "https://github.com/Seldaek/monolog/issues", - "source": "https://github.com/Seldaek/monolog/tree/3.5.0" + "source": "https://github.com/Seldaek/monolog/tree/3.6.0" }, "funding": [ { @@ -1472,7 +1472,7 @@ "type": "tidelift" } ], - "time": "2023-10-27T15:32:31+00:00" + "time": "2024-04-12T21:02:21+00:00" }, { "name": "phpdocumentor/reflection-common", @@ -1529,28 +1529,35 @@ }, { "name": "phpdocumentor/reflection-docblock", - "version": "5.3.0", + "version": "5.4.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "622548b623e81ca6d78b721c5e029f4ce664f170" + "reference": "298d2febfe79d03fe714eb871d5538da55205b1a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/622548b623e81ca6d78b721c5e029f4ce664f170", - "reference": "622548b623e81ca6d78b721c5e029f4ce664f170", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/298d2febfe79d03fe714eb871d5538da55205b1a", + "reference": "298d2febfe79d03fe714eb871d5538da55205b1a", "shasum": "" }, "require": { + "doctrine/deprecations": "^1.1", "ext-filter": "*", - "php": "^7.2 || ^8.0", + "php": "^7.4 || ^8.0", "phpdocumentor/reflection-common": "^2.2", - "phpdocumentor/type-resolver": "^1.3", + "phpdocumentor/type-resolver": "^1.7", + "phpstan/phpdoc-parser": "^1.7", "webmozart/assert": "^1.9.1" }, "require-dev": { - "mockery/mockery": "~1.3.2", - "psalm/phar": "^4.8" + "mockery/mockery": "~1.3.5", + "phpstan/extension-installer": "^1.1", + "phpstan/phpstan": "^1.8", + "phpstan/phpstan-mockery": "^1.1", + "phpstan/phpstan-webmozart-assert": "^1.2", + "phpunit/phpunit": "^9.5", + "vimeo/psalm": "^5.13" }, "type": "library", "extra": { @@ -1574,15 +1581,15 @@ }, { "name": "Jaap van Otterdijk", - "email": "account@ijaap.nl" + "email": "opensource@ijaap.nl" } ], "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", "support": { "issues": "https://github.com/phpDocumentor/ReflectionDocBlock/issues", - "source": "https://github.com/phpDocumentor/ReflectionDocBlock/tree/5.3.0" + "source": "https://github.com/phpDocumentor/ReflectionDocBlock/tree/5.4.0" }, - "time": "2021-10-19T17:43:47+00:00" + "time": "2024-04-09T21:13:58+00:00" }, { "name": "phpdocumentor/type-resolver", @@ -6820,53 +6827,55 @@ }, { "name": "symfony/validator", - "version": "v7.0.6", + "version": "v6.4.6", "source": { "type": "git", "url": "https://github.com/symfony/validator.git", - "reference": "a2df2c63b7944a162dee86ab8065f2f91b7d6e36" + "reference": "ca1d78e8677e966e307a63799677b64b194d735d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/validator/zipball/a2df2c63b7944a162dee86ab8065f2f91b7d6e36", - "reference": "a2df2c63b7944a162dee86ab8065f2f91b7d6e36", + "url": "https://api.github.com/repos/symfony/validator/zipball/ca1d78e8677e966e307a63799677b64b194d735d", + "reference": "ca1d78e8677e966e307a63799677b64b194d735d", "shasum": "" }, "require": { - "php": ">=8.2", + "php": ">=8.1", + "symfony/deprecation-contracts": "^2.5|^3", "symfony/polyfill-ctype": "~1.8", "symfony/polyfill-mbstring": "~1.0", "symfony/polyfill-php83": "^1.27", "symfony/translation-contracts": "^2.5|^3" }, "conflict": { + "doctrine/annotations": "<1.13", "doctrine/lexer": "<1.1", - "symfony/dependency-injection": "<6.4", - "symfony/doctrine-bridge": "<7.0", - "symfony/expression-language": "<6.4", - "symfony/http-kernel": "<6.4", - "symfony/intl": "<6.4", - "symfony/property-info": "<6.4", - "symfony/translation": "<6.4.3|>=7.0,<7.0.3", - "symfony/yaml": "<6.4" + "symfony/dependency-injection": "<5.4", + "symfony/expression-language": "<5.4", + "symfony/http-kernel": "<5.4", + "symfony/intl": "<5.4", + "symfony/property-info": "<5.4", + "symfony/translation": "<5.4.35|>=6.0,<6.3.12|>=6.4,<6.4.3|>=7.0,<7.0.3", + "symfony/yaml": "<5.4" }, "require-dev": { + "doctrine/annotations": "^1.13|^2", "egulias/email-validator": "^2.1.10|^3|^4", - "symfony/cache": "^6.4|^7.0", - "symfony/config": "^6.4|^7.0", - "symfony/console": "^6.4|^7.0", - "symfony/dependency-injection": "^6.4|^7.0", - "symfony/expression-language": "^6.4|^7.0", - "symfony/finder": "^6.4|^7.0", - "symfony/http-client": "^6.4|^7.0", - "symfony/http-foundation": "^6.4|^7.0", - "symfony/http-kernel": "^6.4|^7.0", - "symfony/intl": "^6.4|^7.0", - "symfony/mime": "^6.4|^7.0", - "symfony/property-access": "^6.4|^7.0", - "symfony/property-info": "^6.4|^7.0", - "symfony/translation": "^6.4.3|^7.0.3", - "symfony/yaml": "^6.4|^7.0" + "symfony/cache": "^5.4|^6.0|^7.0", + "symfony/config": "^5.4|^6.0|^7.0", + "symfony/console": "^5.4|^6.0|^7.0", + "symfony/dependency-injection": "^5.4|^6.0|^7.0", + "symfony/expression-language": "^5.4|^6.0|^7.0", + "symfony/finder": "^5.4|^6.0|^7.0", + "symfony/http-client": "^5.4|^6.0|^7.0", + "symfony/http-foundation": "^5.4|^6.0|^7.0", + "symfony/http-kernel": "^5.4|^6.0|^7.0", + "symfony/intl": "^5.4|^6.0|^7.0", + "symfony/mime": "^5.4|^6.0|^7.0", + "symfony/property-access": "^5.4|^6.0|^7.0", + "symfony/property-info": "^5.4|^6.0|^7.0", + "symfony/translation": "^5.4.35|~6.3.12|^6.4.3|^7.0.3", + "symfony/yaml": "^5.4|^6.0|^7.0" }, "type": "library", "autoload": { @@ -6894,7 +6903,7 @@ "description": "Provides tools to validate values", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/validator/tree/v7.0.6" + "source": "https://github.com/symfony/validator/tree/v6.4.6" }, "funding": [ { @@ -6910,7 +6919,7 @@ "type": "tidelift" } ], - "time": "2024-03-28T09:20:36+00:00" + "time": "2024-03-27T22:00:14+00:00" }, { "name": "symfony/var-dumper", diff --git a/src/Action/Functions.php b/src/Action/Functions.php index 34e4ecd..b91efe4 100644 --- a/src/Action/Functions.php +++ b/src/Action/Functions.php @@ -4,11 +4,12 @@ declare(strict_types=1); namespace App\Action; +use Exception; +use RuntimeException; use DateTimeImmutable; use DateTime; -use DateInterval; use DatePeriod; -use Exception; +use DateInterval; class Functions { @@ -17,7 +18,6 @@ class Functions * @param array $array * @return array */ - public function sortPrice(array $array): array { $prices = array_column($array, 'price'); @@ -31,13 +31,6 @@ class Functions 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 - маÑÑив, Ñодержащий Ñлементы Ñо Ñтруктурой @@ -64,21 +57,11 @@ class Functions * @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 @@ -89,7 +72,6 @@ class Functions * @param array $aMenu * @return array */ - public function prepareMenu(array $aMenu): array { $result = []; @@ -110,47 +92,12 @@ class Functions 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)); @@ -164,7 +111,6 @@ class Functions * @return DateTimeImmutable[] * @throws Exception */ - public function countFriday13(int $year): iterable { $startDate = new DateTime("$year-01-01 Friday"); @@ -196,7 +142,6 @@ class Functions * @return string * @throws RuntimeException */ - public function readLogFile(string $filePath): string { if (file_exists($filePath)) { @@ -209,8 +154,6 @@ class Functions fclose($file); return $text; } - else { - throw new RuntimeException("File not found: $filePath"); - } + throw new RuntimeException("File not found: $filePath"); } } \ No newline at end of file diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 708cb5e..38e1feb 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -3,67 +3,64 @@ namespace App\Controller; use App\Action\Functions; -use App\Validation\{ArrayValidation, DateValidation}; +use App\Requests\{ + SortPriceRequest, + SearchRequest, + UniqElementsRequest, + MenuRequest, + DiffDaysRequest +}; use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Attribute\Route; class HomeController extends AbstractController { - private Functions $functions; + public function __construct(private Functions $functions) {} - public function __construct(Functions $functions) + #[Route('/sortPrice', name: 'sortPrice', methods: ['POST'])] + public function sortPrice(SortPriceRequest $request): Response { - $this->functions = $functions; - } - - #[Route('/func1', name: 'home', methods: ['POST'])] - public function func1(Request $request): Response - { - $array = $request->get('arr'); - if (!ArrayValidation::validateFunc1($array)) { - return new Response("Invalid array"); - } - $array = $this->functions->sortPrice($array); + $array = $this->functions->sortPrice($request->getRequest()->toArray()['items']); return $this->json($array); } - #[Route('/func2', name: 'func2', methods: ['POST'])] - public function func2(Request $request): Response + #[Route('/search', name: 'search', methods: ['POST'])] + public function search(SearchRequest $request): Response { - $id = $request->query->getInt('id'); - $array = $request->get('arr'); - if (!ArrayValidation::validateFunc2($array)) { - return new Response("Invalid array"); - } + $array = $request->getRequest()->toArray()['items']; + $id = $request->getRequest()->query->get('id'); $result = $this->functions->search($array, $id); return $this->json($result); } - #[Route('/func3', name: 'func3', methods: ['POST'])] - public function home(Request $request): Response + #[Route('/uniqElements', name: 'uniqElements', methods: ['POST'])] + public function uniqElements(UniqElementsRequest $request): Response { - $array = $request->get('arr'); - $result = $this->functions->uniqElements($array); + $result = $this->functions->uniqElements($request->getRequest()->toArray()['items']); return $this->json($result); } - #[Route('/func4', name: 'func4', methods: ['POST'])] - public function func4(Request $request): Response + #[Route('/prepareMenu', name: 'prepareMenu', methods: ['POST'])] + public function prepareMenu(MenuRequest $request): Response { - $array = $request->get('arr'); - if (!ArrayValidation::validateFunc4($array)) { - return new Response("Invalid array"); - } - $result = $this->functions->prepareMenu($array); + $result = $this->functions->prepareMenu($request->getRequest()->toArray()['items']); return $this->json($result); } - #[Route('/func5/{day}/{month}/{year}', name: 'func5')] - public function func5(int $day, int $month, int $year): Response + #[Route( + '/howDaysToNy/{day}/{month}/{year}', + name: 'howDaysToNy', + requirements: [ + 'day' => '(?<!-)(?<!\d)\d+', + 'month' => '(?<!-)(?<!\d)\d+', + 'year' => '(?<!-)(?<!\d)\d+' + ], + methods: ['GET'] + )] + public function howDaysToNy(int $day, int $month, int $year): Response { $dateAsString = $year . "-" . $month . "-" . $day; try { @@ -74,8 +71,12 @@ class HomeController extends AbstractController return $this->json(["Days before NY:" => $result]); } - #[Route('/func6/{year}', name: 'func6', methods: ['GET'])] - public function func6(int $year): Response + #[Route( + '/countFriday13/{year}', + name: 'countFriday13', + requirements: ['year' => '(?<!-)(?<!\d)\d+'], + methods: ['GET'])] + public function countFriday13(int $year): Response { $fridays = array(); try { @@ -88,25 +89,22 @@ class HomeController extends AbstractController return $this->json($fridays); } - #[Route('/func7/{startDate}/{endDate}', name: 'func7')] // 01-01-2024 - public function func7(string $startDate, string $endDate): Response + #[Route('/diffDays/', name: 'diffDays')] // 01-01-2024 + public function diffDays(DiffDaysRequest $request): Response { - if (DateValidation::validate($startDate) && DateValidation::validate($endDate)) { - try { - $result = $this->functions->diffDays( - new DateTimeImmutable($startDate), - new DateTimeImmutable($endDate) - ); - return $this->json(["The difference of days:" => $result]); - } catch (\Exception $e) { - return new Response($e->getMessage()); - } + try { + $result = $this->functions->diffDays( + new DateTimeImmutable($request->getRequest()->get('startDate')), + new DateTimeImmutable($request->getRequest()->get('endDate')), + ); + return $this->json(["The difference of days:" => $result]); + } catch (\Exception $e) { + return new Response($e->getMessage()); } - return new Response("Invalid date format"); } - #[Route('/func8/{fileName}', name: 'func8')] - public function func8(string $fileName): Response // text.txt + #[Route('/readLogFile', name: 'readLogFile')] + public function readLogFile(Request $request): Response { $filePath = $this->getParameter('kernel.project_dir') . "/public/files/"; $text = $this->functions->readLogFile($filePath . $fileName); -- GitLab