2011-01-01 23:34:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2011-01-01 23:34:01 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-01 23:34:01 +00:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2011-01-01 23:34:01 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Validator\Constraints;
|
|
|
|
|
2011-01-01 23:34:01 +00:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates whether a value is a valid locale code
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-01 23:34:01 +00:00
|
|
|
*/
|
|
|
|
class LocaleValidator extends ConstraintValidator
|
|
|
|
{
|
2011-07-20 09:37:57 +01:00
|
|
|
/**
|
|
|
|
* Checks if the passed value is valid.
|
|
|
|
*
|
|
|
|
* @param mixed $value The value that should be validated
|
|
|
|
* @param Constraint $constraint The constrain for the validation
|
|
|
|
*
|
|
|
|
* @return Boolean Whether or not the value is valid
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2011-01-01 23:34:01 +00:00
|
|
|
public function isValid($value, Constraint $constraint)
|
|
|
|
{
|
|
|
|
if (null === $value || '' === $value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-07 20:01:44 +01:00
|
|
|
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
|
2011-01-01 23:34:01 +00:00
|
|
|
throw new UnexpectedTypeException($value, 'string');
|
|
|
|
}
|
|
|
|
|
2011-03-31 13:02:00 +01:00
|
|
|
$value = (string) $value;
|
2011-01-01 23:34:01 +00:00
|
|
|
|
|
|
|
if (!in_array($value, \Symfony\Component\Locale\Locale::getLocales())) {
|
|
|
|
$this->setMessage($constraint->message, array('{{ value }}' => $value));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|