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

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 94e379e

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

File tree

4 files changed

+58
-42
lines changed

4 files changed

+58
-42
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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
* Shared implementation of the Luhn algorithm.
16+
*
17+
* @author Tim Nagel <t.nagel@infinite.net.au>
18+
* @author Greg Knapp http://gregk.me/2011/php-implementation-of-bank-card-luhn-algorithm/
19+
*
20+
* @internal
21+
*/
22+
trait LuhnChecksumTrait
23+
{
24+
private function isChecksumCorrect(string $value): bool
25+
{
26+
$checkSum = 0;
27+
$length = \strlen($value);
28+
29+
// Starting with the last digit and walking left, add every second
30+
// digit to the check sum
31+
// e.g. 7 9 9 2 7 3 9 8 7 1 3
32+
// ^ ^ ^ ^ ^ ^
33+
// = 7 + 9 + 7 + 9 + 7 + 3
34+
for ($i = $length - 1; $i >= 0; $i -= 2) {
35+
$checkSum += (int) $value[$i];
36+
}
37+
38+
// Starting with the second last digit and walking left, double every
39+
// second digit and add it to the check sum
40+
// For doubles greater than 9, sum the individual digits
41+
// e.g. 7 9 9 2 7 3 9 8 7 1 3
42+
// ^ ^ ^ ^ ^
43+
// = 1+8 + 4 + 6 + 1+6 + 2
44+
for ($i = $length - 2; $i >= 0; $i -= 2) {
45+
$checkSum += array_sum(str_split((int) $value[$i] * 2));
46+
}
47+
48+
return 0 !== $checkSum && 0 === $checkSum % 10;
49+
}
50+
}

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

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
* @see http://en.wikipedia.org/wiki/Luhn_algorithm
2626
*
2727
* @author Tim Nagel <t.nagel@infinite.net.au>
28-
* @author Greg Knapp http://gregk.me/2011/php-implementation-of-bank-card-luhn-algorithm/
2928
* @author Bernhard Schussek <bschussek@gmail.com>
3029
*/
3130
class LuhnValidator extends ConstraintValidator
3231
{
32+
use LuhnChecksumTrait;
33+
3334
/**
3435
* Validates a credit card number with the Luhn algorithm.
3536
*
@@ -64,29 +65,7 @@ public function validate($value, Constraint $constraint)
6465
return;
6566
}
6667

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) {
68+
if (!$this->isChecksumCorrect($value)) {
9069
$this->context->buildViolation($constraint->message)
9170
->setParameter('{{ value }}', $this->formatValue($value))
9271
->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