AbstractComparison.php
1 year ago
AbstractComparisonValidator.php
1 year ago
All.php
1 year ago
AllValidator.php
4 years ago
AtLeastOneOf.php
1 year ago
AtLeastOneOfValidator.php
1 year ago
Bic.php
1 year ago
BicValidator.php
1 year ago
Blank.php
1 year ago
BlankValidator.php
4 years ago
Callback.php
1 year ago
CallbackValidator.php
4 years ago
CardScheme.php
1 year ago
CardSchemeValidator.php
1 year ago
Cascade.php
1 year ago
Choice.php
1 year ago
ChoiceValidator.php
1 year ago
Cidr.php
1 year ago
CidrValidator.php
4 years ago
Collection.php
1 year ago
CollectionValidator.php
4 years ago
Composite.php
1 year ago
Compound.php
4 years ago
CompoundValidator.php
4 years ago
Count.php
1 year ago
CountValidator.php
4 years ago
Country.php
1 year ago
CountryValidator.php
4 years ago
CssColor.php
1 year ago
CssColorValidator.php
1 year ago
Currency.php
1 year ago
CurrencyValidator.php
4 years ago
Date.php
1 year ago
DateTime.php
1 year ago
DateTimeValidator.php
2 years ago
DateValidator.php
1 year ago
DisableAutoMapping.php
1 year ago
DivisibleBy.php
4 years ago
DivisibleByValidator.php
4 years ago
Email.php
1 year ago
EmailValidator.php
1 year ago
EnableAutoMapping.php
1 year ago
EqualTo.php
4 years ago
EqualToValidator.php
4 years ago
Existence.php
4 years ago
Expression.php
1 year ago
ExpressionLanguageSyntax.php
1 year ago
ExpressionLanguageSyntaxValidator.php
1 year ago
ExpressionValidator.php
1 year ago
File.php
1 year ago
FileValidator.php
4 years ago
GreaterThan.php
4 years ago
GreaterThanOrEqual.php
4 years ago
GreaterThanOrEqualValidator.php
4 years ago
GreaterThanValidator.php
4 years ago
GroupSequence.php
4 years ago
GroupSequenceProvider.php
4 years ago
Hostname.php
1 year ago
HostnameValidator.php
4 years ago
Iban.php
1 year ago
IbanValidator.php
1 year ago
IdenticalTo.php
4 years ago
IdenticalToValidator.php
4 years ago
Image.php
1 year ago
ImageValidator.php
4 years ago
Ip.php
1 year ago
IpValidator.php
4 years ago
IsFalse.php
1 year ago
IsFalseValidator.php
4 years ago
IsNull.php
1 year ago
IsNullValidator.php
4 years ago
IsTrue.php
1 year ago
IsTrueValidator.php
4 years ago
Isbn.php
1 year ago
IsbnValidator.php
1 year ago
Isin.php
1 year ago
IsinValidator.php
4 years ago
Issn.php
1 year ago
IssnValidator.php
4 years ago
Json.php
1 year ago
JsonValidator.php
1 year ago
Language.php
1 year ago
LanguageValidator.php
4 years ago
Length.php
1 year ago
LengthValidator.php
4 years ago
LessThan.php
4 years ago
LessThanOrEqual.php
4 years ago
LessThanOrEqualValidator.php
4 years ago
LessThanValidator.php
4 years ago
Locale.php
1 year ago
LocaleValidator.php
4 years ago
Luhn.php
1 year ago
LuhnValidator.php
4 years ago
Negative.php
4 years ago
NegativeOrZero.php
4 years ago
NotBlank.php
1 year ago
NotBlankValidator.php
4 years ago
NotCompromisedPassword.php
1 year ago
NotCompromisedPasswordValidator.php
1 year ago
NotEqualTo.php
4 years ago
NotEqualToValidator.php
4 years ago
NotIdenticalTo.php
4 years ago
NotIdenticalToValidator.php
4 years ago
NotNull.php
1 year ago
NotNullValidator.php
4 years ago
NumberConstraintTrait.php
4 years ago
Optional.php
4 years ago
Positive.php
4 years ago
PositiveOrZero.php
4 years ago
Range.php
1 year ago
RangeValidator.php
1 year ago
Regex.php
1 year ago
RegexValidator.php
4 years ago
Required.php
4 years ago
Sequentially.php
1 year ago
SequentiallyValidator.php
4 years ago
Time.php
1 year ago
TimeValidator.php
1 year ago
Timezone.php
1 year ago
TimezoneValidator.php
1 year ago
Traverse.php
4 years ago
Type.php
1 year ago
TypeValidator.php
4 years ago
Ulid.php
1 year ago
UlidValidator.php
2 years ago
Unique.php
1 year ago
UniqueValidator.php
1 year ago
Url.php
1 year ago
UrlValidator.php
1 year ago
Uuid.php
1 year ago
UuidValidator.php
4 years ago
Valid.php
1 year ago
ValidValidator.php
4 years ago
ZeroComparisonConstraintTrait.php
1 year ago
index.php
3 years ago
IsbnValidator.php
122 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Validator\Constraints; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\Validator\Constraint; |
| 5 | use MailPoetVendor\Symfony\Component\Validator\ConstraintValidator; |
| 6 | use MailPoetVendor\Symfony\Component\Validator\Exception\UnexpectedTypeException; |
| 7 | use MailPoetVendor\Symfony\Component\Validator\Exception\UnexpectedValueException; |
| 8 | class IsbnValidator extends ConstraintValidator |
| 9 | { |
| 10 | public function validate($value, Constraint $constraint) |
| 11 | { |
| 12 | if (!$constraint instanceof Isbn) { |
| 13 | throw new UnexpectedTypeException($constraint, Isbn::class); |
| 14 | } |
| 15 | if (null === $value || '' === $value) { |
| 16 | return; |
| 17 | } |
| 18 | if (!\is_scalar($value) && !(\is_object($value) && \method_exists($value, '__toString'))) { |
| 19 | throw new UnexpectedValueException($value, 'string'); |
| 20 | } |
| 21 | $value = (string) $value; |
| 22 | $canonical = \str_replace('-', '', $value); |
| 23 | // Explicitly validate against ISBN-10 |
| 24 | if (Isbn::ISBN_10 === $constraint->type) { |
| 25 | if (\true !== ($code = $this->validateIsbn10($canonical))) { |
| 26 | $this->context->buildViolation($this->getMessage($constraint, $constraint->type))->setParameter('{{ value }}', $this->formatValue($value))->setCode($code)->addViolation(); |
| 27 | } |
| 28 | return; |
| 29 | } |
| 30 | // Explicitly validate against ISBN-13 |
| 31 | if (Isbn::ISBN_13 === $constraint->type) { |
| 32 | if (\true !== ($code = $this->validateIsbn13($canonical))) { |
| 33 | $this->context->buildViolation($this->getMessage($constraint, $constraint->type))->setParameter('{{ value }}', $this->formatValue($value))->setCode($code)->addViolation(); |
| 34 | } |
| 35 | return; |
| 36 | } |
| 37 | // Try both ISBNs |
| 38 | // First, try ISBN-10 |
| 39 | $code = $this->validateIsbn10($canonical); |
| 40 | // The ISBN can only be an ISBN-13 if the value was too long for ISBN-10 |
| 41 | if (Isbn::TOO_LONG_ERROR === $code) { |
| 42 | // Try ISBN-13 now |
| 43 | $code = $this->validateIsbn13($canonical); |
| 44 | // If too short, this means we have 11 or 12 digits |
| 45 | if (Isbn::TOO_SHORT_ERROR === $code) { |
| 46 | $code = Isbn::TYPE_NOT_RECOGNIZED_ERROR; |
| 47 | } |
| 48 | } |
| 49 | if (\true !== $code) { |
| 50 | $this->context->buildViolation($this->getMessage($constraint))->setParameter('{{ value }}', $this->formatValue($value))->setCode($code)->addViolation(); |
| 51 | } |
| 52 | } |
| 53 | protected function validateIsbn10(string $isbn) |
| 54 | { |
| 55 | // Choose an algorithm so that ERROR_INVALID_CHARACTERS is preferred |
| 56 | // over ERROR_TOO_SHORT/ERROR_TOO_LONG |
| 57 | // Otherwise "0-45122-5244" passes, but "0-45122_5244" reports |
| 58 | // "too long" |
| 59 | // Error priority: |
| 60 | // 1. ERROR_INVALID_CHARACTERS |
| 61 | // 2. ERROR_TOO_SHORT/ERROR_TOO_LONG |
| 62 | // 3. ERROR_CHECKSUM_FAILED |
| 63 | $checkSum = 0; |
| 64 | for ($i = 0; $i < 10; ++$i) { |
| 65 | // If we test the length before the loop, we get an ERROR_TOO_SHORT |
| 66 | // when actually an ERROR_INVALID_CHARACTERS is wanted, e.g. for |
| 67 | // "0-45122_5244" (typo) |
| 68 | if (!isset($isbn[$i])) { |
| 69 | return Isbn::TOO_SHORT_ERROR; |
| 70 | } |
| 71 | if ('X' === $isbn[$i]) { |
| 72 | $digit = 10; |
| 73 | } elseif (\ctype_digit($isbn[$i])) { |
| 74 | $digit = $isbn[$i]; |
| 75 | } else { |
| 76 | return Isbn::INVALID_CHARACTERS_ERROR; |
| 77 | } |
| 78 | $checkSum += $digit * (10 - $i); |
| 79 | } |
| 80 | if (isset($isbn[$i])) { |
| 81 | return Isbn::TOO_LONG_ERROR; |
| 82 | } |
| 83 | return 0 === $checkSum % 11 ? \true : Isbn::CHECKSUM_FAILED_ERROR; |
| 84 | } |
| 85 | protected function validateIsbn13(string $isbn) |
| 86 | { |
| 87 | // Error priority: |
| 88 | // 1. ERROR_INVALID_CHARACTERS |
| 89 | // 2. ERROR_TOO_SHORT/ERROR_TOO_LONG |
| 90 | // 3. ERROR_CHECKSUM_FAILED |
| 91 | if (!\ctype_digit($isbn)) { |
| 92 | return Isbn::INVALID_CHARACTERS_ERROR; |
| 93 | } |
| 94 | $length = \strlen($isbn); |
| 95 | if ($length < 13) { |
| 96 | return Isbn::TOO_SHORT_ERROR; |
| 97 | } |
| 98 | if ($length > 13) { |
| 99 | return Isbn::TOO_LONG_ERROR; |
| 100 | } |
| 101 | $checkSum = 0; |
| 102 | for ($i = 0; $i < 13; $i += 2) { |
| 103 | $checkSum += $isbn[$i]; |
| 104 | } |
| 105 | for ($i = 1; $i < 12; $i += 2) { |
| 106 | $checkSum += $isbn[$i] * 3; |
| 107 | } |
| 108 | return 0 === $checkSum % 10 ? \true : Isbn::CHECKSUM_FAILED_ERROR; |
| 109 | } |
| 110 | protected function getMessage(Isbn $constraint, ?string $type = null) |
| 111 | { |
| 112 | if (null !== $constraint->message) { |
| 113 | return $constraint->message; |
| 114 | } elseif (Isbn::ISBN_10 === $type) { |
| 115 | return $constraint->isbn10Message; |
| 116 | } elseif (Isbn::ISBN_13 === $type) { |
| 117 | return $constraint->isbn13Message; |
| 118 | } |
| 119 | return $constraint->bothIsbnMessage; |
| 120 | } |
| 121 | } |
| 122 |