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
IssnValidator.php
70 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 IssnValidator extends ConstraintValidator |
| 9 | { |
| 10 | public function validate($value, Constraint $constraint) |
| 11 | { |
| 12 | if (!$constraint instanceof Issn) { |
| 13 | throw new UnexpectedTypeException($constraint, Issn::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 = $value; |
| 23 | // 1234-567X |
| 24 | // ^ |
| 25 | if (isset($canonical[4]) && '-' === $canonical[4]) { |
| 26 | // remove hyphen |
| 27 | $canonical = \substr($canonical, 0, 4) . \substr($canonical, 5); |
| 28 | } elseif ($constraint->requireHyphen) { |
| 29 | $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(Issn::MISSING_HYPHEN_ERROR)->addViolation(); |
| 30 | return; |
| 31 | } |
| 32 | $length = \strlen($canonical); |
| 33 | if ($length < 8) { |
| 34 | $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(Issn::TOO_SHORT_ERROR)->addViolation(); |
| 35 | return; |
| 36 | } |
| 37 | if ($length > 8) { |
| 38 | $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(Issn::TOO_LONG_ERROR)->addViolation(); |
| 39 | return; |
| 40 | } |
| 41 | // 1234567X |
| 42 | // ^^^^^^^ digits only |
| 43 | if (!\ctype_digit(\substr($canonical, 0, 7))) { |
| 44 | $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(Issn::INVALID_CHARACTERS_ERROR)->addViolation(); |
| 45 | return; |
| 46 | } |
| 47 | // 1234567X |
| 48 | // ^ digit, x or X |
| 49 | if (!\ctype_digit($canonical[7]) && 'x' !== $canonical[7] && 'X' !== $canonical[7]) { |
| 50 | $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(Issn::INVALID_CHARACTERS_ERROR)->addViolation(); |
| 51 | return; |
| 52 | } |
| 53 | // 1234567X |
| 54 | // ^ case-sensitive? |
| 55 | if ($constraint->caseSensitive && 'x' === $canonical[7]) { |
| 56 | $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(Issn::INVALID_CASE_ERROR)->addViolation(); |
| 57 | return; |
| 58 | } |
| 59 | // Calculate a checksum. "X" equals 10. |
| 60 | $checkSum = 'X' === $canonical[7] || 'x' === $canonical[7] ? 10 : $canonical[7]; |
| 61 | for ($i = 0; $i < 7; ++$i) { |
| 62 | // Multiply the first digit by 8, the second by 7, etc. |
| 63 | $checkSum += (8 - $i) * (int) $canonical[$i]; |
| 64 | } |
| 65 | if (0 !== $checkSum % 11) { |
| 66 | $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(Issn::CHECKSUM_FAILED_ERROR)->addViolation(); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 |