Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. IsinValidator.php

class IsinValidator

@author Laurent Masforné <l.masforne@gmail.com>

Hierarchy

  • class \Symfony\Component\Validator\ConstraintValidator implements \Symfony\Component\Validator\ConstraintValidatorInterface
    • class \Symfony\Component\Validator\Constraints\IsinValidator extends \Symfony\Component\Validator\ConstraintValidator

Expanded class hierarchy of IsinValidator

See also

https://en.wikipedia.org/wiki/International_Securities_Identification_N…

File

vendor/symfony/validator/Constraints/IsinValidator.php, line 24

Namespace

Symfony\Component\Validator\Constraints
View source
class IsinValidator extends ConstraintValidator {
    public function validate(mixed $value, Constraint $constraint) : void {
        if (!$constraint instanceof Isin) {
            throw new UnexpectedTypeException($constraint, Isin::class);
        }
        if (null === $value || '' === $value) {
            return;
        }
        if (!\is_scalar($value) && !$value instanceof \Stringable) {
            throw new UnexpectedValueException($value, 'string');
        }
        $value = strtoupper($value);
        if (Isin::VALIDATION_LENGTH !== \strlen($value)) {
            $this->context
                ->buildViolation($constraint->message)
                ->setParameter('{{ value }}', $this->formatValue($value))
                ->setCode(Isin::INVALID_LENGTH_ERROR)
                ->addViolation();
            return;
        }
        if (!preg_match(Isin::VALIDATION_PATTERN, $value)) {
            $this->context
                ->buildViolation($constraint->message)
                ->setParameter('{{ value }}', $this->formatValue($value))
                ->setCode(Isin::INVALID_PATTERN_ERROR)
                ->addViolation();
            return;
        }
        if (!$this->isCorrectChecksum($value)) {
            $this->context
                ->buildViolation($constraint->message)
                ->setParameter('{{ value }}', $this->formatValue($value))
                ->setCode(Isin::INVALID_CHECKSUM_ERROR)
                ->addViolation();
        }
    }
    private function isCorrectChecksum(string $input) : bool {
        $characters = str_split($input);
        foreach ($characters as $i => $char) {
            $characters[$i] = \intval($char, 36);
        }
        $number = implode('', $characters);
        return 0 === $this->context
            ->getValidator()
            ->validate($number, new Luhn())
            ->count();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ConstraintValidator::$context protected property
ConstraintValidator::formatTypeOf protected function Returns a string representation of the type of the value.
ConstraintValidator::formatValue protected function Returns a string representation of the value.
ConstraintValidator::formatValues protected function Returns a string representation of a list of values.
ConstraintValidator::initialize public function Initializes the constraint validator. Overrides ConstraintValidatorInterface::initialize
ConstraintValidator::OBJECT_TO_STRING public constant Whether to cast objects with a &quot;__toString()&quot; method to strings.
ConstraintValidator::PRETTY_DATE public constant Whether to format {@link \DateTime} objects, either with the {@link \IntlDateFormatter}
(if it is available) or as RFC-3339 dates (&quot;Y-m-d H:i:s&quot;).
IsinValidator::isCorrectChecksum private function
IsinValidator::validate public function Checks if the passed value is valid. Overrides ConstraintValidatorInterface::validate
RSS feed
Powered by Drupal