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

Breadcrumb

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

function UuidValidator::validateLoose

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

File

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

Class

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

Namespace

Symfony\Component\Validator\Constraints

Code

private function validateLoose(string $value, Uuid $constraint) : void {
    // Error priority:
    // 1. ERROR_INVALID_CHARACTERS
    // 2. ERROR_INVALID_HYPHEN_PLACEMENT
    // 3. ERROR_TOO_SHORT/ERROR_TOO_LONG
    // Trim any wrapping characters like [] or {} used by some legacy systems
    $trimmed = trim($value, '[]{}');
    // Position of the next expected hyphen
    $h = self::LOOSE_FIRST_HYPHEN_POSITION;
    // Expected length
    $l = self::LOOSE_MAX_LENGTH;
    for ($i = 0; $i < $l; ++$i) {
        // Check length
        if (!isset($trimmed[$i])) {
            $this->context
                ->buildViolation($constraint->message)
                ->setParameter('{{ value }}', $this->formatValue($value))
                ->setCode(Uuid::TOO_SHORT_ERROR)
                ->addViolation();
            return;
        }
        // Hyphens must occur every fifth position
        // xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx
        //     ^    ^    ^    ^    ^    ^    ^
        if ('-' === $trimmed[$i]) {
            if ($i !== $h) {
                $this->context
                    ->buildViolation($constraint->message)
                    ->setParameter('{{ value }}', $this->formatValue($value))
                    ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
                    ->addViolation();
                return;
            }
            $h += 5;
            continue;
        }
        // Missing hyphens are ignored
        if ($i === $h) {
            $h += 4;
            --$l;
        }
        // Check characters
        if (!ctype_xdigit($trimmed[$i])) {
            $this->context
                ->buildViolation($constraint->message)
                ->setParameter('{{ value }}', $this->formatValue($value))
                ->setCode(Uuid::INVALID_CHARACTERS_ERROR)
                ->addViolation();
            return;
        }
    }
    // Check length again
    if (isset($trimmed[$i])) {
        $this->context
            ->buildViolation($constraint->message)
            ->setParameter('{{ value }}', $this->formatValue($value))
            ->setCode(Uuid::TOO_LONG_ERROR)
            ->addViolation();
    }
}

API Navigation

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