src/Requests/BaseRequest.php line 120

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Requests;
  3. use EADPlataforma\Enum\ContentTypeEnum;
  4. use EADPlataforma\Error\ActionInvalidException;
  5. use EADPlataforma\Error\FieldException;
  6. use EADPlataforma\Error\EmptyBodyException;
  7. use EADPlataforma\Error\UnsupportedContentTypeException;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Validator\ConstraintViolation;
  10. use Symfony\Component\Validator\Validator\ValidatorInterface;
  11. abstract class BaseRequest
  12. {
  13.     protected $validator;
  14.     /**
  15.      * @var Request
  16.      */
  17.     public $request;
  18.     /**
  19.      * @var array
  20.      */
  21.     public $data;
  22.     /**
  23.      * @var string
  24.      */
  25.     private $method;
  26.     /**
  27.      * @var string|null
  28.      */
  29.     private $contentType;
  30.     /**
  31.      * @var array
  32.      */
  33.     private $routeParams;
  34.     /**
  35.      * @var bool
  36.      */
  37.     private $isFormData;
  38.     /**
  39.      * @throws ActionInvalidException
  40.      * @throws EmptyBodyException
  41.      * @throws UnsupportedContentTypeException
  42.      */
  43.     public function __construct(ValidatorInterface $validatorRequest $request)
  44.     {
  45.         $this->validator $validator;
  46.         $this->request $request;
  47.         $this->method $request->getMethod();
  48.         $this->contentType $request->headers->get('Content-Type');
  49.         $this->routeParams $request->attributes->get('_route_params');
  50.         $this->isFormData stristr($this->contentTypeContentTypeEnum::MULTPART_FORMDATA);
  51.         if(
  52.             $this->method !== Request::METHOD_GET && 
  53.             $this->contentType !== ContentTypeEnum::APPLICATION_JSON &&
  54.             !$this->isFormData
  55.         ){
  56.             throw new UnsupportedContentTypeException;
  57.         }
  58.         //TO DO -> Change switch to MATCH in php 8
  59.         switch ($this->method)
  60.         {
  61.             case Request::METHOD_GET:
  62.                 $this->data $this->request->query->all();
  63.                 break;
  64.             case Request::METHOD_DELETE:
  65.                 $this->data $this->request->attributes->get('_route_params');
  66.                 break;
  67.             default:
  68.                 $this->data $this->getData();
  69.                 break;
  70.         }
  71.         $this->populate();
  72.         if ($this->autoValidateRequest()) {
  73.             $this->validate();
  74.         }
  75.     }
  76.     public function getData(): ?array
  77.     {
  78.         if(!$this->isFormData){
  79.             return $this->request->toArray();
  80.         }
  81.         $data $this->request->request->all();
  82.         foreach ($this->request->files as $key => $file) {
  83.             $data[$key] = $file;
  84.         }
  85.         return $data;
  86.     }
  87.     /**
  88.      * @throws ActionInvalidException
  89.      * @throws EmptyBodyException
  90.      */
  91.     public function validate()
  92.     {
  93.         if(
  94.             (
  95.                 $this->method !== Request::METHOD_GET && 
  96.                 $this->method !== Request::METHOD_DELETE
  97.             ) && empty($this->data)
  98.         ){
  99.             throw new EmptyBodyException("Empty request body");
  100.         }
  101.         $errors $this->validator->validate($this);
  102.         $messages = ['errors' => []];
  103.         /** @var ConstraintViolation $errors */
  104.         foreach ($errors as $message) {
  105.             $messages['errors'][] = [
  106.                 'property' => $message->getPropertyPath(),
  107.                 'value' => $message->getInvalidValue(),
  108.                 'message' => $message->getMessage(),
  109.             ];
  110.         }
  111.         if (count($messages['errors']) > 0) {
  112.             throw new FieldException("FieldException"$messages['errors']);
  113.         }
  114.     }
  115.     protected function populate(): void
  116.     {
  117.         if(isset($this->routeParams)) {
  118.             $this->data array_merge($this->data$this->routeParams);
  119.         }
  120.         foreach ($this->data as $property => $value) {
  121.             if (property_exists($this$property)) {
  122.                 $this->{$property} = $value;
  123.             }
  124.         }
  125.     }
  126.     protected function autoValidateRequest(): bool
  127.     {
  128.         return true;
  129.     }
  130. }