2010-12-15 18:07:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-12-15 18:07:43 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-12-15 18:07:43 +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.
|
2010-12-15 18:07:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Validator\Constraints;
|
|
|
|
|
2013-03-15 11:44:20 +00:00
|
|
|
use Symfony\Component\Intl\Intl;
|
2010-12-15 18:07:43 +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 language code
|
|
|
|
*
|
2012-02-09 16:54:30 +00:00
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-12-15 18:07:43 +00:00
|
|
|
*/
|
|
|
|
class LanguageValidator extends ConstraintValidator
|
|
|
|
{
|
2011-07-20 09:37:57 +01:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2011-07-20 09:37:57 +01:00
|
|
|
*/
|
2012-02-09 17:02:12 +00:00
|
|
|
public function validate($value, Constraint $constraint)
|
2010-12-15 18:07:43 +00:00
|
|
|
{
|
2010-12-21 02:59:17 +00:00
|
|
|
if (null === $value || '' === $value) {
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2010-12-15 18:07:43 +00:00
|
|
|
}
|
|
|
|
|
2011-05-07 20:01:44 +01:00
|
|
|
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
|
2010-12-15 18:07:43 +00:00
|
|
|
throw new UnexpectedTypeException($value, 'string');
|
|
|
|
}
|
|
|
|
|
2011-03-31 13:02:00 +01:00
|
|
|
$value = (string) $value;
|
2013-03-15 15:18:48 +00:00
|
|
|
$languages = Intl::getLanguageBundle()->getLanguageNames();
|
2010-12-15 18:07:43 +00:00
|
|
|
|
2013-03-15 15:18:48 +00:00
|
|
|
if (!isset($languages[$value])) {
|
2014-04-10 16:39:39 +01:00
|
|
|
$this->context->addViolation($constraint->message, array(
|
|
|
|
'{{ value }}' => $value,
|
|
|
|
));
|
2010-12-15 18:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|