You are here

private function UuidValidator::validateLoose in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Constraints/UuidValidator.php \Symfony\Component\Validator\Constraints\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 99

Class

UuidValidator
Validates whether the value is a valid UUID per RFC 4122.

Namespace

Symfony\Component\Validator\Constraints

Code

private function validateLoose($value, Uuid $constraint) {

  // 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])) {
      if ($this->context instanceof ExecutionContextInterface) {
        $this->context
          ->buildViolation($constraint->message)
          ->setParameter('{{ value }}', $this
          ->formatValue($value))
          ->setCode(Uuid::TOO_SHORT_ERROR)
          ->addViolation();
      }
      else {
        $this
          ->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) {
        if ($this->context instanceof ExecutionContextInterface) {
          $this->context
            ->buildViolation($constraint->message)
            ->setParameter('{{ value }}', $this
            ->formatValue($value))
            ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
            ->addViolation();
        }
        else {
          $this
            ->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])) {
      if ($this->context instanceof ExecutionContextInterface) {
        $this->context
          ->buildViolation($constraint->message)
          ->setParameter('{{ value }}', $this
          ->formatValue($value))
          ->setCode(Uuid::INVALID_CHARACTERS_ERROR)
          ->addViolation();
      }
      else {
        $this
          ->buildViolation($constraint->message)
          ->setParameter('{{ value }}', $this
          ->formatValue($value))
          ->setCode(Uuid::INVALID_CHARACTERS_ERROR)
          ->addViolation();
      }
      return;
    }
  }

  // Check length again
  if (isset($trimmed[$i])) {
    if ($this->context instanceof ExecutionContextInterface) {
      $this->context
        ->buildViolation($constraint->message)
        ->setParameter('{{ value }}', $this
        ->formatValue($value))
        ->setCode(Uuid::TOO_LONG_ERROR)
        ->addViolation();
    }
    else {
      $this
        ->buildViolation($constraint->message)
        ->setParameter('{{ value }}', $this
        ->formatValue($value))
        ->setCode(Uuid::TOO_LONG_ERROR)
        ->addViolation();
    }
  }
}