-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Improved ISBN validator #10546
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
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,10 @@ | |
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
/** | ||
* Validates whether the value is a valid ISBN-10 or ISBN-13. | ||
* Validates whether the value is a valid ISBN-10 or ISBN-13 | ||
* | ||
* @author The Whole Life To Learn <thewholelifetolearn@gmail.com> | ||
* | ||
* @author Manuel Reinhard <manu@sprain.ch> | ||
* @see https://en.wikipedia.org/wiki/Isbn | ||
*/ | ||
class IsbnValidator extends ConstraintValidator | ||
|
@@ -45,11 +45,43 @@ public function validate($value, Constraint $constraint) | |
$value = str_replace('-', '', $value); | ||
} | ||
|
||
$validation = 0; | ||
$value = strtoupper($value); | ||
if (null == $constraint->type) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if ($constraint->isbn10 && !$constraint->isbn13) { | ||
$constraint->type = 'isbn10'; | ||
$value = strtoupper($value); | ||
} elseif ($constraint->isbn13 && !$constraint->isbn10) { | ||
$constraint->type = 'isbn13'; | ||
$value = strtoupper($value); | ||
} | ||
} | ||
|
||
if ('isbn10' === $constraint->type) { | ||
if (!$this->validateIsbn10($value)) { | ||
$this->context->addViolation($this->getMessage($constraint, 'isbn10')); | ||
|
||
return; | ||
} | ||
} elseif ('isbn13' === $constraint->type) { | ||
if (!$this->validateIsbn13($value)) { | ||
$this->context->addViolation($this->getMessage($constraint, 'isbn13')); | ||
|
||
return; | ||
} | ||
} else { | ||
if (!$this->validateIsbn10($value) && !$this->validateIsbn13($value)) { | ||
$this->context->addViolation($this->getMessage($constraint)); | ||
|
||
return; | ||
} | ||
} | ||
} | ||
|
||
protected function validateIsbn10($value) | ||
{ | ||
$validation = 0; | ||
$valueLength = strlen($value); | ||
|
||
if (10 === $valueLength && null !== $constraint->isbn10) { | ||
if (10 === $valueLength) { | ||
for ($i = 0; $i < 10; $i++) { | ||
if ($value[$i] == 'X') { | ||
$validation += 10 * intval(10 - $i); | ||
|
@@ -59,13 +91,21 @@ public function validate($value, Constraint $constraint) | |
} | ||
|
||
if ($validation % 11 != 0) { | ||
if (null !== $constraint->isbn13) { | ||
$this->context->addViolation($constraint->bothIsbnMessage); | ||
} else { | ||
$this->context->addViolation($constraint->isbn10Message); | ||
} | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} elseif (13 === $valueLength && null !== $constraint->isbn13) { | ||
} | ||
|
||
return false; | ||
} | ||
|
||
protected function validateIsbn13($value) | ||
{ | ||
$validation = 0; | ||
$valueLength = strlen($value); | ||
|
||
if (13 === $valueLength) { | ||
for ($i = 0; $i < 13; $i += 2) { | ||
$validation += intval($value[$i]); | ||
} | ||
|
@@ -74,20 +114,25 @@ public function validate($value, Constraint $constraint) | |
} | ||
|
||
if ($validation % 10 != 0) { | ||
if (null !== $constraint->isbn10) { | ||
$this->context->addViolation($constraint->bothIsbnMessage); | ||
} else { | ||
$this->context->addViolation($constraint->isbn13Message); | ||
} | ||
} | ||
} else { | ||
if (null !== $constraint->isbn10 && null !== $constraint->isbn13) { | ||
$this->context->addViolation($constraint->bothIsbnMessage); | ||
} elseif (null !== $constraint->isbn10) { | ||
$this->context->addViolation($constraint->isbn10Message); | ||
return false; | ||
} else { | ||
$this->context->addViolation($constraint->isbn13Message); | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
protected function getMessage($constraint, $type=null) | ||
{ | ||
if (null !== $constraint->message) { | ||
return $constraint->message; | ||
} elseif ($type == 'isbn10') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'isbn10' === $type |
||
return $constraint->isbn10Message; | ||
} elseif ($type == 'isbn13') { | ||
return $constraint->isbn13Message; | ||
} else { | ||
return $constraint->bothIsbnMessage; | ||
} | ||
} | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really like
type
. What if there is another one likeisbn15
you cannot specify that you allow isbn13 and isbn15 but not isbn10 with a "type". But with the booleans you can.Also it's not clear what values "type" actualyl accept. Much easier with booleans.
Also why deprecating stuff when it's not necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original issue #10386 shows, that it's not so clear with the booleans.
Also, the chance that there will be an ISBN15 type or so is very small. ISBN13 has been introduced in 2007, after 35 years of ISBN10.