IsbnValidator.php in Plug 7
File
lib/Symfony/validator/Symfony/Component/Validator/Constraints/IsbnValidator.php
View source
<?php
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class IsbnValidator extends ConstraintValidator {
public function validate($value, Constraint $constraint) {
if (!$constraint instanceof Isbn) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\Isbn');
}
if (null === $value || '' === $value) {
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
$value = (string) $value;
$canonical = str_replace('-', '', $value);
if (null === $constraint->type) {
if ($constraint->isbn10 && !$constraint->isbn13) {
$constraint->type = 'isbn10';
}
elseif ($constraint->isbn13 && !$constraint->isbn10) {
$constraint->type = 'isbn13';
}
}
if ('isbn10' === $constraint->type) {
if (true !== ($code = $this
->validateIsbn10($canonical))) {
$this
->buildViolation($this
->getMessage($constraint, $constraint->type))
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode($code)
->addViolation();
}
return;
}
if ('isbn13' === $constraint->type) {
if (true !== ($code = $this
->validateIsbn13($canonical))) {
$this
->buildViolation($this
->getMessage($constraint, $constraint->type))
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode($code)
->addViolation();
}
return;
}
$code = $this
->validateIsbn10($canonical);
if (Isbn::TOO_LONG_ERROR === $code) {
$code = $this
->validateIsbn13($canonical);
if (Isbn::TOO_SHORT_ERROR === $code) {
$code = Isbn::TYPE_NOT_RECOGNIZED_ERROR;
}
}
if (true !== $code) {
$this
->buildViolation($this
->getMessage($constraint))
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode($code)
->addViolation();
}
}
protected function validateIsbn10($isbn) {
$checkSum = 0;
for ($i = 0; $i < 10; ++$i) {
if (!isset($isbn[$i])) {
return Isbn::TOO_SHORT_ERROR;
}
if ('X' === $isbn[$i]) {
$digit = 10;
}
elseif (ctype_digit($isbn[$i])) {
$digit = $isbn[$i];
}
else {
return Isbn::INVALID_CHARACTERS_ERROR;
}
$checkSum += $digit * (10 - $i);
}
if (isset($isbn[$i])) {
return Isbn::TOO_LONG_ERROR;
}
return 0 === $checkSum % 11 ? true : Isbn::CHECKSUM_FAILED_ERROR;
}
protected function validateIsbn13($isbn) {
if (!ctype_digit($isbn)) {
return Isbn::INVALID_CHARACTERS_ERROR;
}
$length = strlen($isbn);
if ($length < 13) {
return Isbn::TOO_SHORT_ERROR;
}
if ($length > 13) {
return Isbn::TOO_LONG_ERROR;
}
$checkSum = 0;
for ($i = 0; $i < 13; $i += 2) {
$checkSum += $isbn[$i];
}
for ($i = 1; $i < 12; $i += 2) {
$checkSum += $isbn[$i] * 3;
}
return 0 === $checkSum % 10 ? true : Isbn::CHECKSUM_FAILED_ERROR;
}
protected function getMessage($constraint, $type = null) {
if (null !== $constraint->message) {
return $constraint->message;
}
elseif ('isbn10' === $type) {
return $constraint->isbn10Message;
}
elseif ('isbn13' === $type) {
return $constraint->isbn13Message;
}
return $constraint->bothIsbnMessage;
}
}
Classes
Name |
Description |
IsbnValidator |
Validates whether the value is a valid ISBN-10 or ISBN-13 |