PageRenderTime 54ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Validator/Constraints/LocaleValidator.php

https://bitbucket.org/ingsol/music_sonata
PHP | 57 lines | 23 code | 9 blank | 25 comment | 6 complexity | 8223464df4f687e0d6f0350b0210852a MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, Apache-2.0, JSON, LGPL-3.0, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\ConstraintValidator;
  13. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  14. /**
  15. * Validates whether a value is a valid locale code
  16. *
  17. * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  18. *
  19. * @api
  20. */
  21. class LocaleValidator extends ConstraintValidator
  22. {
  23. /**
  24. * Checks if the passed value is valid.
  25. *
  26. * @param mixed $value The value that should be validated
  27. * @param Constraint $constraint The constraint for the validation
  28. *
  29. * @return Boolean Whether or not the value is valid
  30. *
  31. * @api
  32. */
  33. public function isValid($value, Constraint $constraint)
  34. {
  35. if (null === $value || '' === $value) {
  36. return true;
  37. }
  38. if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
  39. throw new UnexpectedTypeException($value, 'string');
  40. }
  41. $value = (string) $value;
  42. if (!in_array($value, \Symfony\Component\Locale\Locale::getLocales())) {
  43. $this->setMessage($constraint->message, array('{{ value }}' => $value));
  44. return false;
  45. }
  46. return true;
  47. }
  48. }