From 258c3f56720dd81781e9d5174f3b181e61a810ba Mon Sep 17 00:00:00 2001
From: "a.shamavov" <a.shamavov@iqdev.digital>
Date: Thu, 11 Apr 2024 14:51:12 +0500
Subject: [PATCH] refactoring

---
 .gitignore                         |  1 +
 src/Action/Functions.php           | 34 ++++++++++++++++++++++++++++++
 src/Controller/HomeController.php  | 34 +++++++++++++-----------------
 src/Validation/ArrayValidation.php | 27 ++++++++++++++++++++++++
 4 files changed, 77 insertions(+), 19 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..3afc804
--- /dev/null
+++ b/src/Action/Functions.php
@@ -0,0 +1,34 @@
+<?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],]
+     */
+}
\ No newline at end of file
diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php
index 0a6be9d..175e9bc 100644
--- a/src/Controller/HomeController.php
+++ b/src/Controller/HomeController.php
@@ -2,35 +2,31 @@
 
 namespace App\Controller;
 
+use App\Action\Functions;
+use App\Validation\ArrayValidation;
 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 function sortPrice(array $array): array
+    private Functions $functions;
+
+    public function __construct(Functions $functions)
     {
-        $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;
+        $this->functions = $functions;
     }
 
-    #[Route('/', name: 'home')]
-    public function home(): Response
+    #[Route('/', name: 'home', methods: ['POST'])]
+    public function home(Request $request): 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);
+        $array = $request->get('arr');
+        $errors = ArrayValidation::validate($array);
+        if (count($errors) > 0) {
+            return new Response((string)$errors);
+        }
+        $array = $this->functions->sortPrice($array);
         return $this->json($array);
     }
 }
diff --git a/src/Validation/ArrayValidation.php b/src/Validation/ArrayValidation.php
new file mode 100644
index 0000000..ea6eb8a
--- /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([
+                        'price' => new Assert\Type('int'),
+                        'count' => new Assert\Type('int')
+                    ])
+                ])
+            ])
+        ]);
+        return $validator->validate($array, $constraints);
+    }
+}
\ No newline at end of file
-- 
GitLab