private function UuidValidator::validateStrict in Zircon Profile 8.0
Same name and namespace in other branches
- 8 vendor/symfony/validator/Constraints/UuidValidator.php \Symfony\Component\Validator\Constraints\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 198
Class
- UuidValidator
- Validates whether the value is a valid UUID per RFC 4122.
Namespace
Symfony\Component\Validator\ConstraintsCode
private function validateStrict($value, Uuid $constraint) {
// 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])) {
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;
}
// Check hyphen placement
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
// ^ ^ ^ ^
if ('-' === $value[$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;
}
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
// ^
if ($h < self::STRICT_LAST_HYPHEN_POSITION) {
$h += 5;
}
continue;
}
// Check characters
if (!ctype_xdigit($value[$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;
}
// Missing hyphen
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;
}
}
// Check length again
if (isset($value[$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();
}
}
// Check version
if (!in_array($value[self::STRICT_VERSION_POSITION], $constraint->versions)) {
if ($this->context instanceof ExecutionContextInterface) {
$this->context
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Uuid::INVALID_VERSION_ERROR)
->addViolation();
}
else {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Uuid::INVALID_VERSION_ERROR)
->addViolation();
}
}
// Check variant - first two bits must equal "10"
// 0b10xx
// & 0b1100 (12)
// = 0b1000 (8)
if ((hexdec($value[self::STRICT_VARIANT_POSITION]) & 12) !== 8) {
if ($this->context instanceof ExecutionContextInterface) {
$this->context
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Uuid::INVALID_VARIANT_ERROR)
->addViolation();
}
else {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Uuid::INVALID_VARIANT_ERROR)
->addViolation();
}
}
}