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
Fabien Potencier b91866f6c1 Merge branch '2.4' into 2.5
* 2.4:
  fixed CS
  [Process] fixed some volatile tests
  [HttpKernel] fixed a volatile test
  [HttpFoundation] fixed some volatile tests
  [Tests] PHPUnit Optimizations
  Use getPathname() instead of string casting to get BinaryFileReponse file path

Conflicts:
	src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php
	src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php
	src/Symfony/Component/ClassLoader/Tests/ApcUniversalClassLoaderTest.php
	src/Symfony/Component/Console/Formatter/OutputFormatterStyle.php
	src/Symfony/Component/HttpKernel/DataCollector/LoggerDataCollector.php
	src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php
	src/Symfony/Component/HttpKernel/Tests/DataCollector/LoggerDataCollectorTest.php
	src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php
	src/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php
	src/Symfony/Component/Process/Tests/AbstractProcessTest.php
	src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
	src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php
	src/Symfony/Component/Security/Acl/Dbal/MutableAclProvider.php
	src/Symfony/Component/Security/Core/Authentication/Provider/PreAuthenticatedAuthenticationProvider.php
	src/Symfony/Component/Security/Http/Tests/Firewall/SwitchUserListenerTest.php
	src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php
	src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php
	src/Symfony/Component/Validator/Constraints/ChoiceValidator.php
	src/Symfony/Component/Validator/Constraints/CollectionValidator.php
	src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php
	src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php
	src/Symfony/Component/Validator/Tests/ValidationVisitorTest.php
	src/Symfony/Component/Yaml/Parser.php
2014-09-22 11:14:18 +02:00

128 lines
3.6 KiB
PHP

<?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;
/**
* 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>
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @see https://en.wikipedia.org/wiki/Isbn
*/
class IsbnValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
*/
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 && !$this->validateIsbn10($canonical)) {
$this->context->addViolation($this->getMessage($constraint, 'isbn10'), array(
'{{ value }}' => $this->formatValue($value),
));
} elseif ('isbn13' === $constraint->type && !$this->validateIsbn13($canonical)) {
$this->context->addViolation($this->getMessage($constraint, 'isbn13'), array(
'{{ value }}' => $this->formatValue($value),
));
} elseif (!$this->validateIsbn10($canonical) && !$this->validateIsbn13($canonical)) {
$this->context->addViolation($this->getMessage($constraint), array(
'{{ value }}' => $this->formatValue($value),
));
}
}
protected function validateIsbn10($isbn)
{
if (10 !== strlen($isbn)) {
return false;
}
$checkSum = 0;
for ($i = 0; $i < 10; ++$i) {
if ('X' === $isbn{$i}) {
$digit = 10;
} elseif (ctype_digit($isbn{$i})) {
$digit = $isbn{$i};
} else {
return false;
}
$checkSum += $digit * intval(10 - $i);
}
return 0 === $checkSum % 11;
}
protected function validateIsbn13($isbn)
{
if (13 !== strlen($isbn) || !ctype_digit($isbn)) {
return false;
}
$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;
}
protected function getMessage($constraint, $type = null)
{
if (null !== $constraint->message) {
return $constraint->message;
} elseif ($type == 'isbn10') {
return $constraint->isbn10Message;
} elseif ($type == 'isbn13') {
return $constraint->isbn13Message;
}
return $constraint->bothIsbnMessage;
}
}