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/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb index b6bd383fba48b306652314f6a74176bf887010fd..697f0bf028fecaf8d0bb0cfae5a2ec9b8090f4ba 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 b6bd383fba48b306652314f6a74176bf887010fd..697f0bf028fecaf8d0bb0cfae5a2ec9b8090f4ba 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 0000000000000000000000000000000000000000..6516c42b3dfbe6ac52bf59a84e2837b9632bd918 --- /dev/null +++ b/src/Action/Functions.php @@ -0,0 +1,76 @@ +<?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)) + * ) + */ +} + diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 455f3e5958c6b759a20685aefcf60cc135a9cf82..f3bf85d700c8b460af2184eff752c5c79046f89a 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 0000000000000000000000000000000000000000..0206375d384c660f8a3e3e1250258d58108eb5b8 --- /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([ + new Assert\Type('string'), + new Assert\Type('string'), + ]) + ]) + ]) + ]); + return $validator->validate($array, $constraints); + } +} \ No newline at end of file