[Validator] add exception when intl component not found

This commit is contained in:
Rudy Onfroy 2018-09-19 15:51:57 +02:00 committed by Nicolas Grekas
parent 6856c023e3
commit b6f29f4721
8 changed files with 42 additions and 6 deletions

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\LogicException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
@ -68,10 +69,11 @@ class BicValidator extends ConstraintValidator
return;
}
// next 2 letters must be alphabetic (country code)
if (!class_exists(Intl::class)) {
throw new \LogicException('The "symfony/intl" component is required to use the Bic constraint.');
throw new LogicException('The "symfony/intl" component is required to use the Bic constraint.');
}
// next 2 letters must be alphabetic (country code)
$countries = Intl::getRegionBundle()->getCountryNames();
if (!isset($countries[substr($canonicalize, 4, 2)])) {
$this->context->buildViolation($constraint->message)

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\LogicException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
@ -40,6 +41,10 @@ class CountryValidator extends ConstraintValidator
throw new UnexpectedTypeException($value, 'string');
}
if (!class_exists(Intl::class)) {
throw new LogicException('The "symfony/intl" component is required to use the Country constraint.');
}
$value = (string) $value;
$countries = Intl::getRegionBundle()->getCountryNames();

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\LogicException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
@ -41,6 +42,10 @@ class CurrencyValidator extends ConstraintValidator
throw new UnexpectedTypeException($value, 'string');
}
if (!class_exists(Intl::class)) {
throw new LogicException('The "symfony/intl" component is required to use the Currency constraint.');
}
$value = (string) $value;
$currencies = Intl::getCurrencyBundle()->getCurrencyNames();

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\LogicException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
@ -40,6 +41,10 @@ class LanguageValidator extends ConstraintValidator
throw new UnexpectedTypeException($value, 'string');
}
if (!class_exists(Intl::class)) {
throw new LogicException('The "symfony/intl" component is required to use the Language constraint.');
}
$value = (string) $value;
$languages = Intl::getLanguageBundle()->getLanguageNames();

View File

@ -0,0 +1,16 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Exception;
class LogicException extends \LogicException implements ExceptionInterface
{
}

View File

@ -11,6 +11,8 @@
namespace Symfony\Component\Validator\Mapping\Factory;
use Symfony\Component\Validator\Exception\LogicException;
/**
* Metadata factory that does not store metadata.
*
@ -27,7 +29,7 @@ class BlackHoleMetadataFactory implements MetadataFactoryInterface
*/
public function getMetadataFor($value)
{
throw new \LogicException('This class does not support metadata.');
throw new LogicException('This class does not support metadata.');
}
/**

View File

@ -17,7 +17,7 @@ use Symfony\Component\Validator\Mapping\Factory\BlackHoleMetadataFactory;
class BlackHoleMetadataFactoryTest extends TestCase
{
/**
* @expectedException \LogicException
* @expectedException \Symfony\Component\Validator\Exception\LogicException
*/
public function testGetMetadataForThrowsALogicException()
{

View File

@ -17,6 +17,7 @@ use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Cache\ArrayCache;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Component\Validator\Context\ExecutionContextFactory;
use Symfony\Component\Validator\Exception\LogicException;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Component\Validator\Mapping\Cache\CacheInterface;
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
@ -191,8 +192,8 @@ class ValidatorBuilder implements ValidatorBuilderInterface
}
if (null === $annotationReader) {
if (!class_exists('Doctrine\Common\Annotations\AnnotationReader') || !class_exists('Doctrine\Common\Cache\ArrayCache')) {
throw new \RuntimeException('Enabling annotation based constraint mapping requires the packages doctrine/annotations and doctrine/cache to be installed.');
if (!class_exists(AnnotationReader::class) || !class_exists(ArrayCache::class)) {
throw new LogicException('Enabling annotation based constraint mapping requires the packages doctrine/annotations and doctrine/cache to be installed.');
}
$annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache());