Skip to content

Commit

Permalink
merged branch thewholelifetolearn/master (PR #6718)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the master branch (closes #6718).

Discussion
----------

[Validator] Added ISBN validator

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
License of the code: MIT

For information about ISBN-10 and ISBN13: https://en.wikipedia.org/wiki/Isbn

This constraint permits to valid a ISBN-10 code, a ISBN-13 code or both on a value

Commits
-------

4582261 [Validator] Added ISBN validator
  • Loading branch information
fabpot committed Apr 20, 2013
2 parents 4f7e3d4 + 4582261 commit cf526ff
Show file tree
Hide file tree
Showing 6 changed files with 403 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Isbn.php
@@ -0,0 +1,47 @@
<?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\Exception\MissingOptionsException;

/**
* @Annotation
*
* @author The Whole Life To Learn <thewholelifetolearn@gmail.com>
*
* @api
*/
class Isbn extends Constraint
{
public $isbn10Message = 'This value is not a valid ISBN-10.';
public $isbn13Message = 'This value is not a valid ISBN-13.';
public $bothIsbnMessage = 'This value is neither a valid ISBN-10 nor a valid ISBN-13.';
public $isbn10;
public $isbn13;

public function __construct($options = null)
{
if (null !== $options && !is_array($options)) {
$options = array(
'isbn10' => $options,
'isbn13' => $options,
);
}

parent::__construct($options);

if (null === $this->isbn10 && null === $this->isbn13) {
throw new MissingOptionsException('Either option "isbn10" or "isbn13" must be given for constraint ' . __CLASS__, array('isbn10', 'isbn13'));
}
}
}
90 changes: 90 additions & 0 deletions src/Symfony/Component/Validator/Constraints/IsbnValidator.php
@@ -0,0 +1,90 @@
<?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 wether the value is a valid ISBN-10 or ISBN-13
*
* @see https://en.wikipedia.org/wiki/Isbn
* @author The Whole Life To Learn <thewholelifetolearn@gmail.com>
*
* @api
*/
class IsbnValidator extends ConstraintValidator
{
/**
* {@inheritDoc}
*/
public function validate($value, Constraint $constraint)
{
if (null === $value || '' === $value) {
return;
}

if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}

if (!is_numeric($value)) {
$value = str_replace('-', '', $value);
}

$validation = 0;
$value = strtoupper($value);
$valueLength = strlen($value);

if (10 === $valueLength && null !== $constraint->isbn10) {
for ($i = 0; $i < 10; $i++) {
if ($value[$i] == 'X') {
$validation += 10 * intval(10 - $i);
} else {
$validation += intval($value[$i]) * intval(10 - $i);
}
}

if ($validation % 11 != 0) {
if (null !== $constraint->isbn13) {
$this->context->addViolation($constraint->bothIsbnMessage);
} else {
$this->context->addViolation($constraint->isbn10Message);
}
}
} elseif (13 === $valueLength && null !== $constraint->isbn13) {
for ($i = 0; $i < 13; $i += 2) {
$validation += intval($value[$i]);
}
for ($i = 1; $i < 12; $i += 2) {
$validation += intval($value[$i]) * 3;
}

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);
} else {
$this->context->addViolation($constraint->isbn13Message);
}
}
}
}
Expand Up @@ -226,6 +226,18 @@
<source>This is not a valid International Bank Account Number (IBAN).</source>
<target>Dieser Wert ist keine gültige IBAN-Kontonummer.</target>
</trans-unit>
<trans-unit id="60">
<source>This value is not a valid ISBN-10.</source>
<target>Dieser Wert entspricht keinem gültigen ISBN-10.</target>
</trans-unit>
<trans-unit id="61">
<source>This value is not a valid ISBN-13.</source>
<target>Dieser Wert entspricht keinem gültigen ISBN-13.</target>
</trans-unit>
<trans-unit id="62">
<source>This value is neither a valid ISBN-10 nor a valid ISBN-13.</source>
<target>Dieser Wert ist weder ein gültiges ISBN-10 noch ein gültige ISBN-13.</target>
</trans-unit>
</body>
</file>
</xliff>
Expand Up @@ -226,6 +226,18 @@
<source>This is not a valid International Bank Account Number (IBAN).</source>
<target>This is not a valid International Bank Account Number (IBAN).</target>
</trans-unit>
<trans-unit id="60">
<source>This value is not a valid ISBN-10.</source>
<target>This value is not a valid ISBN-10.</target>
</trans-unit>
<trans-unit id="61">
<source>This value is not a valid ISBN-13.</source>
<target>This value is not a valid ISBN-13.</target>
</trans-unit>
<trans-unit id="62">
<source>This value is neither a valid ISBN-10 nor a valid ISBN-13.</source>
<target>This value is neither a valid ISBN-10 nor a valid ISBN-13.</target>
</trans-unit>
</body>
</file>
</xliff>
Expand Up @@ -226,6 +226,18 @@
<source>This is not a valid International Bank Account Number (IBAN).</source>
<target>Le numéro IBAN (International Bank Account Number) saisi n'est pas valide.</target>
</trans-unit>
<trans-unit id="60">
<source>This value is not a valid ISBN-10.</source>
<target>Cette valeur n'est pas un code ISBN-10 valide.</target>
</trans-unit>
<trans-unit id="61">
<source>This value is not a valid ISBN-13.</source>
<target>Cette valeur n'est pas un code ISBN-13 valide.</target>
</trans-unit>
<trans-unit id="62">
<source>This value is neither a valid ISBN-10 nor a valid ISBN-13.</source>
<target>Cette valeur n'est ni un code ISBN-10, ni un code ISBN-13 valide.</target>
</trans-unit>
</body>
</file>
</xliff>

0 comments on commit cf526ff

Please sign in to comment.