Skip to content

Commit 2437a4f

Browse files
committed
Merge pull request #945 from alekitto/master
2.5 validator API compatibility
2 parents a21ce4e + 4ef8bf0 commit 2437a4f

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

Request/AbstractRequestBodyParamConverter.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
1717
use Symfony\Component\OptionsResolver\OptionsResolver;
1818
use Symfony\Component\Serializer\Exception\Exception as SymfonySerializerException;
19-
use Symfony\Component\Validator\ValidatorInterface;
19+
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
20+
use Symfony\Component\Validator\Validator\ValidatorInterface;
2021
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
2122
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
2223
use JMS\Serializer\Exception\UnsupportedFormatException;
@@ -45,7 +46,7 @@ abstract class AbstractRequestBodyParamConverter implements ParamConverterInterf
4546
* @param array|null $groups An array of groups to be used in the serialization context
4647
* @param string|null $version A version string to be used in the serialization context
4748
* @param object $serializer
48-
* @param ValidatorInterface $validator
49+
* @param LegacyValidatorInterface|ValidatorInterface $validator
4950
* @param string|null $validationErrorsArgument
5051
*
5152
* @throws \InvalidArgumentException
@@ -54,7 +55,7 @@ public function __construct(
5455
$serializer,
5556
$groups = null,
5657
$version = null,
57-
ValidatorInterface $validator = null,
58+
$validator = null,
5859
$validationErrorsArgument = null
5960
) {
6061
$this->serializer = $serializer;
@@ -67,6 +68,15 @@ public function __construct(
6768
$this->context['version'] = $version;
6869
}
6970

71+
if ($validator !== null && !$validator instanceof LegacyValidatorInterface && !$validator instanceof ValidatorInterface) {
72+
throw new \InvalidArgumentException(sprintf(
73+
'Validator has expected to be an instance of %s or %s, "%s" given',
74+
'Symfony\Component\Validator\ValidatorInterface',
75+
'Symfony\Component\Validator\Validator\ValidatorInterface',
76+
get_class($validator)
77+
));
78+
}
79+
7080
if (null !== $validator && null === $validationErrorsArgument) {
7181
throw new \InvalidArgumentException('"$validationErrorsArgument" cannot be null when using the validator');
7282
}
@@ -119,14 +129,21 @@ protected function execute(Request $request, ParamConverter $configuration)
119129

120130
if (null !== $this->validator) {
121131
$validatorOptions = $this->getValidatorOptions($options);
122-
$request->attributes->set(
123-
$this->validationErrorsArgument,
124-
$this->validator->validate(
132+
133+
if ($this->validator instanceof ValidatorInterface) {
134+
$errors = $this->validator->validate($object, null, $validatorOptions['groups']);
135+
} else {
136+
$errors = $this->validator->validate(
125137
$object,
126138
$validatorOptions['groups'],
127139
$validatorOptions['traverse'],
128140
$validatorOptions['deep']
129-
)
141+
);
142+
}
143+
144+
$request->attributes->set(
145+
$this->validationErrorsArgument,
146+
$errors
130147
);
131148
}
132149

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

Request/ParamFetcher.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
2121
use Symfony\Component\Validator\Constraints\Regex;
2222
use Symfony\Component\Validator\Constraints\NotBlank;
23-
use Symfony\Component\Validator\ValidatorInterface;
23+
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
24+
use Symfony\Component\Validator\Validator\ValidatorInterface;
2425

2526
/**
2627
* Helper to validate parameters of the active request.
@@ -46,19 +47,26 @@ class ParamFetcher implements ParamFetcherInterface
4647
/**
4748
* Initializes fetcher.
4849
*
49-
* For symfony 2.5 or higher use 2.5-bc validation API https://github.com/symfony/symfony/blob/master/UPGRADE-2.5.md#validator
50-
*
5150
* @param ParamReader $paramReader
5251
* @param Request $request
53-
* @param ValidatorInterface $validator
52+
* @param ValidatorInterface|LegacyValidatorInterface $validator
5453
* @param ViolationFormatterInterface $violationFormatter
5554
*/
56-
public function __construct(ParamReader $paramReader, Request $request, ViolationFormatterInterface $violationFormatter, ValidatorInterface $validator = null)
55+
public function __construct(ParamReader $paramReader, Request $request, ViolationFormatterInterface $violationFormatter, $validator = null)
5756
{
5857
$this->paramReader = $paramReader;
5958
$this->request = $request;
6059
$this->violationFormatter = $violationFormatter;
6160
$this->validator = $validator;
61+
62+
if ($validator !== null && !$validator instanceof LegacyValidatorInterface && !$validator instanceof ValidatorInterface) {
63+
throw new \InvalidArgumentException(sprintf(
64+
'Validator has expected to be an instance of %s or %s, "%s" given',
65+
'Symfony\Component\Validator\ValidatorInterface',
66+
'Symfony\Component\Validator\Validator\ValidatorInterface',
67+
get_class($validator)
68+
));
69+
}
6270
}
6371

6472
/**
@@ -218,7 +226,11 @@ public function cleanParamWithRequirements(Param $config, $param, $strict)
218226
}
219227
}
220228

221-
$errors = $this->validator->validateValue($param, $constraint);
229+
if ($this->validator instanceof ValidatorInterface) {
230+
$errors = $this->validator->validate($param, $constraint);
231+
} else {
232+
$errors = $this->validator->validateValue($param, $constraint);
233+
}
222234

223235
if (0 !== count($errors)) {
224236
if ($strict) {

0 commit comments

Comments
 (0)