-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Validator] Add Isin validator constraint #37565
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ CHANGELOG | |
* }) | ||
*/ | ||
``` | ||
* added the `Isin` constraint and validator | ||
|
||
5.1.0 | ||
----- | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?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\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"PROPERTY", "METHOD", "ANNOTATION"}) | ||
* | ||
* @author Laurent Masforné <l.masforne@gmail.com> | ||
*/ | ||
class Isin extends Constraint | ||
{ | ||
const VALIDATION_LENGTH = 12; | ||
const VALIDATION_PATTERN = '/[A-Z]{2}[A-Z0-9]{9}[0-9]{1}/'; | ||
|
||
const INVALID_LENGTH_ERROR = '88738dfc-9ed5-ba1e-aebe-402a2a9bf58e'; | ||
const INVALID_PATTERN_ERROR = '3d08ce0-ded9-a93d-9216-17ac21265b65e'; | ||
const INVALID_CHECKSUM_ERROR = '32089b-0ee1-93ba-399e-aa232e62f2d29d'; | ||
|
||
protected static $errorNames = [ | ||
self::INVALID_LENGTH_ERROR => 'INVALID_LENGTH_ERROR', | ||
self::INVALID_PATTERN_ERROR => 'INVALID_PATTERN_ERROR', | ||
self::INVALID_CHECKSUM_ERROR => 'INVALID_CHECKSUM_ERROR', | ||
]; | ||
|
||
public $message = 'This is not a valid International Securities Identification Number (ISIN).'; | ||
} |
92 changes: 92 additions & 0 deletions
92
src/Symfony/Component/Validator/Constraints/IsinValidator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?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\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
use Symfony\Component\Validator\Exception\UnexpectedValueException; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
/** | ||
* @author Laurent Masforné <l.masforne@gmail.com> | ||
* | ||
* @see https://en.wikipedia.org/wiki/International_Securities_Identification_Number | ||
*/ | ||
class IsinValidator extends ConstraintValidator | ||
{ | ||
/** | ||
* @var ValidatorInterface | ||
*/ | ||
private $validator; | ||
|
||
public function __construct(ValidatorInterface $validator) | ||
{ | ||
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validate($value, Constraint $constraint) | ||
{ | ||
if (!$constraint instanceof Isin) { | ||
throw new UnexpectedTypeException($constraint, Isin::class); | ||
} | ||
|
||
if (null === $value || '' === $value) { | ||
return; | ||
} | ||
|
||
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { | ||
throw new UnexpectedValueException($value, 'string'); | ||
} | ||
|
||
$value = strtoupper($value); | ||
|
||
if (Isin::VALIDATION_LENGTH !== \strlen($value)) { | ||
$this->context->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode(Isin::INVALID_LENGTH_ERROR) | ||
->addViolation(); | ||
|
||
return; | ||
} | ||
|
||
if (!preg_match(Isin::VALIDATION_PATTERN, $value)) { | ||
$this->context->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode(Isin::INVALID_PATTERN_ERROR) | ||
->addViolation(); | ||
|
||
return; | ||
} | ||
|
||
if (!$this->isCorrectChecksum($value)) { | ||
$this->context->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode(Isin::INVALID_CHECKSUM_ERROR) | ||
->addViolation(); | ||
} | ||
} | ||
|
||
private function isCorrectChecksum(string $input): bool | ||
{ | ||
$characters = str_split($input); | ||
foreach ($characters as $i => $char) { | ||
$characters[$i] = \intval($char, 36); | ||
} | ||
$number = implode('', $characters); | ||
|
||
return 0 === $this->validator->validate($number, new Luhn())->count(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/Symfony/Component/Validator/Tests/Constraints/IsinValidatorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Validator\Tests\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraints\Isin; | ||
use Symfony\Component\Validator\Constraints\IsinValidator; | ||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; | ||
use Symfony\Component\Validator\ValidatorBuilder; | ||
|
||
class IsinValidatorTest extends ConstraintValidatorTestCase | ||
{ | ||
protected function createValidator() | ||
{ | ||
$validatorBuilder = new ValidatorBuilder(); | ||
|
||
return new IsinValidator($validatorBuilder->getValidator()); | ||
} | ||
|
||
public function testNullIsValid() | ||
{ | ||
$this->validator->validate(null, new Isin()); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
public function testEmptyStringIsValid() | ||
{ | ||
$this->validator->validate('', new Isin()); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
/** | ||
* @dataProvider getValidIsin | ||
*/ | ||
public function testValidIsin($isin) | ||
{ | ||
$this->validator->validate($isin, new Isin()); | ||
$this->assertNoViolation(); | ||
} | ||
|
||
public function getValidIsin() | ||
{ | ||
return [ | ||
['XS2125535901'], // Goldman Sachs International | ||
['DE000HZ8VA77'], // UniCredit Bank AG | ||
['CH0528261156'], // Leonteq Securities AG [Guernsey] | ||
['US0378331005'], // Apple, Inc. | ||
['AU0000XVGZA3'], // TREASURY CORP VICTORIA 5 3/4% 2005-2016 | ||
['GB0002634946'], // BAE Systems | ||
['CH0528261099'], // Leonteq Securities AG [Guernsey] | ||
['XS2155672814'], // OP Corporate Bank plc | ||
['XS2155687259'], // Orbian Financial Services III, LLC | ||
['XS2155696672'], // Sheffield Receivables Company LLC | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider getIsinWithInvalidLenghFormat | ||
*/ | ||
public function testIsinWithInvalidFormat($isin) | ||
{ | ||
$this->assertViolationRaised($isin, Isin::INVALID_LENGTH_ERROR); | ||
} | ||
|
||
public function getIsinWithInvalidLenghFormat() | ||
{ | ||
return [ | ||
['X'], | ||
['XS'], | ||
['XS2'], | ||
['XS21'], | ||
['XS215'], | ||
['XS2155'], | ||
['XS21556'], | ||
['XS215569'], | ||
['XS2155696'], | ||
['XS21556966'], | ||
['XS215569667'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider getIsinWithInvalidPattern | ||
*/ | ||
public function testIsinWithInvalidPattern($isin) | ||
{ | ||
$this->assertViolationRaised($isin, Isin::INVALID_PATTERN_ERROR); | ||
} | ||
|
||
public function getIsinWithInvalidPattern() | ||
{ | ||
return [ | ||
['X12155696679'], | ||
['123456789101'], | ||
['XS215569667E'], | ||
['XS215E69667A'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider getIsinWithValidFormatButIncorrectChecksum | ||
*/ | ||
public function testIsinWithValidFormatButIncorrectChecksum($isin) | ||
{ | ||
$this->assertViolationRaised($isin, Isin::INVALID_CHECKSUM_ERROR); | ||
} | ||
|
||
public function getIsinWithValidFormatButIncorrectChecksum() | ||
{ | ||
return [ | ||
['XS2112212144'], | ||
['DE013228VA77'], | ||
['CH0512361156'], | ||
['XS2125660123'], | ||
['XS2012587408'], | ||
['XS2012380102'], | ||
['XS2012239364'], | ||
]; | ||
} | ||
|
||
private function assertViolationRaised($isin, $code) | ||
{ | ||
$constraint = new Isin([ | ||
'message' => 'myMessage', | ||
]); | ||
|
||
$this->validator->validate($isin, $constraint); | ||
|
||
$this->buildViolation('myMessage') | ||
->setParameter('{{ value }}', '"'.$isin.'"') | ||
->setCode($code) | ||
->assertRaised(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.