src/Resource/Error/errorDisplay.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Resource\Error;
  3. use Symfony\Component\HttpFoundation\JsonResponse;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Validator\ConstraintViolationListInterface;
  7. use Symfony\Component\Validator\Validator\ValidatorInterface;
  8. class errorDisplay {
  9.     private $validator;
  10.     private $request;
  11.     public function __construct(array $request nullValidatorInterface $validator null) {
  12.         if ($validator) {
  13.             $this->validator $validator;
  14.             $this->request $request;
  15.         }
  16.     }
  17.     /**
  18.      * @param ConstraintViolationListInterface $errors
  19.      * @return JsonResponse
  20.      */
  21.     public function show(ConstraintViolationListInterface $errors) {
  22.         $errorDisplay = [];
  23.         foreach ($errors as $error) {
  24.             $errorDisplay[$error->getPropertyPath()] = $error->getMessage();
  25.         }
  26.         return new JsonResponse([
  27.             'success' => false,
  28.             'message' => 'datos incorrectos',
  29.             'messageData' => $errorDisplay
  30.         ], Response::HTTP_BAD_REQUEST);
  31.     }
  32.     /**
  33.      * @param       $val
  34.      * @param       $entity
  35.      * @param string $method
  36.      * @param array $out
  37.      * @param bool $type
  38.      * @return JsonResponse | array
  39.      */
  40.     public function paramValidate($val$entity$method 'post'$out = [], $type true) {
  41.         $obj = new $entity;
  42.         foreach ($val as $key) {
  43.             $b "set" ucfirst($key);
  44.             $obj->$b($this->request[$key]);
  45.             array_push($out$key);
  46.         }
  47.         if (count($b $this->validator->validate($obj)) > 0) {
  48.             if (!is_bool($c $this->validateElements($b$out))) {
  49.                 if ($type) {
  50.                     return new JsonResponse([
  51.                         'success' => false,
  52.                         'data' => 'Datos no validos',
  53.                         'msg' => 'Datos no validos',
  54.                         'message' => 'Datos no validos',
  55.                         'messageData' => $c
  56.                     ], Response::HTTP_BAD_REQUEST);
  57.                 } else {
  58.                     return [
  59.                         'success' => false,
  60.                         'data' => 'Datos no validos',
  61.                         'msg' => 'Datos no validos',
  62.                         'message' => 'Datos no validos',
  63.                         'messageData' => $c
  64.                     ];
  65.                 }
  66.             }
  67.         }
  68.         return $obj;
  69.     }
  70.     /**
  71.      * @param ConstraintViolationListInterface $obj
  72.      * @param array $out
  73.      * @param array $errorDisplay
  74.      * @return array|bool
  75.      */
  76.     private function validateElements(ConstraintViolationListInterface $obj$out$errorDisplay = []) {
  77.         foreach ($obj as $error) {
  78.             if (in_array($error->getpropertyPath(), $out)) {
  79.                 $errorDisplay[$error->getPropertyPath()] = $error->getMessage();
  80.             }
  81.         }
  82.         if (count($errorDisplay) == 0) return true;
  83.         return $errorDisplay;
  84.     }
  85. }