-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Feature #36362 add Isin validator constraint #36368
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
Closed
lmasforne
wants to merge
5
commits into
symfony:master
from
lmasforne:ticket_36362_isin_constraint_validator
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2aec593
Feature #36362 add Isin validator constraint
lmasforne 53583e0
ticket 36362 fix PR feedbacks
lmasforne 02e86f6
ticket 36362 fix PR feedbacks and revert changelog
lmasforne 5ec9f42
Update src/Symfony/Component/Validator/CHANGELOG.md
lmasforne 4611692
Merge branch 'master' into ticket_36362_isin_constraint_validator
lmasforne 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
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
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 <[email protected]> | ||
* | ||
* 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é <[email protected]> | ||
*/ | ||
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).'; | ||
} |
129 changes: 129 additions & 0 deletions
129
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,129 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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; | ||
|
||
/** | ||
* @author Laurent Masforné <[email protected]> | ||
* | ||
* @see https://en.wikipedia.org/wiki/International_Securities_Identification_Number | ||
*/ | ||
class IsinValidator extends ConstraintValidator | ||
{ | ||
/** | ||
* {@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(); | ||
|
||
return; | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
private function isCorrectChecksum($input) | ||
{ | ||
$characters = str_split($input); | ||
foreach ($characters as $i => $char) { | ||
$characters[$i] = \intval($char, 36); | ||
} | ||
$checkDigit = array_pop($characters); | ||
$number = implode('', $characters); | ||
$expectedCheckDigit = $this->getCheckDigit($number); | ||
|
||
return $checkDigit === $expectedCheckDigit; | ||
} | ||
|
||
/** | ||
* This method performs the luhn algorithm | ||
* to obtain a check digit. | ||
*/ | ||
private function getCheckDigit($input) | ||
{ | ||
// first split up the string | ||
$numbers = str_split($input); | ||
|
||
// calculate the positional value. | ||
// when there is an even number of digits the second group will be multiplied, so p starts on 0 | ||
// when there is an odd number of digits the first group will be multiplied, so p starts on 1 | ||
$p = \count($numbers) % 2; | ||
// run through each number | ||
foreach ($numbers as $i => $num) { | ||
$num = (int) $num; | ||
// every positional number needs to be multiplied by 2 | ||
if ($p % 2) { | ||
$num = $num * 2; | ||
// if the result was more than 9 | ||
// add the individual digits | ||
$num = array_sum(str_split($num)); | ||
} | ||
$numbers[$i] = $num; | ||
++$p; | ||
} | ||
|
||
// get the total value of all the digits | ||
$sum = array_sum($numbers); | ||
|
||
// get the remainder when dividing by 10 | ||
$mod = $sum % 10; | ||
|
||
// subtract from 10 | ||
$rem = 10 - $mod; | ||
|
||
// mod from 10 to catch if the result was 0 | ||
$digit = $rem % 10; | ||
|
||
return $digit; | ||
} | ||
} |
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
Oops, something went wrong.
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.