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 <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.     public function __construct(
  32.         string|array|null $pattern,
  33.         string $message null,
  34.         string $htmlPattern null,
  35.         bool $match null,
  36.         callable $normalizer null,
  37.         array $groups null,
  38.         mixed $payload null,
  39.         array $options = []
  40.     ) {
  41.         if (\is_array($pattern)) {
  42.             $options array_merge($pattern$options);
  43.         } elseif (null !== $pattern) {
  44.             $options['value'] = $pattern;
  45.         }
  46.         parent::__construct($options$groups$payload);
  47.         $this->message $message ?? $this->message;
  48.         $this->htmlPattern $htmlPattern ?? $this->htmlPattern;
  49.         $this->match $match ?? $this->match;
  50.         $this->normalizer $normalizer ?? $this->normalizer;
  51.         if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
  52.             throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).'get_debug_type($this->normalizer)));
  53.         }
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      */
  58.     public function getDefaultOption(): ?string
  59.     {
  60.         return 'pattern';
  61.     }
  62.     /**
  63.      * {@inheritdoc}
  64.      */
  65.     public function getRequiredOptions(): array
  66.     {
  67.         return ['pattern'];
  68.     }
  69.     /**
  70.      * Converts the htmlPattern to a suitable format for HTML5 pattern.
  71.      * Example: /^[a-z]+$/ would be converted to [a-z]+
  72.      * However, if options are specified, it cannot be converted.
  73.      *
  74.      * @see http://dev.w3.org/html5/spec/single-page.html#the-pattern-attribute
  75.      */
  76.     public function getHtmlPattern(): ?string
  77.     {
  78.         // If htmlPattern is specified, use it
  79.         if (null !== $this->htmlPattern) {
  80.             return empty($this->htmlPattern)
  81.                 ? null
  82.                 $this->htmlPattern;
  83.         }
  84.         // Quit if delimiters not at very beginning/end (e.g. when options are passed)
  85.         if ($this->pattern[0] !== $this->pattern[\strlen($this->pattern) - 1]) {
  86.             return null;
  87.         }
  88.         $delimiter $this->pattern[0];
  89.         // Unescape the delimiter
  90.         $pattern str_replace('\\'.$delimiter$delimitersubstr($this->pattern1, -1));
  91.         // If the pattern is inverted, we can wrap it in
  92.         // ((?!pattern).)*
  93.         if (!$this->match) {
  94.             return '((?!'.$pattern.').)*';
  95.         }
  96.         // If the pattern contains an or statement, wrap the pattern in
  97.         // .*(pattern).* and quit. Otherwise we'd need to parse the pattern
  98.         if (str_contains($pattern'|')) {
  99.             return '.*('.$pattern.').*';
  100.         }
  101.         // Trim leading ^, otherwise prepend .*
  102.         $pattern '^' === $pattern[0] ? substr($pattern1) : '.*'.$pattern;
  103.         // Trim trailing $, otherwise append .*
  104.         $pattern '$' === $pattern[\strlen($pattern) - 1] ? substr($pattern0, -1) : $pattern.'.*';
  105.         return $pattern;
  106.     }
  107. }