Skip to content
Snippets Groups Projects
Commit 2b3fa047 authored by Александр Плохих's avatar Александр Плохих :waxing_gibbous_moon:
Browse files

sumplify solution

parent a430fd5a
No related branches found
No related tags found
1 merge request!1Ptps|controller 1
<?php
namespace App\Actions;
use App\Entity\InterfaceDataEntity;
interface InterfaceAction
{
/**
* абстрактная функция испольняет описаный функционал в контроллере
* @param array $array массив тела реквеста
*/
public function act(InterfaceDataEntity $model): mixed;
}
<?php
namespace App\Entity;
use Symfony\Component\HttpFoundation\Request;
interface InterfaceDataEntity
{
/**
* преобразование реквеста в модель
* @param Request $request
* @return void
*/
public function serialise(Request $request): void;
}
<?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'];
}
}
<?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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment