UuidValidator.php in Plug 7
File
lib/Symfony/validator/Symfony/Component/Validator/Constraints/UuidValidator.php
View source
<?php
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class UuidValidator extends ConstraintValidator {
const STRICT_LENGTH = 36;
const STRICT_FIRST_HYPHEN_POSITION = 8;
const STRICT_LAST_HYPHEN_POSITION = 23;
const STRICT_VERSION_POSITION = 14;
const STRICT_VARIANT_POSITION = 19;
const LOOSE_MAX_LENGTH = 39;
const LOOSE_FIRST_HYPHEN_POSITION = 4;
const STRICT_PATTERN = '/^[a-f0-9]{8}-[a-f0-9]{4}-[%s][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/i';
const LOOSE_PATTERN = '/^[a-f0-9]{4}(?:-?[a-f0-9]{4}){7}$/i';
const STRICT_UUID_LENGTH = self::STRICT_LENGTH;
public function validate($value, Constraint $constraint) {
if (null === $value || '' === $value) {
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
$value = (string) $value;
if ($constraint->strict) {
$this
->validateStrict($value, $constraint);
return;
}
$this
->validateLoose($value, $constraint);
}
private function validateLoose($value, Uuid $constraint) {
$trimmed = trim($value, '[]{}');
$h = self::LOOSE_FIRST_HYPHEN_POSITION;
$l = self::LOOSE_MAX_LENGTH;
for ($i = 0; $i < $l; ++$i) {
if (!isset($trimmed[$i])) {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Uuid::TOO_SHORT_ERROR)
->addViolation();
return;
}
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;
}
if ($i === $h) {
$h += 4;
--$l;
}
if (!ctype_xdigit($trimmed[$i])) {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Uuid::INVALID_CHARACTERS_ERROR)
->addViolation();
return;
}
}
if (isset($trimmed[$i])) {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Uuid::TOO_LONG_ERROR)
->addViolation();
}
}
private function validateStrict($value, Uuid $constraint) {
$h = self::STRICT_FIRST_HYPHEN_POSITION;
for ($i = 0; $i < self::STRICT_LENGTH; ++$i) {
if (!isset($value[$i])) {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Uuid::TOO_SHORT_ERROR)
->addViolation();
return;
}
if ('-' === $value[$i]) {
if ($i !== $h) {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
->addViolation();
return;
}
if ($h < self::STRICT_LAST_HYPHEN_POSITION) {
$h += 5;
}
continue;
}
if (!ctype_xdigit($value[$i])) {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Uuid::INVALID_CHARACTERS_ERROR)
->addViolation();
return;
}
if ($i === $h) {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
->addViolation();
return;
}
}
if (isset($value[$i])) {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Uuid::TOO_LONG_ERROR)
->addViolation();
}
if (!in_array($value[self::STRICT_VERSION_POSITION], $constraint->versions)) {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Uuid::INVALID_VERSION_ERROR)
->addViolation();
}
if ((hexdec($value[self::STRICT_VARIANT_POSITION]) & 12) !== 8) {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Uuid::INVALID_VARIANT_ERROR)
->addViolation();
}
}
}
Classes
Name |
Description |
UuidValidator |
Validates whether the value is a valid UUID per RFC 4122. |