From 9d38d4590ee69597bac91e2912043bb854058346 Mon Sep 17 00:00:00 2001
From: "Alex. Plokhikh" <a.plohih@iqdev.digital>
Date: Mon, 15 Apr 2024 12:32:12 +0500
Subject: [PATCH] create interfaces | create VlidationService

---
 src/Actions/InterfaceAction.php    | 14 ++++++++++
 src/Entity/InterfaceDataEntity.php | 15 +++++++++++
 src/Service/ValidationService.php  | 41 ++++++++++++++++++++++++++++++
 3 files changed, 70 insertions(+)
 create mode 100644 src/Actions/InterfaceAction.php
 create mode 100644 src/Entity/InterfaceDataEntity.php
 create mode 100644 src/Service/ValidationService.php

diff --git a/src/Actions/InterfaceAction.php b/src/Actions/InterfaceAction.php
new file mode 100644
index 0000000..f4dcce0
--- /dev/null
+++ b/src/Actions/InterfaceAction.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Actions;
+
+use App\Entity\InterfaceDataEntity;
+
+interface InterfaceAction
+{
+    /**
+     * абстрактная функция испольняет описаный функционал в контроллере
+     * @param array $array массив тела реквеста
+     */
+    public function act(InterfaceDataEntity $model): mixed;
+}
diff --git a/src/Entity/InterfaceDataEntity.php b/src/Entity/InterfaceDataEntity.php
new file mode 100644
index 0000000..9580e66
--- /dev/null
+++ b/src/Entity/InterfaceDataEntity.php
@@ -0,0 +1,15 @@
+<?php
+
+namespace App\Entity;
+
+use Symfony\Component\HttpFoundation\Request;
+
+interface InterfaceDataEntity
+{
+    /**
+     * преобразование реквеста в модель
+     * @param Request $request
+     * @return void
+     */
+    public function serialise(Request $request): void;
+}
diff --git a/src/Service/ValidationService.php b/src/Service/ValidationService.php
new file mode 100644
index 0000000..cd9df8c
--- /dev/null
+++ b/src/Service/ValidationService.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace App\Service;
+
+use App\Entity\InterfaceDataEntity;
+use Symfony\Component\HttpFoundation\JsonResponse;
+use Symfony\Component\Validator\Validation;
+
+class ValidationService
+{
+    /**
+     * Валидирование содержимого модели согласно атрибутам парамтров
+     * @param InterfaceDataEntity $model
+     * @return void
+     */
+    public function validate(InterfaceDataEntity $model): void
+    {
+        $validator = Validation::createValidator();
+        $errors = $validator->validate($model);
+
+        $messages = [
+            'message' => 'validation_fail',
+            'errors' => [],
+        ];
+
+        foreach ($errors as $error) {
+            $messages['errors'][] = [
+                'property' => $error->getPropertyPath(),
+                'value' => $error->getInvalidValue(),
+                'message' => $error->getMessage(),
+            ];
+        }
+
+        if (count($messages['errors']) > 0) {
+            $response = new JsonResponse($messages, 400);
+            $response->send();
+
+            exit;
+        }
+    }
+}
\ No newline at end of file
-- 
GitLab