8000 minor #28606 [Validator] Add BC layer covering BicValidator without I… · symfony/validator@f1fdab1 · GitHub
[go: up one dir, main page]

Skip to content

Commit f1fdab1

Browse files
committed
minor #28606 [Validator] Add BC layer covering BicValidator without Intl (chalasr)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Validator] Add BC layer covering BicValidator without Intl | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | symfony/symfony#28473 (review) | License | MIT | Doc PR | n/a Commits ------- 10b8a5f041 [Validator] Add BC layer covering BicValidator without Intl
2 parents ac8581f + e93bd08 commit f1fdab1

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

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
-----

Constraints/Bic.php

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

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Symfony\Component\Intl\Intl;
1415
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\Exception\LogicException;
1517

1618
/**
1719
* @Annotation
@@ -36,4 +38,14 @@ class Bic extends Constraint
3638
);
3739

3840
public $message = 'This is not a valid Business Identifier Code (BIC).';
41+
42+
public function __construct($options = null)
43+
{
44+
if (!class_exists(Intl::class)) {
45+
// throw new LogicException(sprintf('The "symfony/intl" component is required to use the "%s" constraint.', __CLASS__));
46+
@trigger_error(sprintf('Using the "%s" constraint without the "symfony/intl" component installed is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
47+
}
48+
49+
parent::__construct($options);
50+
}
3951
}

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)
0