8000 [Validator] add exception when intl component not found by ronfroy · Pull Request #28513 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] add exception when intl component not found #28513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Symfony/Component/Validator/Constraints/BicValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();

Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Validator/Exception/LogicException.php
Original file line number Diff line number Diff line change
@@ -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
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Validator\Mapping\Factory;

use Symfony\Component\Validator\Exception\LogicException;

/**
* Metadata factory that does not store metadata.
*
Expand All @@ -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.');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class BlackHoleMetadataFactoryTest extends TestCase
{
/**
* @expectedException \LogicException
* @expectedException \Symfony\Component\Validator\Exception\LogicException
*/
public function testGetMetadataForThrowsALogicException()
{
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/Validator/ValidatorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
0