Skip to content

Commit 539896f

Browse files
committed
Add a mandatory escape char argument
1 parent b6d8bf8 commit 539896f

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

lib/Doctrine/DBAL/Platforms/AbstractPlatform.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3582,21 +3582,29 @@ public function getStringLiteralQuoteCharacter()
35823582
/**
35833583
* Escapes metacharacters in a string intended to be used with a LIKE
35843584
* operator.
3585+
*
3586+
* @param string $inputString a literal, unquoted string
3587+
* @param string $escapeChar should be exactly one character long and
3588+
* should be reused by the caller in the LIKE
3589+
* expression.
35853590
*/
3586-
final public function escapeStringForLike(string $inputString): string
3591+
final public function escapeStringForLike(string $inputString, string $escapeChar): string
35873592
{
3588-
$escapeChar = $this->getLikeEscapeChar();
3593+
/* Some RDBMS' can deal with multibyte characters, but let us assume
3594+
* that kind of will not actually be needed */
3595+
$escapeCharLength = \strlen($escapeChar);
3596+
if ($escapeCharLength !== 1) {
3597+
throw new \InvalidArgumentException(sprintf(
3598+
'The escape character should be exactly that, got "%s"',
3599+
$escapeCharLength
3600+
));
3601+
}
35893602
$replacePairs = [$escapeChar => $escapeChar.$escapeChar];
35903603
foreach ($this->getLikeWildcardCharacters() as $metaChar) {
35913604
$replacePairs[$metaCharacter] = $escapeChar.$metaChar;
35923605
}
35933606

3594-
return strtr($inputString, $replacePairs);
3595-
}
3596-
3597-
protected function getLikeEscapeChar(): string
3598-
{
3599-
return '\\';
3607+
return \strtr($inputString, $replacePairs);
36003608
}
36013609

36023610
protected function getLikeWildcardCharacters(): iterable

tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,4 +1468,10 @@ public function getGeneratesFloatDeclarationSQL()
14681468
array(array('precision' => 8, 'scale' => 2), 'DOUBLE PRECISION'),
14691469
);
14701470
}
1471+
1472+
public function testItThrowsOnMultibyteEscapeChar()
1473+
{
1474+
$this->expectException(\InvalidArgumentException::class);
1475+
$this->_platform->escapeStringForLike('random string', '💩');
1476+
}
14711477
}

0 commit comments

Comments
 (0)