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

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. class Regex extends Constraint
  20. {
  21.     public const REGEX_FAILED_ERROR 'de1e3db3-5ed4-4941-aae4-59f3667cc3a3';
  22.     protected static $errorNames = [
  23.         self::REGEX_FAILED_ERROR => 'REGEX_FAILED_ERROR',
  24.     ];
  25.     public $message 'This value is not valid.';
  26.     public $pattern;
  27.     public $htmlPattern;
  28.     public $match true;
  29.     public $normalizer;
  30.     public function __construct($options null)
  31.     {
  32.         parent::__construct($options);
  33.         if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
  34.             throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', \is_object($this->normalizer) ? \get_class($this->normalizer) : \gettype($this->normalizer)));
  35.         }
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public function getDefaultOption()
  41.     {
  42.         return 'pattern';
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function getRequiredOptions()
  48.     {
  49.         return ['pattern'];
  50.     }
  51.     /**
  52.      * Converts the htmlPattern to a suitable format for HTML5 pattern.
  53.      * Example: /^[a-z]+$/ would be converted to [a-z]+
  54.      * However, if options are specified, it cannot be converted.
  55.      *
  56.      * @see http://dev.w3.org/html5/spec/single-page.html#the-pattern-attribute
  57.      *
  58.      * @return string|null
  59.      */
  60.     public function getHtmlPattern()
  61.     {
  62.         // If htmlPattern is specified, use it
  63.         if (null !== $this->htmlPattern) {
  64.             return empty($this->htmlPattern)
  65.                 ? null
  66.                 $this->htmlPattern;
  67.         }
  68.         // Quit if delimiters not at very beginning/end (e.g. when options are passed)
  69.         if ($this->pattern[0] !== $this->pattern[\strlen($this->pattern) - 1]) {
  70.             return null;
  71.         }
  72.         $delimiter $this->pattern[0];
  73.         // Unescape the delimiter
  74.         $pattern str_replace('\\'.$delimiter$delimitersubstr($this->pattern1, -1));
  75.         // If the pattern is inverted, we can wrap it in
  76.         // ((?!pattern).)*
  77.         if (!$this->match) {
  78.             return '((?!'.$pattern.').)*';
  79.         }
  80.         // If the pattern contains an or statement, wrap the pattern in
  81.         // .*(pattern).* and quit. Otherwise we'd need to parse the pattern
  82.         if (str_contains($pattern'|')) {
  83.             return '.*('.$pattern.').*';
  84.         }
  85.         // Trim leading ^, otherwise prepend .*
  86.         $pattern '^' === $pattern[0] ? substr($pattern1) : '.*'.$pattern;
  87.         // Trim trailing $, otherwise append .*
  88.         $pattern '$' === $pattern[\strlen($pattern) - 1] ? substr($pattern0, -1) : $pattern.'.*';
  89.         return $pattern;
  90.     }
  91. }