private function UuidValidator::validateLoose in Plug 7
1 call to UuidValidator::validateLoose()
- UuidValidator::validate in lib/
Symfony/ validator/ Symfony/ Component/ Validator/ Constraints/ UuidValidator.php - Checks if the passed value is valid.
File
- lib/
Symfony/ validator/ Symfony/ Component/ Validator/ Constraints/ UuidValidator.php, line 97
Class
- UuidValidator
- Validates whether the value is a valid UUID per RFC 4122.
Namespace
Symfony\Component\Validator\ConstraintsCode
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])) {
$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) {
$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])) {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Uuid::INVALID_CHARACTERS_ERROR)
->addViolation();
return;
}
}
// Check length again
if (isset($trimmed[$i])) {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Uuid::TOO_LONG_ERROR)
->addViolation();
}
}