0 follower

Final Class Yiisoft\Strings\WildcardPattern

InheritanceYiisoft\Strings\WildcardPattern

A wildcard pattern to match strings against.

  • \ escapes other special characters if usage of escape character is not turned off.
  • * matches any string including the empty string except it has a delimiter (/ and \ by default).
  • ** matches any string including the empty string and delimiters.
  • ? matches any single character.
  • [seq] matches any character in seq.
  • [a-z] matches any character from a to z.
  • [!seq] matches any character not in seq.
  • [[:alnum:]] matches POSIX style character classes, see {@see https://www.php.net/manual/en/regexp.reference.character-classes.php}.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Strings\WildcardPattern
ignoreCase() Make pattern case insensitive. Yiisoft\Strings\WildcardPattern
isDynamic() Returns whether the pattern contains a dynamic part i.e. Yiisoft\Strings\WildcardPattern
match() Checks if the passed string would match the given shell wildcard pattern. Yiisoft\Strings\WildcardPattern
quote() Escapes pattern characters in a string. Yiisoft\Strings\WildcardPattern

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string $pattern, string[] $delimiters = ['\\\\''/'] )
$pattern string

The shell wildcard pattern to match against.

$delimiters string[]

Delimiters to consider for "*" (/ and \ by default).

                public function __construct(
    private string $pattern,
    private array $delimiters = ['\\\\', '/'],
) {
}

            
ignoreCase() public method

Make pattern case insensitive.

public self ignoreCase ( boolean $flag true )
$flag boolean

                public function ignoreCase(bool $flag = true): self
{
    $new = clone $this;
    $new->patternPrepared = null;
    $new->ignoreCase = $flag;
    return $new;
}

            
isDynamic() public static method

Returns whether the pattern contains a dynamic part i.e.

has unescaped "*", "{", "?", or "[" character.

public static boolean isDynamic ( string $pattern )
$pattern string

The pattern to check.

return boolean

Whether the pattern contains a dynamic part.

                public static function isDynamic(string $pattern): bool
{
    /** @var string $pattern `$rule` and `$replacement` always correct, so `preg_replace` always returns string */
    $pattern = preg_replace('/\\\\./', '', $pattern);
    return preg_match('/[*{?\[]/', $pattern) === 1;
}

            
match() public method

Checks if the passed string would match the given shell wildcard pattern.

public boolean match ( string $string )
$string string

The tested string.

return boolean

Whether the string matches pattern or not.

                public function match(string $string): bool
{
    if ($this->pattern === '**') {
        return true;
    }
    return preg_match($this->getPatternPrepared(), $string) === 1;
}

            
quote() public static method

Escapes pattern characters in a string.

public static string quote ( string $string )
$string string

Source string.

return string

String with pattern characters escaped.

                public static function quote(string $string): string
{
    /** @var string `$rule` and `$replacement` always correct, so `preg_replace` always returns string */
    return preg_replace('#([\\\\?*\\[\\]])#', '\\\\$1', $string);
}