/** * @param ParamInterface $param * @param mixed $paramValue * @param bool $strict * * @throws BadRequestHttpException * @throws \RuntimeException * * @return mixed */ protected function cleanParamWithRequirements(ParamInterface $param, $paramValue, $strict) { $default = $param->getDefault(); $this->checkNotIncompatibleParams($param); if ($default !== null && $default === $paramValue) { return $paramValue; } $constraints = $param->getConstraints($paramValue); if (empty($constraints)) { return $paramValue; } if (null === $this->validator) { throw new \RuntimeException('The ParamFetcher requirements feature requires the symfony/validator component.'); } try { if ($this->validator instanceof ValidatorInterface) { $errors = $this->validator->validate($paramValue, $constraints); } else { $errors = $this->validator->validateValue($paramValue, $constraints); } } catch (ValidatorException $e) { $violation = new ConstraintViolation($e->getMessage(), $e->getMessage(), array(), $paramValue, '', null, null, $e->getCode()); $errors->add($violation); } if (0 < count($errors)) { if ($strict) { throw new BadRequestHttpException($this->violationFormatter->formatList($param, $errors)); } return null === $default ? '' : $default; } return $paramValue; }
/** * @param ParamInterface $param * @param mixed $paramValue * @param bool $strict * * @throws BadRequestHttpException * @throws \RuntimeException * * @return mixed */ protected function cleanParamWithRequirements(ParamInterface $param, $paramValue, $strict) { if (empty($this->container)) { throw new \InvalidArgumentException('The ParamFetcher has been not initialized correctly. ' . 'The container for parameter resolution is missing.'); } $default = $param->getDefault(); $default = $this->resolveValue($this->container, $default); $this->checkNotIncompatibleParams($param); if ($default !== null && $default === $paramValue) { return $paramValue; } $constraints = $param->getConstraints(); $this->resolveConstraints($constraints); if (empty($constraints)) { return $paramValue; } if (null === $this->validator) { throw new \RuntimeException('The ParamFetcher requirements feature requires the symfony/validator component.'); } try { $errors = $this->validator->validate($paramValue, $constraints); } catch (ValidatorException $e) { $violation = new ConstraintViolation($e->getMessage(), $e->getMessage(), array(), $paramValue, '', null, null, $e->getCode()); $errors = new ConstraintViolationList(array($violation)); } if (0 < count($errors)) { if ($strict) { throw new BadRequestHttpException($this->violationFormatter->formatList($param, $errors)); } return null === $default ? '' : $default; } return $paramValue; }