vendor/symfony/validator/Constraints/Regex.php line 24

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <[email protected]>
  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. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  16. *
  17. * @author Bernhard Schussek <[email protected]>
  18. */
  19. #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
  20. class Regex extends Constraint
  21. {
  22. public const REGEX_FAILED_ERROR = 'de1e3db3-5ed4-4941-aae4-59f3667cc3a3';
  23. protected static $errorNames = [
  24. self::REGEX_FAILED_ERROR => 'REGEX_FAILED_ERROR',
  25. ];
  26. public $message = 'This value is not valid.';
  27. public $pattern;
  28. public $htmlPattern;
  29. public $match = true;
  30. public $normalizer;
  31. /**
  32. * {@inheritdoc}
  33. *
  34. * @param string|array $pattern The pattern to evaluate or an array of options
  35. */
  36. public function __construct(
  37. $pattern,
  38. ?string $message = null,
  39. ?string $htmlPattern = null,
  40. ?bool $match = null,
  41. ?callable $normalizer = null,
  42. ?array $groups = null,
  43. $payload = null,
  44. array $options = []
  45. ) {
  46. if (\is_array($pattern)) {
  47. $options = array_merge($pattern, $options);
  48. } elseif (null !== $pattern) {
  49. $options['value'] = $pattern;
  50. }
  51. parent::__construct($options, $groups, $payload);
  52. $this->message = $message ?? $this->message;
  53. $this->htmlPattern = $htmlPattern ?? $this->htmlPattern;
  54. $this->match = $match ?? $this->match;
  55. $this->normalizer = $normalizer ?? $this->normalizer;
  56. if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
  57. throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', get_debug_type($this->normalizer)));
  58. }
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function getDefaultOption()
  64. {
  65. return 'pattern';
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getRequiredOptions()
  71. {
  72. return ['pattern'];
  73. }
  74. /**
  75. * Converts the htmlPattern to a suitable format for HTML5 pattern.
  76. * Example: /^[a-z]+$/ would be converted to [a-z]+
  77. * However, if options are specified, it cannot be converted.
  78. *
  79. * @see http://dev.w3.org/html5/spec/single-page.html#the-pattern-attribute
  80. *
  81. * @return string|null
  82. */
  83. public function getHtmlPattern()
  84. {
  85. // If htmlPattern is specified, use it
  86. if (null !== $this->htmlPattern) {
  87. return empty($this->htmlPattern)
  88. ? null
  89. : $this->htmlPattern;
  90. }
  91. // Quit if delimiters not at very beginning/end (e.g. when options are passed)
  92. if ($this->pattern[0] !== $this->pattern[\strlen($this->pattern) - 1]) {
  93. return null;
  94. }
  95. $delimiter = $this->pattern[0];
  96. // Unescape the delimiter
  97. $pattern = str_replace('\\'.$delimiter, $delimiter, substr($this->pattern, 1, -1));
  98. // If the pattern is inverted, we can wrap it in
  99. // ((?!pattern).)*
  100. if (!$this->match) {
  101. return '((?!'.$pattern.').)*';
  102. }
  103. // If the pattern contains an or statement, wrap the pattern in
  104. // .*(pattern).* and quit. Otherwise we'd need to parse the pattern
  105. if (str_contains($pattern, '|')) {
  106. return '.*('.$pattern.').*';
  107. }
  108. // Trim leading ^, otherwise prepend .*
  109. $pattern = '^' === $pattern[0] ? substr($pattern, 1) : '.*'.$pattern;
  110. // Trim trailing $, otherwise append .*
  111. $pattern = '$' === $pattern[\strlen($pattern) - 1] ? substr($pattern, 0, -1) : $pattern.'.*';
  112. return $pattern;
  113. }
  114. }