From bc77238cf725add821c4197bcdf8eccb47e940ea Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Mon, 8 Apr 2024 13:49:46 +0500 Subject: [PATCH 01/11] add search --- .idea/.gitignore | 8 + .idea/iqdevTranningProgram.iml | 140 ++++++++++++++++ .idea/modules.xml | 8 + .idea/php.xml | 158 ++++++++++++++++++ .idea/phpunit.xml | 10 ++ .../ad8b439416d1e02614f47c5b471c7c4e587dca82 | 0 .idea/sonarlint/issuestore/index.pb | 3 + .../ad8b439416d1e02614f47c5b471c7c4e587dca82 | 0 .idea/sonarlint/securityhotspotstore/index.pb | 3 + .idea/vcs.xml | 7 + src/Controller/HomeController.php | 31 ++-- 11 files changed, 350 insertions(+), 18 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/iqdevTranningProgram.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/php.xml create mode 100644 .idea/phpunit.xml create mode 100644 .idea/sonarlint/issuestore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 create mode 100644 .idea/sonarlint/issuestore/index.pb create mode 100644 .idea/sonarlint/securityhotspotstore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 create mode 100644 .idea/sonarlint/securityhotspotstore/index.pb create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/iqdevTranningProgram.iml b/.idea/iqdevTranningProgram.iml new file mode 100644 index 0000000..c7b3066 --- /dev/null +++ b/.idea/iqdevTranningProgram.iml @@ -0,0 +1,140 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..aedf51d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000..af015eb --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,158 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/phpunit.xml b/.idea/phpunit.xml new file mode 100644 index 0000000..4f8104c --- /dev/null +++ b/.idea/phpunit.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 b/.idea/sonarlint/issuestore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 new file mode 100644 index 0000000..e69de29 diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb new file mode 100644 index 0000000..b6bd383 --- /dev/null +++ b/.idea/sonarlint/issuestore/index.pb @@ -0,0 +1,3 @@ + +Q +!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82 \ No newline at end of file diff --git a/.idea/sonarlint/securityhotspotstore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 b/.idea/sonarlint/securityhotspotstore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 new file mode 100644 index 0000000..e69de29 diff --git a/.idea/sonarlint/securityhotspotstore/index.pb b/.idea/sonarlint/securityhotspotstore/index.pb new file mode 100644 index 0000000..b6bd383 --- /dev/null +++ b/.idea/sonarlint/securityhotspotstore/index.pb @@ -0,0 +1,3 @@ + +Q +!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82 \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..8306744 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 0a6be9d..bcab102 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -8,29 +8,24 @@ use Symfony\Component\Routing\Attribute\Route; class HomeController extends AbstractController { - private function sortPrice(array $array): array + #[Route('/search/{id}', name: 'home_search')] + public function search(int $id): Response { - $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; + $array = [ + ['id'=>10, 'name'=>'Jhon', 'age'=>23], + ['id'=>32, 'name'=>'Alex', 'age'=>34], + ['id'=>54, 'name'=>'Bob', 'age'=>45], + ['id'=>6, 'name'=>'Mike', 'age'=>61], + ]; + $rowId = array_search($id, array_column($array, 'id')); + return $this->json($rowId); } #[Route('/', name: 'home')] public function home(): 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); - return $this->json($array); + return $this->redirectToRoute('home_search', [ + 'id' => 32 + ]); } } -- GitLab From c83d1ae60a0003e781a4fa97d00670879b6d7f75 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Mon, 8 Apr 2024 14:20:52 +0500 Subject: [PATCH 02/11] add uniqElements --- .idea/iqdevTranningProgram.iml | 2 ++ src/Controller/HomeController.php | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/.idea/iqdevTranningProgram.iml b/.idea/iqdevTranningProgram.iml index c7b3066..3f9338e 100644 --- a/.idea/iqdevTranningProgram.iml +++ b/.idea/iqdevTranningProgram.iml @@ -2,6 +2,8 @@ + + diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index bcab102..455f3e5 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -8,24 +8,19 @@ use Symfony\Component\Routing\Attribute\Route; class HomeController extends AbstractController { - #[Route('/search/{id}', name: 'home_search')] - public function search(int $id): Response - { - $array = [ - ['id'=>10, 'name'=>'Jhon', 'age'=>23], - ['id'=>32, 'name'=>'Alex', 'age'=>34], - ['id'=>54, 'name'=>'Bob', 'age'=>45], - ['id'=>6, 'name'=>'Mike', 'age'=>61], - ]; - $rowId = array_search($id, array_column($array, 'id')); - return $this->json($rowId); + private function uniqElements(array $array): array { + return array_unique($array, SORT_REGULAR); } #[Route('/', name: 'home')] public function home(): Response { - return $this->redirectToRoute('home_search', [ - 'id' => 32 - ]); + $arr = [ + ['laravel', 'php'], + ['codeigniter', 'php'], + ['laravel', 'php'], + ['c++', 'java'], + ]; + return $this->json($this->uniqElements($arr)); } } -- GitLab From 8fdc17e7cf9ffd8fb155f4e1177df562aa1be8b5 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Fri, 12 Apr 2024 11:03:43 +0500 Subject: [PATCH 03/11] refactoring --- .idea/.gitignore | 8 - .idea/iqdevTranningProgram.iml | 140 ---------------- .idea/modules.xml | 8 - .idea/php.xml | 158 ------------------ .idea/phpunit.xml | 10 -- .../ad8b439416d1e02614f47c5b471c7c4e587dca82 | 0 .idea/sonarlint/issuestore/index.pb | 3 - .../ad8b439416d1e02614f47c5b471c7c4e587dca82 | 0 .idea/sonarlint/securityhotspotstore/index.pb | 3 - .idea/vcs.xml | 7 - 10 files changed, 337 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/iqdevTranningProgram.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/php.xml delete mode 100644 .idea/phpunit.xml delete mode 100644 .idea/sonarlint/issuestore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 delete mode 100644 .idea/sonarlint/issuestore/index.pb delete mode 100644 .idea/sonarlint/securityhotspotstore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 delete mode 100644 .idea/sonarlint/securityhotspotstore/index.pb delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/iqdevTranningProgram.iml b/.idea/iqdevTranningProgram.iml deleted file mode 100644 index c7b3066..0000000 --- a/.idea/iqdevTranningProgram.iml +++ /dev/null @@ -1,140 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index aedf51d..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml deleted file mode 100644 index af015eb..0000000 --- a/.idea/php.xml +++ /dev/null @@ -1,158 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/phpunit.xml b/.idea/phpunit.xml deleted file mode 100644 index 4f8104c..0000000 --- a/.idea/phpunit.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 b/.idea/sonarlint/issuestore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb deleted file mode 100644 index b6bd383..0000000 --- a/.idea/sonarlint/issuestore/index.pb +++ /dev/null @@ -1,3 +0,0 @@ - -Q -!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82 \ No newline at end of file diff --git a/.idea/sonarlint/securityhotspotstore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 b/.idea/sonarlint/securityhotspotstore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/securityhotspotstore/index.pb b/.idea/sonarlint/securityhotspotstore/index.pb deleted file mode 100644 index b6bd383..0000000 --- a/.idea/sonarlint/securityhotspotstore/index.pb +++ /dev/null @@ -1,3 +0,0 @@ - -Q -!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82 \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 8306744..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file -- GitLab From b02ed450d10550a2b747cf07a7c48ebc0372fc15 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Fri, 12 Apr 2024 11:03:50 +0500 Subject: [PATCH 04/11] refactoring --- .gitignore | 1 + src/Action/Functions.php | 55 ++++++++++++++++++++++++++++++ src/Controller/HomeController.php | 32 +++++++++-------- src/Validation/ArrayValidation.php | 28 +++++++++++++++ 4 files changed, 101 insertions(+), 15 deletions(-) create mode 100644 src/Action/Functions.php create mode 100644 src/Validation/ArrayValidation.php diff --git a/.gitignore b/.gitignore index 4daae38..930e1e2 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 0000000..cdabbc3 --- /dev/null +++ b/src/Action/Functions.php @@ -0,0 +1,55 @@ +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; + } +} diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index bcab102..8623303 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -2,30 +2,32 @@ namespace App\Controller; +use App\Action\Functions; +use App\Validation\ArrayValidation; +use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; class HomeController extends AbstractController { - #[Route('/search/{id}', name: 'home_search')] - public function search(int $id): Response + private Functions $functions; + + public function __construct(Functions $functions) { - $array = [ - ['id'=>10, 'name'=>'Jhon', 'age'=>23], - ['id'=>32, 'name'=>'Alex', 'age'=>34], - ['id'=>54, 'name'=>'Bob', 'age'=>45], - ['id'=>6, 'name'=>'Mike', 'age'=>61], - ]; - $rowId = array_search($id, array_column($array, 'id')); - return $this->json($rowId); + $this->functions = $functions; } - #[Route('/', name: 'home')] - public function home(): Response + #[Route('/', name: 'home', methods: ['POST'])] + public function home(Request $request): Response { - return $this->redirectToRoute('home_search', [ - 'id' => 32 - ]); + $id = $request->query->getInt('id'); + $array = $request->get('arr'); + $errors = ArrayValidation::validate($array); + if (count($errors) > 0) { + return new Response((string)$errors); + } + $result = $this->functions->search($array, $id); + return $this->json($result); } } diff --git a/src/Validation/ArrayValidation.php b/src/Validation/ArrayValidation.php new file mode 100644 index 0000000..739cea1 --- /dev/null +++ b/src/Validation/ArrayValidation.php @@ -0,0 +1,28 @@ + new Assert\Type('int'), + 'name' => new Assert\Type('string'), + 'age' => new Assert\Type('int'), + ]) + ]) + ]) + ]); + return $validator->validate($array, $constraints); + } +} \ No newline at end of file -- GitLab From 297a10894132578775b556ce4f5092008f561f1b Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Fri, 12 Apr 2024 11:17:11 +0500 Subject: [PATCH 05/11] refactoring --- .gitignore | 1 + .idea/sonarlint/issuestore/index.pb | 9 ++- .idea/sonarlint/securityhotspotstore/index.pb | 9 ++- src/Action/Functions.php | 76 +++++++++++++++++++ src/Controller/HomeController.php | 28 ++++--- src/Validation/ArrayValidation.php | 27 +++++++ 6 files changed, 137 insertions(+), 13 deletions(-) create mode 100644 src/Action/Functions.php create mode 100644 src/Validation/ArrayValidation.php diff --git a/.gitignore b/.gitignore index 4daae38..930e1e2 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/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb index b6bd383..697f0bf 100644 --- a/.idea/sonarlint/issuestore/index.pb +++ b/.idea/sonarlint/issuestore/index.pb @@ -1,3 +1,10 @@ Q -!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82 \ No newline at end of file +!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82 +: + +.gitignore,a\5\a5cc2925ca8258af241be7e5b0381edf30266302 +H +src/Action/Functions.php,4\3\43dcb35f966f0fa054ba7993783bf64ca2be218c +R +"src/Validation/ArrayValidation.php,6\0\6037d4b4b463114752d4470f297faf20f6eb091f \ No newline at end of file diff --git a/.idea/sonarlint/securityhotspotstore/index.pb b/.idea/sonarlint/securityhotspotstore/index.pb index b6bd383..697f0bf 100644 --- a/.idea/sonarlint/securityhotspotstore/index.pb +++ b/.idea/sonarlint/securityhotspotstore/index.pb @@ -1,3 +1,10 @@ Q -!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82 \ No newline at end of file +!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82 +: + +.gitignore,a\5\a5cc2925ca8258af241be7e5b0381edf30266302 +H +src/Action/Functions.php,4\3\43dcb35f966f0fa054ba7993783bf64ca2be218c +R +"src/Validation/ArrayValidation.php,6\0\6037d4b4b463114752d4470f297faf20f6eb091f \ No newline at end of file diff --git a/src/Action/Functions.php b/src/Action/Functions.php new file mode 100644 index 0000000..6516c42 --- /dev/null +++ b/src/Action/Functions.php @@ -0,0 +1,76 @@ +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)) + * ) + */ +} + diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 455f3e5..f3bf85d 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -2,25 +2,31 @@ namespace App\Controller; +use App\Action\Functions; +use App\Validation\ArrayValidation; +use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; class HomeController extends AbstractController { - private function uniqElements(array $array): array { - return array_unique($array, SORT_REGULAR); + private Functions $functions; + + public function __construct(Functions $functions) + { + $this->functions = $functions; } - #[Route('/', name: 'home')] - public function home(): Response + #[Route('/', name: 'home', methods: ['POST'])] + public function home(Request $request): Response { - $arr = [ - ['laravel', 'php'], - ['codeigniter', 'php'], - ['laravel', 'php'], - ['c++', 'java'], - ]; - return $this->json($this->uniqElements($arr)); + $array = $request->get('arr'); + $errors = ArrayValidation::validate($array); + if (count($errors) > 0) { + return new Response((string)$errors); + } + $result = $this->functions->uniqElements($array); + return $this->json($result); } } diff --git a/src/Validation/ArrayValidation.php b/src/Validation/ArrayValidation.php new file mode 100644 index 0000000..0206375 --- /dev/null +++ b/src/Validation/ArrayValidation.php @@ -0,0 +1,27 @@ +validate($array, $constraints); + } +} \ No newline at end of file -- GitLab From 2c7549017aa212b55650a6319c83ee643135c929 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Fri, 12 Apr 2024 11:17:59 +0500 Subject: [PATCH 06/11] delete .idea --- .idea/.gitignore | 8 - .idea/iqdevTranningProgram.iml | 142 ---------------- .idea/modules.xml | 8 - .idea/php.xml | 158 ------------------ .idea/phpunit.xml | 10 -- .../ad8b439416d1e02614f47c5b471c7c4e587dca82 | 0 .idea/sonarlint/issuestore/index.pb | 10 -- .../ad8b439416d1e02614f47c5b471c7c4e587dca82 | 0 .idea/sonarlint/securityhotspotstore/index.pb | 10 -- .idea/vcs.xml | 7 - 10 files changed, 353 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/iqdevTranningProgram.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/php.xml delete mode 100644 .idea/phpunit.xml delete mode 100644 .idea/sonarlint/issuestore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 delete mode 100644 .idea/sonarlint/issuestore/index.pb delete mode 100644 .idea/sonarlint/securityhotspotstore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 delete mode 100644 .idea/sonarlint/securityhotspotstore/index.pb delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/iqdevTranningProgram.iml b/.idea/iqdevTranningProgram.iml deleted file mode 100644 index 3f9338e..0000000 --- a/.idea/iqdevTranningProgram.iml +++ /dev/null @@ -1,142 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index aedf51d..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml deleted file mode 100644 index af015eb..0000000 --- a/.idea/php.xml +++ /dev/null @@ -1,158 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/phpunit.xml b/.idea/phpunit.xml deleted file mode 100644 index 4f8104c..0000000 --- a/.idea/phpunit.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 b/.idea/sonarlint/issuestore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb deleted file mode 100644 index 697f0bf..0000000 --- a/.idea/sonarlint/issuestore/index.pb +++ /dev/null @@ -1,10 +0,0 @@ - -Q -!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82 -: - -.gitignore,a\5\a5cc2925ca8258af241be7e5b0381edf30266302 -H -src/Action/Functions.php,4\3\43dcb35f966f0fa054ba7993783bf64ca2be218c -R -"src/Validation/ArrayValidation.php,6\0\6037d4b4b463114752d4470f297faf20f6eb091f \ No newline at end of file diff --git a/.idea/sonarlint/securityhotspotstore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 b/.idea/sonarlint/securityhotspotstore/a/d/ad8b439416d1e02614f47c5b471c7c4e587dca82 deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/securityhotspotstore/index.pb b/.idea/sonarlint/securityhotspotstore/index.pb deleted file mode 100644 index 697f0bf..0000000 --- a/.idea/sonarlint/securityhotspotstore/index.pb +++ /dev/null @@ -1,10 +0,0 @@ - -Q -!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82 -: - -.gitignore,a\5\a5cc2925ca8258af241be7e5b0381edf30266302 -H -src/Action/Functions.php,4\3\43dcb35f966f0fa054ba7993783bf64ca2be218c -R -"src/Validation/ArrayValidation.php,6\0\6037d4b4b463114752d4470f297faf20f6eb091f \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 8306744..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file -- GitLab From 86264a54406f154471202948c6ec81a8500ee685 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Fri, 12 Apr 2024 18:09:52 +0500 Subject: [PATCH 07/11] refactoring --- src/Controller/HomeController.php | 9 ++++----- src/Validation/ArrayValidation.php | 19 ++++--------------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index d37e3ac..ef9c777 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -29,14 +29,13 @@ class HomeController extends AbstractController return $this->json($array); } - #[Route('/', name: 'home', methods: ['POST'])] - public function home(Request $request): Response + #[Route('/func2', name: 'func2', methods: ['POST'])] + public function func2(Request $request): Response { $id = $request->query->getInt('id'); $array = $request->get('arr'); - $errors = ArrayValidation::validate($array); - if (count($errors) > 0) { - return new Response((string)$errors); + if (!ArrayValidation::validateFunc2($array)) { + return new Response("Invalid array"); } $result = $this->functions->search($array, $id); return $this->json($result); diff --git a/src/Validation/ArrayValidation.php b/src/Validation/ArrayValidation.php index 9f715d5..473d0ba 100644 --- a/src/Validation/ArrayValidation.php +++ b/src/Validation/ArrayValidation.php @@ -11,21 +11,10 @@ class ArrayValidation return ctype_digit(implode('',$prices)) && ctype_digit(implode('', $counts)); } - public static function validate(array $array): ConstraintViolationListInterface + public static function validateFunc2(array $array): bool { - $validator = Validation::createValidator(); - $constraints = new Assert\Optional([ - new Assert\Collection([ - new Assert\Optional([ - new Assert\Type('array'), - new Assert\Collection([ - 'id' => new Assert\Type('int'), - 'name' => new Assert\Type('string'), - 'age' => new Assert\Type('int'), - ]) - ]) - ]) - ]); - return $validator->validate($array, $constraints); + $ids = array_column($array, 'id'); + $ages = array_column($array, 'age'); + return ctype_digit(implode('', $ids)) && ctype_digit(implode('', $ages)); } } \ No newline at end of file -- GitLab From 6274eb5db27175ec4ea01445845008015e6a56d0 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Fri, 12 Apr 2024 18:14:40 +0500 Subject: [PATCH 08/11] refactoring --- src/Controller/HomeController.php | 6 +----- src/Validation/ArrayValidation.php | 17 ----------------- 2 files changed, 1 insertion(+), 22 deletions(-) diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 947d15f..bc56c53 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -41,14 +41,10 @@ class HomeController extends AbstractController return $this->json($result); } - #[Route('/', name: 'home', methods: ['POST'])] + #[Route('/func3', 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->uniqElements($array); return $this->json($result); } diff --git a/src/Validation/ArrayValidation.php b/src/Validation/ArrayValidation.php index 3a10d5e..473d0ba 100644 --- a/src/Validation/ArrayValidation.php +++ b/src/Validation/ArrayValidation.php @@ -17,21 +17,4 @@ class ArrayValidation $ages = array_column($array, 'age'); return ctype_digit(implode('', $ids)) && ctype_digit(implode('', $ages)); } - - 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([ - new Assert\Type('string'), - new Assert\Type('string'), - ]) - ]) - ]) - ]); - return $validator->validate($array, $constraints); - } } \ No newline at end of file -- GitLab From bd0baa65040d82032d14a106bb0874138974839e Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Fri, 12 Apr 2024 18:27:08 +0500 Subject: [PATCH 09/11] fix --- src/Controller/HomeController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index bc56c53..e90fe08 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -41,7 +41,7 @@ class HomeController extends AbstractController return $this->json($result); } - #[Route('/func3', name: 'home', methods: ['POST'])] + #[Route('/func3', name: 'func3', methods: ['POST'])] public function home(Request $request): Response { $array = $request->get('arr'); -- GitLab From d7c4543865b8032253635a628595bea3600598d1 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Mon, 15 Apr 2024 14:50:35 +0500 Subject: [PATCH 10/11] refactoring --- src/Validation/ArrayValidation.php | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 src/Validation/ArrayValidation.php diff --git a/src/Validation/ArrayValidation.php b/src/Validation/ArrayValidation.php deleted file mode 100644 index 473d0ba..0000000 --- a/src/Validation/ArrayValidation.php +++ /dev/null @@ -1,20 +0,0 @@ - Date: Mon, 15 Apr 2024 14:50:41 +0500 Subject: [PATCH 11/11] refactoring --- composer.json | 2 +- composer.lock | 107 +++++++++++++++------------ src/Action/Functions.php | 18 ----- src/Controller/HomeController.php | 42 ++++------- src/Requests/BaseRequest.php | 62 ++++++++++++++++ src/Requests/SearchRequest.php | 20 +++++ src/Requests/SortPriceRequest.php | 16 ++++ src/Requests/UniqElementsRequest.php | 16 ++++ 8 files changed, 189 insertions(+), 94 deletions(-) create mode 100644 src/Requests/BaseRequest.php create mode 100644 src/Requests/SearchRequest.php create mode 100644 src/Requests/SortPriceRequest.php create mode 100644 src/Requests/UniqElementsRequest.php 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 bf15706..cf401a8 100644 --- a/src/Action/Functions.php +++ b/src/Action/Functions.php @@ -11,7 +11,6 @@ class Functions * @param array $array * @return array */ - public function sortPrice(array $array): array { $prices = array_column($array, 'price'); @@ -25,13 +24,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 - массив, содержащий элементы со структурой @@ -58,18 +50,8 @@ 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)) - * ) - */ } \ No newline at end of file diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index e90fe08..db9d53a 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -3,49 +3,39 @@ namespace App\Controller; use App\Action\Functions; -use App\Validation\ArrayValidation; +use App\Requests\{ + SortPriceRequest, + SearchRequest, + UniqElementsRequest +}; 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 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); } } diff --git a/src/Requests/BaseRequest.php b/src/Requests/BaseRequest.php new file mode 100644 index 0000000..9bebc7d --- /dev/null +++ b/src/Requests/BaseRequest.php @@ -0,0 +1,62 @@ +populate(); + + if ($this->autoValidateRequest()) { + $this->validate(); + } + } + + public function validate(): void + { + $errors = $this->validator->validate($this); + + $messages = ['message' => 'validation_failed', 'errors' => []]; + + /** @var ConstraintViolation $errors */ + foreach ($errors as $message) { + $messages['errors'][] = [ + 'property' => $message->getPropertyPath(), + 'value' => $message->getInvalidValue(), + 'message' => $message->getMessage(), + ]; + } + + if (count($messages['errors']) > 0) { + $response = new JsonResponse($messages, 201); + $response->send(); + + exit; + } + } + + public function getRequest(): Request + { + return Request::createFromGlobals(); + } + + protected function populate(): void + { + foreach ($this->getRequest()->request->all() as $property => $value) { + if (property_exists($this, $property)) { + $this->{$property} = $value; + } + } + } + + protected function autoValidateRequest(): bool + { + return true; + } +} \ No newline at end of file diff --git a/src/Requests/SearchRequest.php b/src/Requests/SearchRequest.php new file mode 100644 index 0000000..eb03843 --- /dev/null +++ b/src/Requests/SearchRequest.php @@ -0,0 +1,20 @@ + new Assert\Type('int'), + 'name' => new Assert\Type('string'), + 'age' => new Assert\Type('int'), + ]) + ])] + public array $items; + + #[Assert\Type('int')] + public int $id; +} \ No newline at end of file diff --git a/src/Requests/SortPriceRequest.php b/src/Requests/SortPriceRequest.php new file mode 100644 index 0000000..38bdf00 --- /dev/null +++ b/src/Requests/SortPriceRequest.php @@ -0,0 +1,16 @@ + new Assert\Type('int'), + 'count' => new Assert\Type('int') + ]) + ])] + public array $items; +} \ No newline at end of file diff --git a/src/Requests/UniqElementsRequest.php b/src/Requests/UniqElementsRequest.php new file mode 100644 index 0000000..f53156b --- /dev/null +++ b/src/Requests/UniqElementsRequest.php @@ -0,0 +1,16 @@ +