Skip to content

[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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ CHANGELOG
* allow to define a reusable set of constraints by extending the `Compound` constraint
* added `Sequentially` constraint, to sequentially validate a set of constraints (any violation raised will prevent further validation of the nested constraints)
* added the `divisibleBy` option to the `Count` constraint
* added `Isin` constraint
* added the `ExpressionLanguageSyntax` constraint

5.0.0
Expand Down
38 changes: 38 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Isin.php
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 src/Symfony/Component/Validator/Constraints/IsinValidator.php
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@
<source>Each element of this collection should satisfy its own set of constraints.</source>
<target>Each element of this collection should satisfy its own set of constraints.</target>
</trans-unit>
<trans-unit id="99">
<source>This value is not a valid International Securities Identification Number (ISIN).</source>
<target>This value is not a valid International Securities Identification Number (ISIN).</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@
<source>Each element of this collection should satisfy its own set of constraints.</source>
<target>Chaque élément de cette collection doit satisfaire à son propre jeu de contraintes.</target>
</trans-unit>
<trans-unit id="99">
<source>This value is not a valid International Securities Identification Number (ISIN).</source>
<target>Ce n'est pas un code international de sécurité valide (ISIN).</target>
</trans-unit>
</body>
</file>
</xliff>
Loading