1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Polish Validator Bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Grzegorz Koziński |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Kiczort\PolishValidatorBundle\Validator\Constraints; |
13
|
|
|
|
14
|
|
|
use Kiczort\PolishValidator\ValidatorInterface; |
15
|
|
|
use Symfony\Component\Validator\Constraint; |
16
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
17
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
18
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Grzegorz Koziński <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
abstract class ValidatorAbstract extends ConstraintValidator |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @return ValidatorInterface |
28
|
|
|
*/ |
29
|
|
|
abstract public function getBaseValidator(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Constraint $constraint |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
abstract public function getValidationOptions(Constraint $constraint); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
abstract public function getValidatorConstraintClass(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Checks if the passed value is valid. |
44
|
|
|
* |
45
|
|
|
* @param mixed $value The value that should be validated |
46
|
|
|
* @param Constraint $constraint The constraint for the validation |
47
|
|
|
*/ |
48
|
|
|
public function validate($value, Constraint $constraint) |
49
|
|
|
{ |
50
|
|
|
$constraintClass = $this->getValidatorConstraintClass(); |
51
|
|
|
|
52
|
|
|
if (get_class($constraint) !== $constraintClass) { |
53
|
|
|
throw new UnexpectedTypeException($constraint, $constraintClass); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (null === $value || '' === $value) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { |
61
|
|
|
throw new UnexpectedTypeException($value, 'string'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$value = (string) $value; |
65
|
|
|
|
66
|
|
|
$validator = $this->getBaseValidator(); |
67
|
|
|
if (!$validator->isValid($value, $this->getValidationOptions($constraint))) { |
68
|
|
|
if ($this->context instanceof ExecutionContextInterface) { |
69
|
|
|
$this->context->buildViolation($constraint->message) |
70
|
|
|
->setParameter('{{ value }}', $this->formatValue($value)) |
71
|
|
|
->addViolation(); |
72
|
|
|
} else { |
73
|
|
|
$this->buildViolation($constraint->message) |
|
|
|
|
74
|
|
|
->setParameter('{{ value }}', $this->formatValue($value)) |
75
|
|
|
->addViolation(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.