Beispiel #1
0
 /**
  * Create violation error response
  *
  * @param ViolationListException $exception
  *
  * @return JsonResponse
  */
 public function createViolationErrorResponse(ViolationListException $exception)
 {
     $errorData = [];
     /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */
     foreach ($exception->getViolationList() as $violation) {
         $errorData[$violation->getPropertyPath()] = $violation->getMessage();
     }
     // Try get code from errors storage via exception
     $errors = $this->handler->getErrors();
     $code = $errors->hasException($exception) ? $errors->getExceptionCode($exception) : 0;
     return $this->createErrorResponse($code, null, $errorData);
 }
 /**
  * Resolve request parameter
  *
  * @param ActionInterface      $action
  * @param CallableInterface    $callable
  * @param array                $inputArguments
  * @param \ReflectionParameter $parameter
  *
  * @return object
  *
  * @throws \Exception
  */
 private function resolveRequest(ActionInterface $action, CallableInterface $callable, array $inputArguments, \ReflectionParameter $parameter)
 {
     $class = $parameter->getClass();
     if ($class->isInterface()) {
         throw new \RuntimeException(sprintf('Could not create instance via interface for parameter "%s" in method "%s". ' . 'You must set the class for type hinting.', $parameter->getName(), Reflection::getCalledMethod($callable->getReflection())));
     }
     if ($class->isAbstract()) {
         throw new \RuntimeException(sprintf('Could not create instance via abstract class for parameter "%s" in method "%s". ' . 'You must set the real class for type hinting.', $parameter->getName(), Reflection::getCalledMethod($callable->getReflection())));
     }
     /** @var RequestInterface $request */
     $request = $class->newInstance();
     // Map arguments
     $this->objectMapper->map($request, $inputArguments, ObjectMetadata::DEFAULT_GROUP);
     // Validate process
     if ($this->validator) {
         if ($this->validator instanceof VarTagValidatorInterface) {
             $violationList = $this->validator->validateObjectByVarTags($request);
             if (count($violationList)) {
                 throw ViolationListException::create($violationList);
             }
         }
         $violationList = $this->validator->validate($request);
         if (count($violationList)) {
             throw ViolationListException::create($violationList);
         }
     }
     return $request;
 }