4b9fd3d6439221512e5be7264466cf76344546a3
[yaffs-website] / vendor / symfony / validator / Constraints / BlankValidator.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\Validator\Constraints;
13
14 use Symfony\Component\Validator\Context\ExecutionContextInterface;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\ConstraintValidator;
17 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
18
19 /**
20  * @author Bernhard Schussek <bschussek@gmail.com>
21  */
22 class BlankValidator extends ConstraintValidator
23 {
24     /**
25      * {@inheritdoc}
26      */
27     public function validate($value, Constraint $constraint)
28     {
29         if (!$constraint instanceof Blank) {
30             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Blank');
31         }
32
33         if ('' !== $value && null !== $value) {
34             if ($this->context instanceof ExecutionContextInterface) {
35                 $this->context->buildViolation($constraint->message)
36                     ->setParameter('{{ value }}', $this->formatValue($value))
37                     ->setCode(Blank::NOT_BLANK_ERROR)
38                     ->addViolation();
39             } else {
40                 $this->buildViolation($constraint->message)
41                     ->setParameter('{{ value }}', $this->formatValue($value))
42                     ->setCode(Blank::NOT_BLANK_ERROR)
43                     ->addViolation();
44             }
45         }
46     }
47 }