| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Internal; |
| 4 |
|
| 5 |
class WildcardMatch |
| 6 |
{ |
| 7 |
private const SEARCH_SET = [ |
| 8 |
'~~', // convert double tilde to unprintable value |
| 9 |
'~\*', // convert tilde backslash asterisk to [*] (matches literal asterisk in regexp) |
| 10 |
'\*', // convert backslash asterisk to .* (matches string of any length in regexp) |
| 11 |
'~\?', // convert tilde backslash question to [?] (matches literal question mark in regexp) |
| 12 |
'\?', // convert backslash question to . (matches one character in regexp) |
| 13 |
"\x1c", // convert original double tilde to single tilde |
| 14 |
]; |
| 15 |
|
| 16 |
private const REPLACEMENT_SET = [ |
| 17 |
"\x1c", |
| 18 |
'[*]', |
| 19 |
'.*', |
| 20 |
'[?]', |
| 21 |
'.', |
| 22 |
'~', |
| 23 |
]; |
| 24 |
|
| 25 |
public static function wildcard(string $wildcard): string |
| 26 |
{ |
| 27 |
// Preg Escape the wildcard, but protecting the Excel * and ? search characters |
| 28 |
return str_replace(self::SEARCH_SET, self::REPLACEMENT_SET, preg_quote($wildcard, '/')); |
| 29 |
} |
| 30 |
|
| 31 |
public static function compare(?string $value, string $wildcard): bool |
| 32 |
{ |
| 33 |
if ($value === '' || $value === null) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
return (bool) preg_match("/^{$wildcard}\$/mui", $value); |
| 38 |
} |
| 39 |
} |
| 40 |
|