Skip to content

Commit

Permalink
Merge pull request #945 from alekitto/master
Browse files Browse the repository at this point in the history
2.5 validator API compatibility
  • Loading branch information
lsmith77 committed Jan 23, 2015
2 parents a21ce4e + 4ef8bf0 commit 2437a4f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
32 changes: 25 additions & 7 deletions Request/AbstractRequestBodyParamConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Serializer\Exception\Exception as SymfonySerializerException;
use Symfony\Component\Validator\ValidatorInterface;
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use JMS\Serializer\Exception\UnsupportedFormatException;
Expand Down Expand Up @@ -45,7 +46,7 @@ abstract class AbstractRequestBodyParamConverter implements ParamConverterInterf
* @param array|null $groups An array of groups to be used in the serialization context
* @param string|null $version A version string to be used in the serialization context
* @param object $serializer
* @param ValidatorInterface $validator
* @param LegacyValidatorInterface|ValidatorInterface $validator
* @param string|null $validationErrorsArgument
*
* @throws \InvalidArgumentException
Expand All @@ -54,7 +55,7 @@ public function __construct(
$serializer,
$groups = null,
$version = null,
ValidatorInterface $validator = null,
$validator = null,
$validationErrorsArgument = null
) {
$this->serializer = $serializer;
Expand All @@ -67,6 +68,15 @@ public function __construct(
$this->context['version'] = $version;
}

if ($validator !== null && !$validator instanceof LegacyValidatorInterface && !$validator instanceof ValidatorInterface) {
throw new \InvalidArgumentException(sprintf(
'Validator has expected to be an instance of %s or %s, "%s" given',
'Symfony\Component\Validator\ValidatorInterface',
'Symfony\Component\Validator\Validator\ValidatorInterface',
get_class($validator)
));
}

if (null !== $validator && null === $validationErrorsArgument) {
throw new \InvalidArgumentException('"$validationErrorsArgument" cannot be null when using the validator');
}
Expand Down Expand Up @@ -119,14 +129,21 @@ protected function execute(Request $request, ParamConverter $configuration)

if (null !== $this->validator) {
$validatorOptions = $this->getValidatorOptions($options);
$request->attributes->set(
$this->validationErrorsArgument,
$this->validator->validate(

if ($this->validator instanceof ValidatorInterface) {
$errors = $this->validator->validate($object, null, $validatorOptions['groups']);
} else {
$errors = $this->validator->validate(
$object,
$validatorOptions['groups'],
$validatorOptions['traverse'],
$validatorOptions['deep']
)
);
}

$request->attributes->set(
$this->validationErrorsArgument,
$errors
);
}

Expand Down Expand Up @@ -176,3 +193,4 @@ protected function getValidatorOptions(array $options)
return $resolver->resolve(isset($options['validator']) ? $options['validator'] : array());
}
}

24 changes: 18 additions & 6 deletions Request/ParamFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\ValidatorInterface;
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

/**
* Helper to validate parameters of the active request.
Expand All @@ -46,19 +47,26 @@ class ParamFetcher implements ParamFetcherInterface
/**
* Initializes fetcher.
*
* For symfony 2.5 or higher use 2.5-bc validation API https://github.com/symfony/symfony/blob/master/UPGRADE-2.5.md#validator
*
* @param ParamReader $paramReader
* @param Request $request
* @param ValidatorInterface $validator
* @param ValidatorInterface|LegacyValidatorInterface $validator
* @param ViolationFormatterInterface $violationFormatter
*/
public function __construct(ParamReader $paramReader, Request $request, ViolationFormatterInterface $violationFormatter, ValidatorInterface $validator = null)
public function __construct(ParamReader $paramReader, Request $request, ViolationFormatterInterface $violationFormatter, $validator = null)
{
$this->paramReader = $paramReader;
$this->request = $request;
$this->violationFormatter = $violationFormatter;
$this->validator = $validator;

if ($validator !== null && !$validator instanceof LegacyValidatorInterface && !$validator instanceof ValidatorInterface) {
throw new \InvalidArgumentException(sprintf(
'Validator has expected to be an instance of %s or %s, "%s" given',
'Symfony\Component\Validator\ValidatorInterface',
'Symfony\Component\Validator\Validator\ValidatorInterface',
get_class($validator)
));
}
}

/**
Expand Down Expand Up @@ -218,7 +226,11 @@ public function cleanParamWithRequirements(Param $config, $param, $strict)
}
}

$errors = $this->validator->validateValue($param, $constraint);
if ($this->validator instanceof ValidatorInterface) {
$errors = $this->validator->validate($param, $constraint);
} else {
$errors = $this->validator->validateValue($param, $constraint);
}

if (0 !== count($errors)) {
if ($strict) {
Expand Down

0 comments on commit 2437a4f

Please sign in to comment.