e1ca56ecd737e6f28689e003e00c701480231589
[yaffs-website] / vendor / symfony / validator / Constraints / IsTrueValidator.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 IsTrueValidator extends ConstraintValidator
23 {
24     /**
25      * {@inheritdoc}
26      */
27     public function validate($value, Constraint $constraint)
28     {
29         if (!$constraint instanceof IsTrue) {
30             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\IsTrue');
31         }
32
33         if (null === $value) {
34             return;
35         }
36
37         if (true !== $value && 1 !== $value && '1' !== $value) {
38             if ($this->context instanceof ExecutionContextInterface) {
39                 $this->context->buildViolation($constraint->message)
40                     ->setParameter('{{ value }}', $this->formatValue($value))
41                     ->setCode(IsTrue::NOT_TRUE_ERROR)
42                     ->addViolation();
43             } else {
44                 $this->buildViolation($constraint->message)
45                     ->setParameter('{{ value }}', $this->formatValue($value))
46                     ->setCode(IsTrue::NOT_TRUE_ERROR)
47                     ->addViolation();
48             }
49         }
50     }
51 }