Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 270a5d3

Browse files
author
Robin Chalas
committed
[Validator] Add BC layer covering BicValidator without Intl
1 parent e95ea81 commit 270a5d3

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

UPGRADE-4.2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,4 @@ Validator
206206
* The component is now decoupled from `symfony/translation` and uses `Symfony\Contracts\Translation\TranslatorInterface` instead
207207
* The `ValidatorBuilderInterface` has been deprecated and `ValidatorBuilder` made final
208208
* 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.
209+
* Using the `Bic` constraint without `symfony/intl` is deprecated

UPGRADE-5.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ Validator
191191
* The component is now decoupled from `symfony/translation` and uses `Symfony\Contracts\Translation\TranslatorInterface` instead
192192
* The `ValidatorBuilderInterface` has been removed and `ValidatorBuilder` is now final
193193
* 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.
194+
* The `symfony/intl` component is now required for using the `Bic` constraint
194195

195196
Workflow
196197
--------

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* made `ValidatorBuilder` final
1111
* marked `format` the default option in `DateTime` constraint
1212
* deprecated validating instances of `\DateTimeInterface` in `DateTimeValidator`, `DateValidator` and `TimeValidator`.
13+
* deprecated using the `Bic` constraint without `symfony/intl`
1314

1415
4.1.0
1516
-----

src/Symfony/Component/Validator/Constraints/Bic.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Symfony\Component\Intl\Intl;
1415
use Symfony\Component\Validator\Constraint;
1516

1617
/**
@@ -36,4 +37,14 @@ class Bic extends Constraint
3637
);
3738

3839
public $message = 'This is not a valid Business Identifier Code (BIC).';
40+
41+
public function __construct($options = null)
42+
{
43+
// @deprecated since Symfony 4.2, throw a LogicException in 5.0
44+
if (!class_exists(Intl::class)) {
45+
@trigger_error('Using the Bic constraint without the "symfony/intl" component installed is deprecated since Symfony 4.2.', E_USER_DEPRECATED);
46+
}
47+
48+
parent::__construct($options);
49+
}
3950
}

src/Symfony/Component/Validator/Constraints/BicValidator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Component\Intl\Intl;
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\ConstraintValidator;
17-
use Symfony\Component\Validator\Exception\LogicException;
1817
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1918

2019
/**
@@ -69,13 +68,14 @@ public function validate($value, Constraint $constraint)
6968
return;
7069
}
7170

72-
if (!class_exists(Intl::class)) {
73-
throw new LogicException('The "symfony/intl" component is required to use the Bic constraint.');
71+
// @deprecated since Symfony 4.2,
72+
if (class_exists(Intl::class)) {
73+
$validCountryCode = isset(Intl::getRegionBundle()->getCountryNames()[substr($canonicalize, 4, 2)]);
74+
} else {
75+
$validCountryCode = ctype_alpha(substr($canonicalize, 4, 2));
7476
}
7577

76-
// next 2 letters must be alphabetic (country code)
77-
$countries = Intl::getRegionBundle()->getCountryNames();
78-
if (!isset($countries[substr($canonicalize, 4, 2)])) {
78+
if (!$validCountryCode) {
7979
$this->context->buildViolation($constraint->message)
8080
->setParameter('{{ value }}', $this->formatValue($value))
8181
->setCode(Bic::INVALID_COUNTRY_CODE_ERROR)

0 commit comments

Comments
 (0)