[Validator] Checking a BIC along with an IBAN#28479
[Validator] Checking a BIC along with an IBAN#28479nicolas-grekas merged 1 commit intosymfony:masterfrom
Conversation
7dc5554 to
67a7ea3
Compare
nicolas-grekas
left a comment
There was a problem hiding this comment.
Thanks for working on this! Here are some random comments :)
| public $ibanMessage = 'This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}.'; | ||
|
|
||
| public $iban; | ||
|
|
There was a problem hiding this comment.
I'd suggest to remove the blank lines between the added properties
|
|
||
| public $ibanPropertyPath; | ||
|
|
||
| public function __construct($options = null) |
There was a problem hiding this comment.
missing "array" type hint?
| } else { | ||
| $iban = $constraint->iban; | ||
| } | ||
| if ($iban) { |
There was a problem hiding this comment.
if (!$iban) => return early?
There was a problem hiding this comment.
@nicolas-grekas no because the logic of validators is to return null on the first violation.
If tomorrow we add some more checks after this new code then we don't want to return early in this check.
There was a problem hiding this comment.
We shouldn't base current code on hypothetical future-based arguments. Here it would make the code clearer now.
| if ($iban) { | ||
| $ibanCountryCode = substr($iban, 0, 2); | ||
| if (ctype_alpha($ibanCountryCode)) { | ||
| if (substr($canonicalize, 4, 2) !== $ibanCountryCode) { |
There was a problem hiding this comment.
the two "if"s should be combined in one using &&
| ->setCode(Bic::INVALID_IBAN_COUNTRY_CODE_ERROR) | ||
| ->addViolation(); | ||
|
|
||
| return; |
There was a problem hiding this comment.
@nicolas-grekas I can remove it but I've done like the other violations raised above
There was a problem hiding this comment.
Please remove it yes, the code elsewhere is unrelated.
|
Could you improve the PR and commit title please also? (I'd suggest to squash commits while doing so) |
2d5d72d to
9fb42ba
Compare
|
Thanks, LGTM, except the test failure of course. |
9fb42ba to
41b422b
Compare
| } else { | ||
| $iban = $constraint->iban; | ||
| } | ||
| if (!isset($iban) || !$iban) { |
There was a problem hiding this comment.
I'd suggest to rewrite the previous this way
$iban = $constraint->iban;
$path = $constraint->ibanPropertyPath;
if ($path && null !== $object = $this->context->getObject()) {
try {
$iban = $this->getPropertyAccessor()->getValue($object, $path);
} catch (NoSuchPropertyException $e) {
throw new ConstraintDefinitionException(sprintf('Invalid property path "%s" provided to "%s" constraint: %s', $path, \get_class($constraint), $e->getMessage()), 0, $e);
}
}
if (!$iban) { 98bb643 to
645b016
Compare
| } | ||
| } | ||
|
|
||
| private function getPropertyAccessor() |
There was a problem hiding this comment.
getPropertyAccessor(): PropertyAccessor
| use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | ||
| use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; | ||
|
|
||
| class BicComparisonTest_Class |
There was a problem hiding this comment.
i believe for consistency we should put this at the bottom of the file. Not sure about using _ in the class name, camelcase is preferred.
| * deprecated using the `Bic`, `Country`, `Currency`, `Language` and `Locale` constraints without `symfony/intl` | ||
| * deprecated using the `Email` constraint without `egulias/email-validator` | ||
| * deprecated using the `Expression` constraint without `symfony/expression-language` | ||
| * added options iban and propertyPath to Bic constraint |
There was a problem hiding this comment.
ibanPropertyPath
also any symbol is put between backticks (``), here the symbols are: "iban", "ibanPropertyPath" and "Bic"
| throw new ConstraintDefinitionException(sprintf('The "iban" and "ibanPropertyPath" of the Iban constraint cannot be used at the same time.', \get_class($this))); | ||
| } | ||
|
|
||
| if (isset($options['propertyPath']) && !class_exists(PropertyAccess::class)) { |
| } | ||
|
|
||
| if (isset($options['iban']) && isset($options['ibanPropertyPath'])) { | ||
| throw new ConstraintDefinitionException(sprintf('The "iban" and "ibanPropertyPath" of the Iban constraint cannot be used at the same time.', \get_class($this))); |
There was a problem hiding this comment.
... options of the Bic constraint ...
| public $ibanPropertyPath; | ||
|
|
||
| public function __construct($options = null) | ||
| public function __construct(array $options = null) |
There was a problem hiding this comment.
looks good for me as we expect an array.
Why aren't you sure about this?
There was a problem hiding this comment.
In this case, the change looks valid to me. Generally you can define a default option by overriding the getDefaultOption() method. But since that is not the case for the Bic constraint, it will already throw an exception when no array is passed.
There was a problem hiding this comment.
exactly, but now we'll never reach the exception at
so basically all constraints will throw this exception, except here it will cause a type error: https://3v4l.org/WjMuY
|
@ro0NL tests are now green :) |
| * deprecated using the `Bic`, `Country`, `Currency`, `Language` and `Locale` constraints without `symfony/intl` | ||
| * deprecated using the `Email` constraint without `egulias/email-validator` | ||
| * deprecated using the `Expression` constraint without `symfony/expression-language` | ||
| * added options `iban` and `ibanPropertyPath` to Bic constraint |
There was a problem hiding this comment.
needs to be added to a new 4.3.0 section now as 4.2 is in feature freeze
| } | ||
|
|
||
| if (isset($options['iban']) && isset($options['ibanPropertyPath'])) { | ||
| throw new ConstraintDefinitionException(sprintf('The "iban" and "ibanPropertyPath" options of the Iban constraint cannot be used at the same time.', \get_class($this))); |
There was a problem hiding this comment.
self::class instead of get_class($this)(same below)?
| private function getPropertyAccessor(): PropertyAccessor | ||
| { | ||
| if (null === $this->propertyAccessor) { | ||
| if (!class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) { |
There was a problem hiding this comment.
we should use the class constant here
There was a problem hiding this comment.
@xabbuh like PropertyAccess::class ?
I haven't tried but I think it will throw an exception if the class does not exist, right?
There was a problem hiding this comment.
yes like that, and using the class constant works even when the class is not present: https://3v4l.org/knSo3
|
Thank you @sylfabre. |
This PR was merged into the 4.3-dev branch. Discussion ---------- [Validator] Checking a BIC along with an IBAN | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #28166 | License | MIT | Doc PR | symfony/symfony-docs#10349 A BIC comes usually with an IBAN so it's better to check that they are associated. This PR provides an `iban` option to `Symfony\Component\Validator\Constraints\Bic` to check the BIC against an IBAN. It also provides an `ibanPropertyPath` to retrieves the IBAN using the property accessor like with comparison constraints. Commits ------- bb6be15 [Validator] Checking a BIC along with an IBAN Fix #28166
This PR was merged into the master branch. Discussion ---------- Validate a BIC along with an IBAN Doc for this PR: symfony/symfony#28479 Commits ------- 51f833b Validate a BIC along with an IBAN
This PR was submitted for the master branch but it was merged into the 3.4 branch instead (closes #29799). Discussion ---------- [Validator] Add Japanese translation for #28479 | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | n/a | License | MIT | Doc PR | n/a Commits ------- 7a0bdde Add Japanese translation for #28479
* 3.4: Add Japanese translation for #28479
* 4.1: Add Japanese translation for #28479
* 4.2: Add Japanese translation for #28479
A BIC comes usually with an IBAN so it's better to check that they are associated. This PR provides an
ibanoption toSymfony\Component\Validator\Constraints\Bicto check the BIC against an IBAN.It also provides an
ibanPropertyPathto retrieves the IBAN using the property accessor like with comparison constraints.