19 protected $allowedCharacters;
24 private const VALID_CHARS = [
'\t',
'\n',
'\\\'',
'\"',
'\f',
'\r',
'\a',
'\b',
'\\\\' ];
26 public function __construct( array $params ) {
27 $this->allowedCharacters = $params[
'values'] ?? [];
29 if ( $this->allowedCharacters === [] || !is_array( $this->allowedCharacters ) ) {
30 throw new InvalidArgumentException(
31 'No values provided for EscapeCharacter validator.'
35 $this->regex = $this->buildRegex( $this->allowedCharacters );
41 preg_match_all(
"/$this->regex/U", $translation, $transVars );
44 $params = $transVars[0];
45 if ( count( $params ) ) {
47 [
'PARAMS', $params ],
48 [
'COUNT', count( $params ) ],
49 [
'PARAMS', $this->allowedCharacters ],
50 [
'COUNT', count( $this->allowedCharacters ) ]
55 'escape',
'invalid',
'translate-checks-escape', $messageParams
57 $issues->add( $issue );
63 private function buildRegex( array $allowedCharacters ):
string {
66 foreach ( $allowedCharacters as $character ) {
67 if ( !in_array( $character, self::VALID_CHARS ) ) {
68 throw new InvalidArgumentException(
69 "Invalid escape character encountered: $character during configuration." .
70 'Valid escape characters include: ' . implode(
', ', self::VALID_CHARS )
74 if ( $character !==
'\\' ) {
75 $character = stripslashes( $character );
77 $prefix =
'(?<!\\\\)';
82 $normalizedChar = addslashes( $character );
83 $regex .= $normalizedChar;
87 return $prefix . $regex;