8000 [Validator] Resolve IsinValidator's dependency on the validator. · symfony/symfony@86cef48 · GitHub
[go: up one dir, main page]

Skip to content

Commit 86cef48

Browse files
committed
[Validator] Resolve IsinValidator's dependency on the validator.
1 parent fcc832b commit 86cef48

File tree

4 files changed

+53
-41
lines changed

4 files changed

+53
-41
lines changed

src/Symfony/Component/Validator/Constraints/IsinValidator.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Component\Validator\ConstraintValidator;
1616
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1717
use Symfony\Component\Validator\Exception\UnexpectedValueException;
18-
use Symfony\Component\Validator\Validator\ValidatorInterface;
1918

2019
/**
2120
* @author Laurent Masforné <l.masforne@gmail.com>
@@ -24,15 +23,7 @@
2423
*/
2524
class IsinValidator extends ConstraintValidator
2625
{
27-
/**
28-
* @var ValidatorInterface
29-
*/
30-
private $validator;
31-
32-
public function __construct(ValidatorInterface $validator)
33-
{
34-
$this->validator = $validator;
35-
}
26+
use LuhnChecksumTrait;
3627

3728
/**
3829
* {@inheritdoc}
@@ -71,22 +62,21 @@ public function validate($value, Constraint $constraint)
7162
return;
7263
}
7364

74-
if (!$this->isCorrectChecksum($value)) {
65+
if (!$this->isChecksumCorrect($this->createLuhnInput($value))) {
7566
$this->context->buildViolation($constraint->message)
7667
->setParameter('{{ value }}', $this->formatValue($value))
7768
->setCode(Isin::INVALID_CHECKSUM_ERROR)
7869
->addViolation();
7970
}
8071
}
8172

82-
private function isCorrectChecksum(string $input): bool
73+
private function createLuhnInput(string $input): string
8374
{
8475
$characters = str_split($input);
8576
foreach ($characters as $i => $char) {
8677
$characters[$i] = \intval($char, 36);
8778
}
88-
$number = implode('', $characters);
8979

90-
return 0 === $this->validator->validate($number, new Luhn())->count();
80+
return implode('', $characters);
9181
}
9282
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
/**
15+
* @internal
16+
*/
17+
trait LuhnChecksumTrait
18+
{
19+
private function isChecksumCorrect(string $value): bool
20+
{
21+
$checkSum = 0;
22+
$length = \strlen($value);
23+
24+
// Starting with the last digit and walking left, add every second
25+
// digit to the check sum
26+
// e.g. 7 9 9 2 7 3 9 8 7 1 3
27+
// ^ ^ ^ ^ ^ ^
28+
// = 7 + 9 + 7 + 9 + 7 + 3
29+
for ($i = $length - 1; $i >= 0; $i -= 2) {
30+
$checkSum += (int) $value[$i];
31+
}
32+
33+
// Starting with the second last digit and walking left, double every
34+
// second digit and add it to the check sum
35+
// For doubles greater than 9, sum the individual digits
36+
// e.g. 7 9 9 2 7 3 9 8 7 1 3
37+
// ^ ^ ^ ^ ^
38+
// = 1+8 + 4 + 6 + 1+6 + 2
39+
for ($i = $length - 2; $i >= 0; $i -= 2) {
40+
$checkSum += array_sum(str_split((int) $value[$i] * 2));
41+
}
42+
43+
return 0 !== $checkSum && 0 === $checkSum % 10;
44+
}
45+
}

src/Symfony/Component/Validator/Constraints/LuhnValidator.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
*/
3131
class LuhnValidator extends ConstraintValidator
3232
{
33+
use LuhnChecksumTrait;
34+
3335
/**
3436
* Validates a credit card number with the Luhn algorithm.
3537
*
@@ -64,29 +66,7 @@ public function validate($value, Constraint $constraint)
6466
return;
6567
}
6668

67-
$checkSum = 0;
68-
$length = \strlen($value);
69-
70-
// Starting with the last digit and walking left, add every second
71-
// digit to the check sum
72-
// e.g. 7 9 9 2 7 3 9 8 7 1 3
73-
// ^ ^ ^ ^ ^ ^
74-
// = 7 + 9 + 7 + 9 + 7 + 3
75-
for ($i = $length - 1; $i >= 0; $i -= 2) {
76-
$checkSum += $value[$i];
77-
}
78-
79-
// Starting with the second last digit and walking left, double every
80-
// second digit and add it to the check sum
81-
// For doubles greater than 9, sum the individual digits
82-
// e.g. 7 9 9 2 7 3 9 8 7 1 3
83-
// ^ ^ ^ ^ ^
84-
// = 1+8 + 4 + 6 + 1+6 + 2
85-
for ($i = $length - 2; $i >= 0; $i -= 2) {
86-
$checkSum += array_sum(str_split((int) $value[$i] * 2));
87-
}
88-
89-
if (0 === $checkSum || 0 !== $checkSum % 10) {
69+
if (!$this->isChecksumCorrect($value)) {
9070
$this->context->buildViolation($constraint->message)
9171
->setParameter('{{ value }}', $this->formatValue($value))
9272
->setCode(Luhn::CHECKSUM_FAILED_ERROR)

src/Symfony/Component/Validator/Tests/Constraints/IsinValidatorTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
use Symfony\Component\Validator\Constraints\Isin;
66
use Symfony\Component\Validator\Constraints\IsinValidator;
77
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
8-
use Symfony\Component\Validator\ValidatorBuilder;
98

109
class IsinValidatorTest extends ConstraintValidatorTestCase
1110
{
1211
protected function createValidator()
1312
{
14-
$validatorBuilder = new ValidatorBuilder();
15-
16-
return new IsinValidator($validatorBuilder->getValidator());
13+
return new IsinValidator();
1714
}
1815

1916
public function testNullIsValid()

0 commit comments

Comments
 (0)
0