Commit a1f25123 authored by Адлан Шамавов's avatar Адлан Шамавов
Browse files

temp commit

parent c6ab1ce2
Loading
Loading
Loading
Loading
+62 −0
Original line number Diff line number Diff line
<?php

namespace App\Requests;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\Validator\ValidatorInterface;

abstract class BaseRequest
{
    public function __construct(protected ValidatorInterface $validator)
    {
        $this->populate();

        if ($this->autoValidateRequest()) {
            $this->validate();
        }
    }

    public function validate(): void
    {
        $errors = $this->validator->validate($this);

        $messages = ['message' => 'validation_failed', 'errors' => []];

        /** @var ConstraintViolation $errors */
        foreach ($errors as $message) {
            $messages['errors'][] = [
                'property' => $message->getPropertyPath(),
                'value' => $message->getInvalidValue(),
                'message' => $message->getMessage(),
            ];
        }

        if (count($messages['errors']) > 0) {
            $response = new JsonResponse($messages, 201);
            $response->send();

            exit;
        }
    }

    public function getRequest(): Request
    {
        return Request::createFromGlobals();
    }

    protected function populate(): void
    {
        foreach ($this->getRequest()->request->all() as $property => $value) {
            if (property_exists($this, $property)) {
                $this->{$property} = $value;
            }
        }
    }

    protected function autoValidateRequest(): bool
    {
        return true;
    }
}
 No newline at end of file
+14 −0
Original line number Diff line number Diff line
<?php

namespace App\Requests;

use Symfony\Component\Validator\Constraints as Assert;

class DiffDaysRequest extends BaseRequest
{
    #[Assert\Type('date')]
    public $startDate;

    #[Assert\Type('date')]
    public $endDate;
}
 No newline at end of file
+16 −0
Original line number Diff line number Diff line
<?php

namespace App\Requests;

use Symfony\Component\Validator\Constraints as Assert;

class MenuRequest extends BaseRequest
{
    #[Assert\All([
        new Assert\Collection([
            'name' => new Assert\Type('string'),
            'depth' => new Assert\Type('int'),
        ])
    ])]
    public array $items;
}
 No newline at end of file
+20 −0
Original line number Diff line number Diff line
<?php

namespace App\Requests;

use Symfony\Component\Validator\Constraints as Assert;

class SearchRequest extends BaseRequest
{
    #[Assert\All([
        new Assert\Collection([
            'id' => new Assert\Type('int'),
            'name' => new Assert\Type('string'),
            'age' => new Assert\Type('int'),
        ])
    ])]
    public array $items;

    #[Assert\Type('int')]
    public int $id;
}
 No newline at end of file
+16 −0
Original line number Diff line number Diff line
<?php

namespace App\Requests;

use Symfony\Component\Validator\Constraints as Assert;

class SortPriceRequest extends BaseRequest
{
    #[Assert\All([
        new Assert\Collection([
            'price' => new Assert\Type('int'),
            'count' => new Assert\Type('int')
        ])
    ])]
    public array $items;
}
 No newline at end of file
Loading