-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
LegacyValidator.php
79 lines (67 loc) · 2.81 KB
/
LegacyValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Validator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
/**
* A validator that supports both the API of Symfony < 2.5 and Symfony 2.5+.
*
* This class is incompatible with PHP versions < 5.3.9, because it implements
* two different interfaces specifying the same method validate():
*
* - {@link \Symfony\Component\Validator\ValidatorInterface}
* - {@link \Symfony\Component\Validator\Validator\ValidatorInterface}
*
* In PHP versions prior to 5.3.9, either use {@link RecursiveValidator} or the
* deprecated class {@link \Symfony\Component\Validator\Validator} instead.
*
* @since 2.5
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @see \Symfony\Component\Validator\ValidatorInterface
* @see \Symfony\Component\Validator\Validator\ValidatorInterface
*
* @deprecated Implemented for backwards compatibility with Symfony < 2.5.
* To be removed in Symfony 3.0.
*/
class LegacyValidator extends RecursiveValidator implements LegacyValidatorInterface
{
public function validate($value, $groups = null, $traverse = false, $deep = false)
{
$numArgs = func_num_args();
// Use new signature if constraints are given in the second argument
if (self::testConstraints($groups) && ($numArgs < 3 || 3 === $numArgs && self::testGroups($traverse))) {
// Rename to avoid total confusion ;)
$constraints = $groups;
$groups = $traverse;
return parent::validate($value, $constraints, $groups);
}
$constraint = new Valid(array('traverse' => $traverse, 'deep' => $deep));
return parent::validate($value, $constraint, $groups);
}
public function validateValue($value, $constraints, $groups = null)
{
return parent::validate($value, $constraints, $groups);
}
public function getMetadataFactory()
{
return $this->metadataFactory;
}
private static function testConstraints($constraints)
{
return null === $constraints || $constraints instanceof Constraint || (is_array($constraints) && (0 === count($constraints) || current($constraints) instanceof Constraint));
}
private static function testGroups($groups)
{
return null === $groups || is_string($groups) || $groups instanceof GroupSequence || (is_array($groups) && (is_string(current($groups)) || current($groups) instanceof GroupSequence));
}
}