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

Breadcrumb

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

function UuidValidator::validateStrict

1 call to UuidValidator::validateStrict()
UuidValidator::validate in vendor/symfony/validator/Constraints/UuidValidator.php
Checks if the passed value is valid.

File

vendor/symfony/validator/Constraints/UuidValidator.php, line 162

Class

UuidValidator
Validates whether the value is a valid UUID (also known as GUID).

Namespace

Symfony\Component\Validator\Constraints

Code

private function validateStrict(string $value, Uuid $constraint) : void {
    // Error priority:
    // 1. ERROR_INVALID_CHARACTERS
    // 2. ERROR_INVALID_HYPHEN_PLACEMENT
    // 3. ERROR_TOO_SHORT/ERROR_TOO_LONG
    // 4. ERROR_INVALID_VERSION
    // 5. ERROR_INVALID_VARIANT
    // Position of the next expected hyphen
    $h = self::STRICT_FIRST_HYPHEN_POSITION;
    for ($i = 0; $i < self::STRICT_LENGTH; ++$i) {
        // Check length
        if (!isset($value[$i])) {
            $this->context
                ->buildViolation($constraint->message)
                ->setParameter('{{ value }}', $this->formatValue($value))
                ->setCode(Uuid::TOO_SHORT_ERROR)
                ->addViolation();
            return;
        }
        // Check hyphen placement
        // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        //         ^    ^    ^    ^
        if ('-' === $value[$i]) {
            if ($i !== $h) {
                $this->context
                    ->buildViolation($constraint->message)
                    ->setParameter('{{ value }}', $this->formatValue($value))
                    ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
                    ->addViolation();
                return;
            }
            // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
            //                        ^
            if ($h < self::STRICT_LAST_HYPHEN_POSITION) {
                $h += 5;
            }
            continue;
        }
        // Check characters
        if (!ctype_xdigit($value[$i])) {
            $this->context
                ->buildViolation($constraint->message)
                ->setParameter('{{ value }}', $this->formatValue($value))
                ->setCode(Uuid::INVALID_CHARACTERS_ERROR)
                ->addViolation();
            return;
        }
        // Missing hyphen
        if ($i === $h) {
            $this->context
                ->buildViolation($constraint->message)
                ->setParameter('{{ value }}', $this->formatValue($value))
                ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
                ->addViolation();
            return;
        }
    }
    // Check length again
    if (isset($value[$i])) {
        $this->context
            ->buildViolation($constraint->message)
            ->setParameter('{{ value }}', $this->formatValue($value))
            ->setCode(Uuid::TOO_LONG_ERROR)
            ->addViolation();
    }
    // Check version
    if (!\in_array($value[self::STRICT_VERSION_POSITION], $constraint->versions)) {
        $code = Uuid::TIME_BASED_VERSIONS === $constraint->versions ? Uuid::INVALID_TIME_BASED_VERSION_ERROR : Uuid::INVALID_VERSION_ERROR;
        $this->context
            ->buildViolation($constraint->message)
            ->setParameter('{{ value }}', $this->formatValue($value))
            ->setCode($code)
            ->addViolation();
    }
    // Check variant - first two bits must equal "10"
    //   0b10xx
    // & 0b1100 (12)
    // = 0b1000 (8)
    if (8 !== (hexdec($value[self::STRICT_VARIANT_POSITION]) & 12)) {
        $this->context
            ->buildViolation($constraint->message)
            ->setParameter('{{ value }}', $this->formatValue($value))
            ->setCode(Uuid::INVALID_VARIANT_ERROR)
            ->addViolation();
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal