From 10b8a5f0415c116bc292255f05b2b13738304fba Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Wed, 26 Sep 2018 14:52:52 +0200 Subject: [PATCH] =?UTF-8?q?[Validator]=C2=A0Add=20BC=20layer=20covering=20?= =?UTF-8?q?BicValidator=20without=20Intl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UPGRADE-4.2.md | 1 + UPGRADE-5.0.md | 1 + src/Symfony/Component/Validator/CHANGELOG.md | 1 + src/Symfony/Component/Validator/Constraints/Bic.php | 12 ++++++++++++ .../Component/Validator/Constraints/BicValidator.php | 12 ++++++------ 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/UPGRADE-4.2.md b/UPGRADE-4.2.md index e7382e4b3d..1ef2cc33ff 100644 --- a/UPGRADE-4.2.md +++ b/UPGRADE-4.2.md @@ -206,3 +206,4 @@ Validator * The component is now decoupled from `symfony/translation` and uses `Symfony\Contracts\Translation\TranslatorInterface` instead * The `ValidatorBuilderInterface` has been deprecated and `ValidatorBuilder` made final * Deprecated validating instances of `\DateTimeInterface` in `DateTimeValidator`, `DateValidator` and `TimeValidator`. Use `Type` instead or remove the constraint if the underlying model is type hinted to `\DateTimeInterface` already. + * Using the `Bic` constraint without `symfony/intl` is deprecated diff --git a/UPGRADE-5.0.md b/UPGRADE-5.0.md index 49ec7e39a8..90b4d520db 100644 --- a/UPGRADE-5.0.md +++ b/UPGRADE-5.0.md @@ -191,6 +191,7 @@ Validator * The component is now decoupled from `symfony/translation` and uses `Symfony\Contracts\Translation\TranslatorInterface` instead * The `ValidatorBuilderInterface` has been removed and `ValidatorBuilder` is now final * Removed support for validating instances of `\DateTimeInterface` in `DateTimeValidator`, `DateValidator` and `TimeValidator`. Use `Type` instead or remove the constraint if the underlying model is type hinted to `\DateTimeInterface` already. + * The `symfony/intl` component is now required for using the `Bic` constraint Workflow -------- diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index a1f068f314..ac98f581b3 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -10,6 +10,7 @@ CHANGELOG * made `ValidatorBuilder` final * marked `format` the default option in `DateTime` constraint * deprecated validating instances of `\DateTimeInterface` in `DateTimeValidator`, `DateValidator` and `TimeValidator`. + * deprecated using the `Bic` constraint without `symfony/intl` 4.1.0 ----- diff --git a/src/Symfony/Component/Validator/Constraints/Bic.php b/src/Symfony/Component/Validator/Constraints/Bic.php index dee5d52693..9af23c8ddb 100644 --- a/src/Symfony/Component/Validator/Constraints/Bic.php +++ b/src/Symfony/Component/Validator/Constraints/Bic.php @@ -11,7 +11,9 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Intl\Intl; use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Exception\LogicException; /** * @Annotation @@ -36,4 +38,14 @@ class Bic extends Constraint ); public $message = 'This is not a valid Business Identifier Code (BIC).'; + + public function __construct($options = null) + { + if (!class_exists(Intl::class)) { + // throw new LogicException(sprintf('The "symfony/intl" component is required to use the "%s" constraint.', __CLASS__)); + @trigger_error(sprintf('Using the "%s" constraint without the "symfony/intl" component installed is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED); + } + + parent::__construct($options); + } } diff --git a/src/Symfony/Component/Validator/Constraints/BicValidator.php b/src/Symfony/Component/Validator/Constraints/BicValidator.php index 640f72da06..2d0a450e86 100644 --- a/src/Symfony/Component/Validator/Constraints/BicValidator.php +++ b/src/Symfony/Component/Validator/Constraints/BicValidator.php @@ -14,7 +14,6 @@ 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; /** @@ -69,13 +68,14 @@ class BicValidator extends ConstraintValidator return; } - if (!class_exists(Intl::class)) { - throw new LogicException('The "symfony/intl" component is required to use the Bic constraint.'); + // @deprecated since Symfony 4.2 + if (class_exists(Intl::class)) { + $validCountryCode = isset(Intl::getRegionBundle()->getCountryNames()[substr($canonicalize, 4, 2)]); + } else { + $validCountryCode = ctype_alpha(substr($canonicalize, 4, 2)); } - // next 2 letters must be alphabetic (country code) - $countries = Intl::getRegionBundle()->getCountryNames(); - if (!isset($countries[substr($canonicalize, 4, 2)])) { + if (!$validCountryCode) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) ->setCode(Bic::INVALID_COUNTRY_CODE_ERROR)