8000 fix #10386 isbnValidator so to allow to specify with false any isbn10 or 113 by cordoval · Pull Request #10387 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

fix #10386 isbnValidator so to allow to specify with false any isbn10 or 113 #10387

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
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
14 changes: 8 additions & 6 deletions src/Symfony/Component/Validator/Constraints/IsbnValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function validate($value, Constraint $constraint)
$value = strtoupper($value);
$valueLength = strlen($value);

if (10 === $valueLength && null !== $constraint->isbn10) {
if (10 === $valueLength && null !== $constraint->isbn10 && false !== $constraint->isbn10) {
for ($i = 0; $i < 10; $i++) {
if ($value[$i] == 'X') {
$validation += 10 * intval(10 - $i);
Expand All @@ -55,13 +55,13 @@ public function validate($value, Constraint $constraint)
}

if ($validation % 11 != 0) {
if (null !== $constraint->isbn13) {
if (null !== $constraint->isbn13 && false !== $constraint->isbn13) {
$this->context->addViolation($constraint->bothIsbnMessage);
} else {
$this->context->addViolation($constraint->isbn10Message);
}
}
} elseif (13 === $valueLength && null !== $constraint->isbn13) {
} elseif (13 === $valueLength && null !== $constraint->isbn13 && false !== $constraint->isbn13) {
for ($i = 0; $i < 13; $i += 2) {
$validation += intval($value[$i]);
}
Expand All @@ -70,16 +70,18 @@ public function validate($value, Constraint $constraint)
}

if ($validation % 10 != 0) {
if (null !== $constraint->isbn10) {
if (null !== $constraint->isbn10 && false !== $constraint->isbn10) {
$this->context->addViolation($constraint->bothIsbnMessage);
} else {
$this->context->addViolation($constraint->isbn13Message);
}
}
} else {
if (null !== $constraint->isbn10 && null !== $constraint->isbn13) {
if (null !== $constraint->isbn10 && null !== $constraint->isbn13 &&
false !== $constraint->isbn10 && false !== $constraint->isbn13
) {
$this->context->addViolation($constraint->bothIsbnMessage);
} elseif (null !== $constraint->isbn10) {
} elseif (null !== $constraint->isbn10 && false !== $constraint->isbn10) {
$this->context->addViolation($constraint->isbn10Message);
} else {
$this->context->addViolation($constraint->isbn13Message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,18 @@ public function testInvalidIsbn($isbn)

$this->validator->validate($isbn, $constraint);
}

/**
* @dataProvider getInvalidIsbn
*/
public function testInvalidIsbnMixed($isbn)
{
$constraint = new Isbn(array('isbn10' => false, 'isbn13' => true));
$this->context
->expects($this->once())
->method('addViolation')
->with($constraint->isbn13Message);

$this->validator->validate($isbn, $constraint);
}
}
0