Uuid.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\Exception\InvalidArgumentException;
  13. /**
  14. * @Annotation
  15. *
  16. * @author Colin O'Dell <colinodell@gmail.com>
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. */
  19. #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
  20. class Uuid extends Constraint
  21. {
  22. public const TOO_SHORT_ERROR = 'aa314679-dac9-4f54-bf97-b2049df8f2a3';
  23. public const TOO_LONG_ERROR = '494897dd-36f8-4d31-8923-71a8d5f3000d';
  24. public const INVALID_CHARACTERS_ERROR = '51120b12-a2bc-41bf-aa53-cd73daf330d0';
  25. public const INVALID_HYPHEN_PLACEMENT_ERROR = '98469c83-0309-4f5d-bf95-a496dcaa869c';
  26. public const INVALID_VERSION_ERROR = '21ba13b4-b185-4882-ac6f-d147355987eb';
  27. public const INVALID_VARIANT_ERROR = '164ef693-2b9d-46de-ad7f-836201f0c2db';
  28. protected static $errorNames = [
  29. self::TOO_SHORT_ERROR => 'TOO_SHORT_ERROR',
  30. self::TOO_LONG_ERROR => 'TOO_LONG_ERROR',
  31. self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
  32. self::INVALID_HYPHEN_PLACEMENT_ERROR => 'INVALID_HYPHEN_PLACEMENT_ERROR',
  33. self::INVALID_VERSION_ERROR => 'INVALID_VERSION_ERROR',
  34. self::INVALID_VARIANT_ERROR => 'INVALID_VARIANT_ERROR',
  35. ];
  36. // Possible versions defined by RFC 4122
  37. public const V1_MAC = 1;
  38. public const V2_DCE = 2;
  39. public const V3_MD5 = 3;
  40. public const V4_RANDOM = 4;
  41. public const V5_SHA1 = 5;
  42. public const V6_SORTABLE = 6;
  43. public const ALL_VERSIONS = [
  44. self::V1_MAC,
  45. self::V2_DCE,
  46. self::V3_MD5,
  47. self::V4_RANDOM,
  48. self::V5_SHA1,
  49. self::V6_SORTABLE,
  50. ];
  51. /**
  52. * Message to display when validation fails.
  53. *
  54. * @var string
  55. */
  56. public $message = 'This is not a valid UUID.';
  57. /**
  58. * Strict mode only allows UUIDs that meet the formal definition and formatting per RFC 4122.
  59. *
  60. * Set this to `false` to allow legacy formats with different dash positioning or wrapping characters
  61. *
  62. * @var bool
  63. */
  64. public $strict = true;
  65. /**
  66. * Array of allowed versions (see version constants above).
  67. *
  68. * All UUID versions are allowed by default
  69. *
  70. * @var int[]
  71. */
  72. public $versions = self::ALL_VERSIONS;
  73. public $normalizer;
  74. /**
  75. * {@inheritdoc}
  76. *
  77. * @param int[]|null $versions
  78. */
  79. public function __construct(
  80. array $options = null,
  81. string $message = null,
  82. array $versions = null,
  83. bool $strict = null,
  84. callable $normalizer = null,
  85. array $groups = null,
  86. $payload = null
  87. ) {
  88. parent::__construct($options, $groups, $payload);
  89. $this->message = $message ?? $this->message;
  90. $this->versions = $versions ?? $this->versions;
  91. $this->strict = $strict ?? $this->strict;
  92. $this->normalizer = $normalizer ?? $this->normalizer;
  93. if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
  94. throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', get_debug_type($this->normalizer)));
  95. }
  96. }
  97. }