From 297a10894132578775b556ce4f5092008f561f1b Mon Sep 17 00:00:00 2001
From: "a.shamavov" <a.shamavov@iqdev.digital>
Date: Fri, 12 Apr 2024 11:17:11 +0500
Subject: [PATCH] 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 @@
+<?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 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 @@
+<?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
-- 
GitLab