-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
403 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
90
src/Symfony/Component/Validator/Constraints/IsbnValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.