From 6994e1f1475b351cf77f7e4d572d97c0ee0ff694 Mon Sep 17 00:00:00 2001
From: "Alex. Plokhikh" <a.plohih@iqdev.digital>
Date: Mon, 15 Apr 2024 15:58:59 +0500
Subject: [PATCH] create search actions controller and req

---
 src/Actions/IdSearchAction.php        | 29 +++++++++++++++++
 src/Controller/IdSearchController.php | 27 ++++++++++++++++
 src/Requests/UsersRequest.php         | 45 +++++++++++++++++++++++++++
 3 files changed, 101 insertions(+)
 create mode 100644 src/Actions/IdSearchAction.php
 create mode 100644 src/Controller/IdSearchController.php
 create mode 100644 src/Requests/UsersRequest.php

diff --git a/src/Actions/IdSearchAction.php b/src/Actions/IdSearchAction.php
new file mode 100644
index 0000000..6953291
--- /dev/null
+++ b/src/Actions/IdSearchAction.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Actions;
+
+class IdSearchAction
+{
+    /**
+     * Найдет элемент с указаным id
+     * @param array $array - массив, содержащий элементы со структурой
+     * [
+     * 'id' => 30,
+     * 'name' => 'Jhon',
+     * 'age' => 23,
+     * ]
+     * @param $id - ид искомого элемента
+     * @return array|null - найденный элемент/ вернет null при его отсутствии
+     */
+    public static function act(array $array): ?array
+    {
+
+        foreach ($array['users'] as $item){
+            if ($item['id'] === $array['id']){
+                return $item;
+            }
+        }
+
+        return null;
+    }
+}
diff --git a/src/Controller/IdSearchController.php b/src/Controller/IdSearchController.php
new file mode 100644
index 0000000..86ba514
--- /dev/null
+++ b/src/Controller/IdSearchController.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace App\Controller;
+
+use App\Actions\IdSearchAction;
+use App\Requests\UsersRequest;
+use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Symfony\Component\HttpFoundation\JsonResponse;
+use Symfony\Component\Routing\Attribute\Route;
+
+class IdSearchController extends AbstractController
+{
+    /**
+     * Контроллер найдет элемент с указаным id используя sortPrice
+     * @param UsersRequest $request
+     * @param IdSearchAction $action
+     * @return JsonResponse
+     */
+    #[Route('/search', name: 'app_search', methods: ['POST'])]
+    public function index(UsersRequest $request, IdSearchAction $action): JsonResponse
+    {
+        return new JsonResponse(
+            $action->act($request->serialise()),
+            200
+        );
+    }
+}
\ No newline at end of file
diff --git a/src/Requests/UsersRequest.php b/src/Requests/UsersRequest.php
new file mode 100644
index 0000000..1d5bd95
--- /dev/null
+++ b/src/Requests/UsersRequest.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace App\Requests;
+
+use Symfony\Component\Validator\Constraints\All;
+use Symfony\Component\Validator\Constraints\Collection;
+use Symfony\Component\Validator\Constraints\NotBlank;
+use Symfony\Component\Validator\Constraints\Optional;
+use Symfony\Component\Validator\Constraints\Type;
+
+class UsersRequest extends BaseRequest
+{
+    #[Type('integer')]
+    #[NotBlank]
+    public $id;
+
+    #[Type('array')]
+    #[All(
+        constraints: new Collection(fields: [
+                'id' => [
+                    new Type('integer'),
+                    new NotBlank(),
+                ],
+                'name' => [
+                    new Type('string'),
+                    new NotBlank(),
+                ],
+                'age' => new Optional([
+                    new Type('integer'),
+                ])
+            ])
+    )]
+    public $users;
+
+    /**
+     * @return array
+     */
+    public function serialise(): array
+    {
+        return [
+            'id' => $this->id,
+            'users' => $this->users,
+        ];
+    }
+}
\ No newline at end of file
-- 
GitLab