This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Validator/Constraints/IsbnValidator.php

157 lines
4.2 KiB
PHP
Raw Normal View History

2013-01-12 13:38:26 +00:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
2014-03-27 06:27:38 +00:00
* Validates whether the value is a valid ISBN-10 or ISBN-13
2013-04-20 14:37:23 +01:00
*
2013-01-12 13:38:26 +00:00
* @author The Whole Life To Learn <thewholelifetolearn@gmail.com>
2014-03-27 06:27:38 +00:00
* @author Manuel Reinhard <manu@sprain.ch>
* @author Bernhard Schussek <bschussek@gmail.com>
2013-01-12 13:38:26 +00:00
*
2013-04-20 14:37:23 +01:00
* @see https://en.wikipedia.org/wiki/Isbn
2013-01-12 13:38:26 +00:00
*/
class IsbnValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
2013-01-12 13:38:26 +00:00
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Isbn) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Isbn');
}
2013-01-12 13:38:26 +00:00
if (null === $value || '' === $value) {
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
$value = (string) $value;
Merge branch '2.4' into 2.5 * 2.4: (24 commits) [Validator] Added Swedish translations Fix incorrect romanian plural translations fix axes handling in Crawler::filterXPath() fix some docblocks Fixed self-reference in 'service_container' service breaks garbage collection (and clone). [Process] Fix tests when pcntl is not available. [DependencyInjection] Roll back changes made to generated files. [Console] Roll back changes made to fixture files. Issue #11489 Added some CA and ES translations [Validator] Added more detailed inline documentation [Validator] Removed information from the violation output if the value is an array, object or resource partially reverted previous commit fixed CS properly handle null data when denormalizing [Validator] Renamed valueToString() to formatValue(); added missing formatValue() calls [Validator] Fixed CS [Validator] Fixed date-to-string conversion tests to match ICU 51 [Validator] Added "{{ value }}" parameters where they were missing [Validator] Simplified and explained the LuhnValidator [Validator] Simplified IssnValidator ... Conflicts: src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php src/Symfony/Component/Security/Core/Encoder/BasePasswordEncoder.php src/Symfony/Component/Validator/Constraints/ChoiceValidator.php src/Symfony/Component/Validator/Constraints/CollectionValidator.php src/Symfony/Component/Validator/Constraints/FileValidator.php src/Symfony/Component/Validator/Constraints/Isbn.php src/Symfony/Component/Validator/Constraints/IsbnValidator.php src/Symfony/Component/Validator/Constraints/LengthValidator.php src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php
2014-08-05 10:00:40 +01:00
$canonical = str_replace('-', '', $value);
2013-04-20 14:37:23 +01:00
2014-03-27 06:27:38 +00:00
if (null == $constraint->type) {
if ($constraint->isbn10 && !$constraint->isbn13) {
$constraint->type = 'isbn10';
} elseif ($constraint->isbn13 && !$constraint->isbn10) {
$constraint->type = 'isbn13';
}
}
// Explicitly validate against ISBN-10
if ('isbn10' === $constraint->type) {
if (!$this->validateIsbn10($canonical)) {
$this->buildViolation($this->getMessage($constraint, $constraint->type))
->setParameter('{{ value }}', $this->formatValue($value))
->addViolation();
}
return;
}
// Explicitly validate against ISBN-13
if ('isbn13' === $constraint->type) {
if (!$this->validateIsbn13($canonical)) {
$this->buildViolation($this->getMessage($constraint, $constraint->type))
->setParameter('{{ value }}', $this->formatValue($value))
->addViolation();
}
return;
}
// Try both ISBNs
if (!$this->validateIsbn10($canonical) && !$this->validateIsbn13($canonical)) {
$this->buildViolation($this->getMessage($constraint))
->setParameter('{{ value }}', $this->formatValue($value))
->addViolation();
2014-03-27 06:27:38 +00:00
}
}
Merge branch '2.4' into 2.5 * 2.4: (24 commits) [Validator] Added Swedish translations Fix incorrect romanian plural translations fix axes handling in Crawler::filterXPath() fix some docblocks Fixed self-reference in 'service_container' service breaks garbage collection (and clone). [Process] Fix tests when pcntl is not available. [DependencyInjection] Roll back changes made to generated files. [Console] Roll back changes made to fixture files. Issue #11489 Added some CA and ES translations [Validator] Added more detailed inline documentation [Validator] Removed information from the violation output if the value is an array, object or resource partially reverted previous commit fixed CS properly handle null data when denormalizing [Validator] Renamed valueToString() to formatValue(); added missing formatValue() calls [Validator] Fixed CS [Validator] Fixed date-to-string conversion tests to match ICU 51 [Validator] Added "{{ value }}" parameters where they were missing [Validator] Simplified and explained the LuhnValidator [Validator] Simplified IssnValidator ... Conflicts: src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php src/Symfony/Component/Security/Core/Encoder/BasePasswordEncoder.php src/Symfony/Component/Validator/Constraints/ChoiceValidator.php src/Symfony/Component/Validator/Constraints/CollectionValidator.php src/Symfony/Component/Validator/Constraints/FileValidator.php src/Symfony/Component/Validator/Constraints/Isbn.php src/Symfony/Component/Validator/Constraints/IsbnValidator.php src/Symfony/Component/Validator/Constraints/LengthValidator.php src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php
2014-08-05 10:00:40 +01:00
protected function validateIsbn10($isbn)
2014-03-27 06:27:38 +00:00
{
$checkSum = 0;
for ($i = 0; $i < 10; ++$i) {
if (!isset($isbn{$i})) {
return false;
}
if ('X' === $isbn{$i}) {
$digit = 10;
} elseif (ctype_digit($isbn{$i})) {
$digit = $isbn{$i};
2014-03-27 06:27:38 +00:00
} else {
return false;
2013-01-12 13:38:26 +00:00
}
$checkSum += $digit * intval(10 - $i);
2014-03-27 06:27:38 +00:00
}
if (isset($isbn{$i})) {
return false;
}
return 0 === $checkSum % 11;
2014-03-27 06:27:38 +00:00
}
Merge branch '2.4' into 2.5 * 2.4: (24 commits) [Validator] Added Swedish translations Fix incorrect romanian plural translations fix axes handling in Crawler::filterXPath() fix some docblocks Fixed self-reference in 'service_container' service breaks garbage collection (and clone). [Process] Fix tests when pcntl is not available. [DependencyInjection] Roll back changes made to generated files. [Console] Roll back changes made to fixture files. Issue #11489 Added some CA and ES translations [Validator] Added more detailed inline documentation [Validator] Removed information from the violation output if the value is an array, object or resource partially reverted previous commit fixed CS properly handle null data when denormalizing [Validator] Renamed valueToString() to formatValue(); added missing formatValue() calls [Validator] Fixed CS [Validator] Fixed date-to-string conversion tests to match ICU 51 [Validator] Added "{{ value }}" parameters where they were missing [Validator] Simplified and explained the LuhnValidator [Validator] Simplified IssnValidator ... Conflicts: src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php src/Symfony/Component/Security/Core/Encoder/BasePasswordEncoder.php src/Symfony/Component/Validator/Constraints/ChoiceValidator.php src/Symfony/Component/Validator/Constraints/CollectionValidator.php src/Symfony/Component/Validator/Constraints/FileValidator.php src/Symfony/Component/Validator/Constraints/Isbn.php src/Symfony/Component/Validator/Constraints/IsbnValidator.php src/Symfony/Component/Validator/Constraints/LengthValidator.php src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php
2014-08-05 10:00:40 +01:00
protected function validateIsbn13($isbn)
2014-03-27 06:27:38 +00:00
{
if (!ctype_digit($isbn)) {
return false;
}
$length = strlen($isbn);
if ($length < 13) {
return false;
}
if ($length > 13) {
return false;
}
2014-03-27 06:27:38 +00:00
$checkSum = 0;
2013-01-12 13:38:26 +00:00
for ($i = 0; $i < 13; $i += 2) {
$checkSum += $isbn{$i};
2013-01-12 13:38:26 +00:00
}
2014-03-27 06:27:38 +00:00
for ($i = 1; $i < 12; $i += 2) {
2014-09-21 19:53:12 +01:00
$checkSum += $isbn{$i}
* 3;
}
return 0 === $checkSum % 10;
2014-03-27 06:27:38 +00:00
}
Merge branch '2.4' into 2.5 * 2.4: (24 commits) [Validator] Added Swedish translations Fix incorrect romanian plural translations fix axes handling in Crawler::filterXPath() fix some docblocks Fixed self-reference in 'service_container' service breaks garbage collection (and clone). [Process] Fix tests when pcntl is not available. [DependencyInjection] Roll back changes made to generated files. [Console] Roll back changes made to fixture files. Issue #11489 Added some CA and ES translations [Validator] Added more detailed inline documentation [Validator] Removed information from the violation output if the value is an array, object or resource partially reverted previous commit fixed CS properly handle null data when denormalizing [Validator] Renamed valueToString() to formatValue(); added missing formatValue() calls [Validator] Fixed CS [Validator] Fixed date-to-string conversion tests to match ICU 51 [Validator] Added "{{ value }}" parameters where they were missing [Validator] Simplified and explained the LuhnValidator [Validator] Simplified IssnValidator ... Conflicts: src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php src/Symfony/Component/Security/Core/Encoder/BasePasswordEncoder.php src/Symfony/Component/Validator/Constraints/ChoiceValidator.php src/Symfony/Component/Validator/Constraints/CollectionValidator.php src/Symfony/Component/Validator/Constraints/FileValidator.php src/Symfony/Component/Validator/Constraints/Isbn.php src/Symfony/Component/Validator/Constraints/IsbnValidator.php src/Symfony/Component/Validator/Constraints/LengthValidator.php src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php
2014-08-05 10:00:40 +01:00
protected function getMessage($constraint, $type = null)
2014-03-27 06:27:38 +00:00
{
if (null !== $constraint->message) {
return $constraint->message;
} elseif ('isbn10' === $type) {
2014-03-27 06:27:38 +00:00
return $constraint->isbn10Message;
} elseif ('isbn13' === $type) {
2014-03-27 06:27:38 +00:00
return $constraint->isbn13Message;
}
Merge branch '2.4' into 2.5 * 2.4: (24 commits) [Validator] Added Swedish translations Fix incorrect romanian plural translations fix axes handling in Crawler::filterXPath() fix some docblocks Fixed self-reference in 'service_container' service breaks garbage collection (and clone). [Process] Fix tests when pcntl is not available. [DependencyInjection] Roll back changes made to generated files. [Console] Roll back changes made to fixture files. Issue #11489 Added some CA and ES translations [Validator] Added more detailed inline documentation [Validator] Removed information from the violation output if the value is an array, object or resource partially reverted previous commit fixed CS properly handle null data when denormalizing [Validator] Renamed valueToString() to formatValue(); added missing formatValue() calls [Validator] Fixed CS [Validator] Fixed date-to-string conversion tests to match ICU 51 [Validator] Added "{{ value }}" parameters where they were missing [Validator] Simplified and explained the LuhnValidator [Validator] Simplified IssnValidator ... Conflicts: src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php src/Symfony/Component/Security/Core/Encoder/BasePasswordEncoder.php src/Symfony/Component/Validator/Constraints/ChoiceValidator.php src/Symfony/Component/Validator/Constraints/CollectionValidator.php src/Symfony/Component/Validator/Constraints/FileValidator.php src/Symfony/Component/Validator/Constraints/Isbn.php src/Symfony/Component/Validator/Constraints/IsbnValidator.php src/Symfony/Component/Validator/Constraints/LengthValidator.php src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php
2014-08-05 10:00:40 +01:00
return $constraint->bothIsbnMessage;
2013-01-12 13:38:26 +00:00
}
}