Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Regex.php

class Regex

Same name in this branch
  1. 11.1.x vendor/composer/pcre/src/Regex.php \Composer\Pcre\Regex
  2. 11.1.x vendor/symfony/polyfill-intl-idn/Resources/unidata/Regex.php \Symfony\Polyfill\Intl\Idn\Resources\unidata\Regex

Validates that a value matches a regular expression.

@author Bernhard Schussek <bschussek@gmail.com>

Hierarchy

  • class \Symfony\Component\Validator\Constraint
    • class \Symfony\Component\Validator\Constraints\Regex extends \Symfony\Component\Validator\Constraint

Expanded class hierarchy of Regex

2 files declare their use of Regex
Recipe.php in core/lib/Drupal/Core/Recipe/Recipe.php
RegexConstraint.php in core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/RegexConstraint.php
3 string references to 'Regex'
FormatConstraint::check in vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/FormatConstraint.php
invokes the validation of an element
NumericFilter::operators in core/modules/views/src/Plugin/views/filter/NumericFilter.php
Returns an array of operator information, keyed by operator ID.
StringFilter::operators in core/modules/views/src/Plugin/views/filter/StringFilter.php
Returns an array of operator information, keyed by operator ID.

File

vendor/symfony/validator/Constraints/Regex.php, line 22

Namespace

Symfony\Component\Validator\Constraints
View source
class Regex extends Constraint {
    public const REGEX_FAILED_ERROR = 'de1e3db3-5ed4-4941-aae4-59f3667cc3a3';
    protected const ERROR_NAMES = [
        self::REGEX_FAILED_ERROR => 'REGEX_FAILED_ERROR',
    ];
    public string $message = 'This value is not valid.';
    public ?string $pattern = null;
    public ?string $htmlPattern = null;
    public bool $match = true;
    
    /** @var callable|null */
    public $normalizer;
    
    /**
     * @param string|array<string,mixed>|null $pattern     The regular expression to match
     * @param string|null                     $htmlPattern The pattern to use in the HTML5 pattern attribute
     * @param bool|null                       $match       Whether to validate the value matches the configured pattern or not (defaults to false)
     * @param string[]|null                   $groups
     * @param array<string,mixed>             $options
     */
    public function __construct(string|array|null $pattern, ?string $message = null, ?string $htmlPattern = null, ?bool $match = null, ?callable $normalizer = null, ?array $groups = null, mixed $payload = null, array $options = []) {
        if (\is_array($pattern)) {
            $options = array_merge($pattern, $options);
        }
        elseif (null !== $pattern) {
            $options['value'] = $pattern;
        }
        parent::__construct($options, $groups, $payload);
        $this->message = $message ?? $this->message;
        $this->htmlPattern = $htmlPattern ?? $this->htmlPattern;
        $this->match = $match ?? $this->match;
        $this->normalizer = $normalizer ?? $this->normalizer;
        if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
            throw new InvalidArgumentException(\sprintf('The "normalizer" option must be a valid callable ("%s" given).', get_debug_type($this->normalizer)));
        }
    }
    public function getDefaultOption() : ?string {
        return 'pattern';
    }
    public function getRequiredOptions() : array {
        return [
            'pattern',
        ];
    }
    
    /**
     * Converts the htmlPattern to a suitable format for HTML5 pattern.
     * Example: /^[a-z]+$/ would be converted to [a-z]+
     * However, if options are specified, it cannot be converted.
     *
     * @see http://dev.w3.org/html5/spec/single-page.html#the-pattern-attribute
     */
    public function getHtmlPattern() : ?string {
        // If htmlPattern is specified, use it
        if (null !== $this->htmlPattern) {
            return $this->htmlPattern ?: null;
        }
        // Quit if delimiters not at very beginning/end (e.g. when options are passed)
        if ($this->pattern[0] !== $this->pattern[\strlen($this->pattern) - 1]) {
            return null;
        }
        $delimiter = $this->pattern[0];
        // Unescape the delimiter
        $pattern = str_replace('\\' . $delimiter, $delimiter, substr($this->pattern, 1, -1));
        // If the pattern is inverted, we can wrap it in
        // ((?!pattern).)*
        if (!$this->match) {
            return '((?!' . $pattern . ').)*';
        }
        // If the pattern contains an or statement, wrap the pattern in
        // .*(pattern).* and quit. Otherwise we'd need to parse the pattern
        if (str_contains($pattern, '|')) {
            return '.*(' . $pattern . ').*';
        }
        // Trim leading ^, otherwise prepend .*
        $pattern = '^' === $pattern[0] ? substr($pattern, 1) : '.*' . $pattern;
        // Trim trailing $, otherwise append .*
        return '$' === $pattern[\strlen($pattern) - 1] ? substr($pattern, 0, -1) : $pattern . '.*';
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Constraint::$groups public property The groups that the constraint belongs to.
Constraint::$payload public property Domain-specific data attached to a constraint.
Constraint::addImplicitGroupName public function Adds the given group if this constraint is in the Default group. 2
Constraint::CLASS_CONSTRAINT public constant Marks a constraint that can be put onto classes.
Constraint::DEFAULT_GROUP public constant The name of the group given to all constraints with no explicit group.
Constraint::getErrorName public static function Returns the name of the given error code.
Constraint::getTargets public function Returns whether the constraint can be put onto classes, properties or
both.
8
Constraint::normalizeOptions protected function
Constraint::PROPERTY_CONSTRAINT public constant Marks a constraint that can be put onto properties.
Constraint::validatedBy public function Returns the name of the class that validates this constraint. 9
Constraint::__get public function Returns the value of a lazily initialized option. 2
Constraint::__isset public function 1
Constraint::__set public function Sets the value of a lazily initialized option. 1
Constraint::__sleep public function Optimizes the serialized value to minimize storage space.
Regex::$htmlPattern public property
Regex::$match public property
Regex::$message public property
Regex::$normalizer public property @var callable|null
Regex::$pattern public property
Regex::ERROR_NAMES protected constant Maps error codes to the names of their constants. Overrides Constraint::ERROR_NAMES
Regex::getDefaultOption public function Returns the name of the default option. Overrides Constraint::getDefaultOption
Regex::getHtmlPattern public function Converts the htmlPattern to a suitable format for HTML5 pattern.
Example: /^[a-z]+$/ would be converted to [a-z]+
However, if options are specified, it cannot be converted.
Regex::getRequiredOptions public function Returns the name of the required options. Overrides Constraint::getRequiredOptions
Regex::REGEX_FAILED_ERROR public constant
Regex::__construct public function Overrides Constraint::__construct 1

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal