From b6f29f47217dfe7f924b2805b0fc16463bd0a79b Mon Sep 17 00:00:00 2001 From: Rudy Onfroy Date: Wed, 19 Sep 2018 15:51:57 +0200 Subject: [PATCH] [Validator] add exception when intl component not found --- .../Validator/Constraints/BicValidator.php | 6 ++++-- .../Validator/Constraints/CountryValidator.php | 5 +++++ .../Validator/Constraints/CurrencyValidator.php | 5 +++++ .../Validator/Constraints/LanguageValidator.php | 5 +++++ .../Validator/Exception/LogicException.php | 16 ++++++++++++++++ .../Mapping/Factory/BlackHoleMetadataFactory.php | 4 +++- .../Factory/BlackHoleMetadataFactoryTest.php | 2 +- .../Component/Validator/ValidatorBuilder.php | 5 +++-- 8 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 src/Symfony/Component/Validator/Exception/LogicException.php diff --git a/src/Symfony/Component/Validator/Constraints/BicValidator.php b/src/Symfony/Component/Validator/Constraints/BicValidator.php index da2ac5fbc3826..640f72da064e7 100644 --- a/src/Symfony/Component/Validator/Constraints/BicValidator.php +++ b/src/Symfony/Component/Validator/Constraints/BicValidator.php @@ -14,6 +14,7 @@ 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 @@ public function validate($value, Constraint $constraint) 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) diff --git a/src/Symfony/Component/Validator/Constraints/CountryValidator.php b/src/Symfony/Component/Validator/Constraints/CountryValidator.php index 46585da8b9697..2cc057d1c266b 100644 --- a/src/Symfony/Component/Validator/Constraints/CountryValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CountryValidator.php @@ -14,6 +14,7 @@ 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 @@ public function validate($value, Constraint $constraint) 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(); diff --git a/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php b/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php index 42fa8cfa45682..bcd9e8f007959 100644 --- a/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php @@ -14,6 +14,7 @@ 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 @@ public function validate($value, Constraint $constraint) 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(); diff --git a/src/Symfony/Component/Validator/Constraints/LanguageValidator.php b/src/Symfony/Component/Validator/Constraints/LanguageValidator.php index 1722034720c60..575e6c14ceda3 100644 --- a/src/Symfony/Component/Validator/Constraints/LanguageValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LanguageValidator.php @@ -14,6 +14,7 @@ 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 @@ public function validate($value, Constraint $constraint) 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(); diff --git a/src/Symfony/Component/Validator/Exception/LogicException.php b/src/Symfony/Component/Validator/Exception/LogicException.php new file mode 100644 index 0000000000000..41d0975c38893 --- /dev/null +++ b/src/Symfony/Component/Validator/Exception/LogicException.php @@ -0,0 +1,16 @@ + + * + * 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 +{ +} diff --git a/src/Symfony/Component/Validator/Mapping/Factory/BlackHoleMetadataFactory.php b/src/Symfony/Component/Validator/Mapping/Factory/BlackHoleMetadataFactory.php index 5b38d0c98a775..ec21d54a6efbf 100644 --- a/src/Symfony/Component/Validator/Mapping/Factory/BlackHoleMetadataFactory.php +++ b/src/Symfony/Component/Validator/Mapping/Factory/BlackHoleMetadataFactory.php @@ -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.'); } /** diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Factory/BlackHoleMetadataFactoryTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Factory/BlackHoleMetadataFactoryTest.php index a323567d2316b..880f6b70d1ab7 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Factory/BlackHoleMetadataFactoryTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Factory/BlackHoleMetadataFactoryTest.php @@ -17,7 +17,7 @@ class BlackHoleMetadataFactoryTest extends TestCase { /** - * @expectedException \LogicException + * @expectedException \Symfony\Component\Validator\Exception\LogicException */ public function testGetMetadataForThrowsALogicException() { diff --git a/src/Symfony/Component/Validator/ValidatorBuilder.php b/src/Symfony/Component/Validator/ValidatorBuilder.php index 047f1827bab0b..c87f018335b24 100644 --- a/src/Symfony/Component/Validator/ValidatorBuilder.php +++ b/src/Symfony/Component/Validator/ValidatorBuilder.php @@ -17,6 +17,7 @@ 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 @@ public function enableAnnotationMapping(Reader $annotationReader = null) } 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());