db backup prior to drupal security update
[yaffs-website] / vendor / symfony / validator / Constraints / LocaleValidator.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\Intl\Intl;
15 use Symfony\Component\Validator\Context\ExecutionContextInterface;
16 use Symfony\Component\Validator\Constraint;
17 use Symfony\Component\Validator\ConstraintValidator;
18 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19
20 /**
21  * Validates whether a value is a valid locale code.
22  *
23  * @author Bernhard Schussek <bschussek@gmail.com>
24  */
25 class LocaleValidator extends ConstraintValidator
26 {
27     /**
28      * {@inheritdoc}
29      */
30     public function validate($value, Constraint $constraint)
31     {
32         if (!$constraint instanceof Locale) {
33             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Locale');
34         }
35
36         if (null === $value || '' === $value) {
37             return;
38         }
39
40         if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
41             throw new UnexpectedTypeException($value, 'string');
42         }
43
44         $value = (string) $value;
45         $locales = Intl::getLocaleBundle()->getLocaleNames();
46         $aliases = Intl::getLocaleBundle()->getAliases();
47
48         if (!isset($locales[$value]) && !in_array($value, $aliases)) {
49             if ($this->context instanceof ExecutionContextInterface) {
50                 $this->context->buildViolation($constraint->message)
51                     ->setParameter('{{ value }}', $this->formatValue($value))
52                     ->setCode(Locale::NO_SUCH_LOCALE_ERROR)
53                     ->addViolation();
54             } else {
55                 $this->buildViolation($constraint->message)
56                     ->setParameter('{{ value }}', $this->formatValue($value))
57                     ->setCode(Locale::NO_SUCH_LOCALE_ERROR)
58                     ->addViolation();
59             }
60         }
61     }
62 }