| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* mbstring-backed implementation of ABJ_404_Solution_RegexHelper. |
| 10 |
* |
| 11 |
* Use when the PHP mbstring extension is loaded. The regex methods wrap |
| 12 |
* mb_ereg / mb_eregi / mb_ereg_replace, which use POSIX regex syntax (no |
| 13 |
* delimiters, no PCRE shorthand like \d). Callers wanting PCRE syntax must |
| 14 |
* use RegexHelperPreg directly. |
| 15 |
*/ |
| 16 |
class ABJ_404_Solution_RegexHelperMb extends ABJ_404_Solution_RegexHelper { |
| 17 |
|
| 18 |
/** @var self|null */ |
| 19 |
private static $instance = null; |
| 20 |
/** |
| 21 |
* Test seam: install or clear the cached singleton instance without |
| 22 |
* private-field reflection. Pass null to reset between tests; pass a |
| 23 |
* configured instance (or double) to install it (M105 singleton-reset seam). |
| 24 |
* |
| 25 |
* @param self|null $instance |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public static function setInstance($instance) { |
| 29 |
self::$instance = $instance; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
public static function getInstance(): self { |
| 34 |
if (self::$instance === null) { |
| 35 |
self::$instance = new self(); |
| 36 |
} |
| 37 |
return self::$instance; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @param array<int, string>|null $regs |
| 42 |
* @return bool|int |
| 43 |
*/ |
| 44 |
public function regexMatch(string $pattern, string $string, ?array &$regs = null) { |
| 45 |
return mb_ereg($pattern, $string, $regs); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @param array<int, string>|null $regs |
| 50 |
* @return bool|int |
| 51 |
*/ |
| 52 |
public function regexMatchi(string $pattern, string $string, ?array &$regs = null) { |
| 53 |
return mb_eregi($pattern, $string, $regs); |
| 54 |
} |
| 55 |
|
| 56 |
/** @return string|null */ |
| 57 |
public function regexReplace($pattern, $replacement, $string) { |
| 58 |
$result = mb_ereg_replace($pattern, $replacement, $string); |
| 59 |
return is_string($result) ? $result : $string; |
| 60 |
} |
| 61 |
} |
| 62 |
|