From a430fd5a1942cad015efbcb49e226343f6616163 Mon Sep 17 00:00:00 2001
From: "Alex. Plokhikh" <a.plohih@iqdev.digital>
Date: Mon, 15 Apr 2024 12:41:06 +0500
Subject: [PATCH] create prices model sort action and controller

---
 src/Actions/SortPriceAction.php        | 31 ++++++++++++++++++++
 src/Controller/SortPriceController.php | 32 +++++++++++++++++++++
 src/Entity/PricesEntity.php            | 39 ++++++++++++++++++++++++++
 3 files changed, 102 insertions(+)
 create mode 100644 src/Actions/SortPriceAction.php
 create mode 100644 src/Controller/SortPriceController.php
 create mode 100644 src/Entity/PricesEntity.php

diff --git a/src/Actions/SortPriceAction.php b/src/Actions/SortPriceAction.php
new file mode 100644
index 0000000..13d2023
--- /dev/null
+++ b/src/Actions/SortPriceAction.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace App\Actions;
+
+use App\Entity\InterfaceDataEntity;
+
+class SortPriceAction implements InterfaceAction
+{
+    /**
+     * Выполняет сортировку массива по убыванию цены
+     * @param InterfaceDataEntity $model
+     * @return array отсортированный
+     */
+    public function act(InterfaceDataEntity $model): array
+    {
+        $array = $model->prices;
+
+        $priceColumn = array_column($array, "price");
+        $countColumn = array_column($array, "count");
+
+        array_multisort(
+            $priceColumn,
+            SORT_DESC,
+            $countColumn,
+            SORT_ASC,
+            $array,
+        );
+
+        return $array;
+    }
+}
diff --git a/src/Controller/SortPriceController.php b/src/Controller/SortPriceController.php
new file mode 100644
index 0000000..417b461
--- /dev/null
+++ b/src/Controller/SortPriceController.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace App\Controller;
+
+use App\Actions\SortPriceAction;
+use App\Entity\PricesEntity;
+use App\Service\ValidationService;
+use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Symfony\Component\HttpFoundation\JsonResponse;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\Routing\Attribute\Route;
+
+class SortPriceController extends AbstractController
+{
+    /**
+     * Контроллер волняет сортировку массива по убыванию цены используя sortPrice
+     * @param Request $request
+     * @param SortPriceAction $action
+     * @return JsonResponse
+     */
+    #[Route('/sort/price', name: 'app_sort_price', methods: ['POST'])]
+    public function index(Request $request, SortPriceAction $action): JsonResponse
+    {
+        $priceEntity = new PricesEntity();
+        $priceEntity->serialise($request);
+
+        $validation = new ValidationService();
+        $validation->validate($priceEntity);
+
+        return new JsonResponse($action->act($priceEntity));
+    }
+}
diff --git a/src/Entity/PricesEntity.php b/src/Entity/PricesEntity.php
new file mode 100644
index 0000000..2b3257f
--- /dev/null
+++ b/src/Entity/PricesEntity.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace App\Entity;
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\Validator\Constraints\All;
+use Symfony\Component\Validator\Constraints\Collection;
+use Symfony\Component\Validator\Constraints\NotBlank;
+use Symfony\Component\Validator\Constraints\Type;
+use Symfony\Contracts\Service\Attribute\Required;
+
+class PricesEntity implements InterfaceDataEntity
+{
+    #[Type('array')]
+    #[NotBlank()]
+    #[Required()]
+    #[All(
+        constraints: [
+            new Collection(
+                fields: [
+                    'price' => [
+                        new NotBlank(),
+                        new Type('integer'),
+                    ],
+                    'count' => [
+                        new NotBlank(),
+                        new Type('integer'),
+                    ],
+                ],
+            )
+        ]
+    )]
+    public $prices;
+
+    public function serialise(Request $request): void
+    {
+        $this->prices = $request->toArray()['prices'];
+    }
+}
-- 
GitLab