Commit 2b3fa047 authored by Александр Плохих's avatar Александр Плохих 🌔
Browse files

sumplify solution

parent a430fd5a
Loading
Loading
Loading
Loading

src/Actions/InterfaceAction.php

deleted100644 → 0
+0 −14
Original line number Diff line number Diff line
<?php

namespace App\Actions;

use App\Entity\InterfaceDataEntity;

interface InterfaceAction
{
    /**
     * абстрактная функция испольняет описаный функционал в контроллере
     * @param array $array массив тела реквеста
     */
    public function act(InterfaceDataEntity $model): mixed;
}
+0 −15
Original line number Diff line number Diff line
<?php

namespace App\Entity;

use Symfony\Component\HttpFoundation\Request;

interface InterfaceDataEntity
{
    /**
     * преобразование реквеста в модель
     * @param Request $request
     * @return void
     */
    public function serialise(Request $request): void;
}

src/Entity/PricesEntity.php

deleted100644 → 0
+0 −39
Original line number Diff line number Diff line
<?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'];
    }
}

src/Service/ValidationService.php

deleted100644 → 0
+0 −41
Original line number Diff line number Diff line
<?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