-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Improved ISBN validator #10542
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,12 @@ | |
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
/** | ||
* Validates whether the value is a valid ISBN-10 or ISBN-13. | ||
* Validates wether the value is a valid ISBN-10 or ISBN-13 | ||
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.
|
||
* | ||
* @author The Whole Life To Learn <thewholelifetolearn@gmail.com> | ||
* @author Manuel Reinhard <manu@sprain.ch> | ||
* | ||
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. what was it removed? 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. By mistake. Putting back in. |
||
* @see https://en.wikipedia.org/wiki/Isbn | ||
* @api | ||
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. must be removed 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. Will do. What is it for? Some validators have this annotation. 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. @sprain these annotations are there to mark a stable api. |
||
*/ | ||
class IsbnValidator extends ConstraintValidator | ||
{ | ||
|
@@ -41,11 +42,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. should be a strict comparison |
||
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); | ||
|
@@ -55,13 +88,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; | ||
} | ||
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. this can be changed to 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. It can. But is it more readable? Or is there a coding standard for this? |
||
} elseif (13 === $valueLength && null !== $constraint->isbn13) { | ||
} | ||
|
||
return false; | ||
} | ||
|
||
protected function validateIsbn13($value) | ||
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. should be private |
||
{ | ||
$validation = 0; | ||
$valueLength = strlen($value); | ||
|
||
if (13 === $valueLength) { | ||
for ($i = 0; $i < 13; $i += 2) { | ||
$validation += intval($value[$i]); | ||
} | ||
|
@@ -70,20 +111,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) | ||
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. should be 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. Why private? 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. because the rule in Symfony is that we use private everywhere until we decide that a method needs to be protected to be a supported extension point (for which we then guarantee BC until 3.0) |
||
{ | ||
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. this should simply be 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. and it should be a strict comparison |
||
return $constraint->isbn10Message; | ||
} elseif ($type == 'isbn13') { | ||
return $constraint->isbn13Message; | ||
} else { | ||
return $constraint->bothIsbnMessage; | ||
} | ||
} | ||
} |
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.
must be removed