Regex.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  16. *
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  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. * Pattern is also ignored if match=false since the pattern should
  80. * then be reversed before application.
  81. *
  82. * @see http://dev.w3.org/html5/spec/single-page.html#the-pattern-attribute
  83. *
  84. * @return string|null
  85. */
  86. public function getHtmlPattern()
  87. {
  88. // If htmlPattern is specified, use it
  89. if (null !== $this->htmlPattern) {
  90. return empty($this->htmlPattern)
  91. ? null
  92. : $this->htmlPattern;
  93. }
  94. // Quit if delimiters not at very beginning/end (e.g. when options are passed)
  95. if ($this->pattern[0] !== $this->pattern[\strlen($this->pattern) - 1]) {
  96. return null;
  97. }
  98. $delimiter = $this->pattern[0];
  99. // Unescape the delimiter
  100. $pattern = str_replace('\\'.$delimiter, $delimiter, substr($this->pattern, 1, -1));
  101. // If the pattern is inverted, we can wrap it in
  102. // ((?!pattern).)*
  103. if (!$this->match) {
  104. return '((?!'.$pattern.').)*';
  105. }
  106. // If the pattern contains an or statement, wrap the pattern in
  107. // .*(pattern).* and quit. Otherwise we'd need to parse the pattern
  108. if (false !== strpos($pattern, '|')) {
  109. return '.*('.$pattern.').*';
  110. }
  111. // Trim leading ^, otherwise prepend .*
  112. $pattern = '^' === $pattern[0] ? substr($pattern, 1) : '.*'.$pattern;
  113. // Trim trailing $, otherwise append .*
  114. $pattern = '$' === $pattern[\strlen($pattern) - 1] ? substr($pattern, 0, -1) : $pattern.'.*';
  115. return $pattern;
  116. }
  117. }