Skip to content

Commit 758a6c7

Browse files
Ocramiusmorozov
authored andcommitted
Merge pull request #3491 from morozov/get-trim-expr
Changed the type of $char in AbstractPlatform::getTrimExpression()`from string|false to ?string
2 parents 37cce80 + 6eef1d4 commit 758a6c7

File tree

7 files changed

+89
-45
lines changed

7 files changed

+89
-45
lines changed

UPGRADE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Upgrade to 3.0
22

3+
## BC BREAK The type of `$char` in `AbstractPlatform::getTrimExpression()` changed from `string|false` to `?string`
4+
5+
The default value of `$char` is now `null`, not `false`. Additionally, the method will throw an `InvalidArgumentException` in an invalid value of `$mode` is passed.
6+
37
## BC BREAK `Statement::quote()` only accepts strings.
48

59
`Statement::quote()` and `ExpressionBuilder::literal()` no longer accept arguments of an arbitrary type and and don't implement type-specific handling. Only strings can be quoted.

lib/Doctrine/DBAL/Platforms/AbstractPlatform.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -753,37 +753,50 @@ public function getModExpression($expression1, $expression2)
753753
*
754754
* @param string $str The expression to apply the trim to.
755755
* @param int $mode The position of the trim (leading/trailing/both).
756-
* @param string|bool $char The char to trim, has to be quoted already. Defaults to space.
756+
* @param string|null $char The char to trim, has to be quoted already. Defaults to space.
757757
*
758-
* @return string
758+
* @throws InvalidArgumentException
759759
*/
760-
public function getTrimExpression($str, $mode = TrimMode::UNSPECIFIED, $char = false)
760+
public function getTrimExpression(string $str, int $mode = TrimMode::UNSPECIFIED, ?string $char = null) : string
761761
{
762-
$expression = '';
762+
$tokens = [];
763763

764764
switch ($mode) {
765+
case TrimMode::UNSPECIFIED:
766+
break;
767+
765768
case TrimMode::LEADING:
766-
$expression = 'LEADING ';
769+
$tokens[] = 'LEADING';
767770
break;
768771

769772
case TrimMode::TRAILING:
770-
$expression = 'TRAILING ';
773+
$tokens[] = 'TRAILING';
771774
break;
772775

773776
case TrimMode::BOTH:
774-
$expression = 'BOTH ';
777+
$tokens[] = 'BOTH';
775778
break;
779+
780+
default:
781+
throw new InvalidArgumentException(
782+
sprintf(
783+
'The value of $mode is expected to be one of the TrimMode constants, %d given',
784+
$mode
785+
)
786+
);
776787
}
777788

778-
if ($char !== false) {
779-
$expression .= $char . ' ';
789+
if ($char !== null) {
790+
$tokens[] = $char;
780791
}
781792

782-
if ($mode || $char !== false) {
783-
$expression .= 'FROM ';
793+
if (count($tokens) > 0) {
794+
$tokens[] = 'FROM';
784795
}
785796

786-
return 'TRIM(' . $expression . $str . ')';
797+
$tokens[] = $str;
798+
799+
return sprintf('TRIM(%s)', implode(' ', $tokens));
787800
}
788801

789802
/**

lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use function func_get_args;
2525
use function get_class;
2626
use function implode;
27+
use function in_array;
2728
use function is_string;
2829
use function preg_match;
2930
use function sprintf;
@@ -1124,10 +1125,16 @@ public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
11241125
/**
11251126
* {@inheritdoc}
11261127
*/
1127-
public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
1128+
public function getTrimExpression(string $str, int $mode = TrimMode::UNSPECIFIED, ?string $char = null) : string
11281129
{
1129-
if (! $char) {
1130-
switch ($pos) {
1130+
if (! in_array($mode, [TrimMode::UNSPECIFIED, TrimMode::LEADING, TrimMode::TRAILING, TrimMode::BOTH], true)) {
1131+
throw new InvalidArgumentException(
1132+
sprintf('The value of $mode is expected to be one of the TrimMode constants, %d given', $mode)
1133+
);
1134+
}
1135+
1136+
if ($char === null) {
1137+
switch ($mode) {
11311138
case TrimMode::LEADING:
11321139
return $this->getLtrimExpression($str);
11331140
case TrimMode::TRAILING:
@@ -1139,7 +1146,7 @@ public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = fa
11391146

11401147
$pattern = "'%[^' + " . $char . " + ']%'";
11411148

1142-
switch ($pos) {
1149+
switch ($mode) {
11431150
case TrimMode::LEADING:
11441151
return 'SUBSTR(' . $str . ', PATINDEX(' . $pattern . ', ' . $str . '))';
11451152
case TrimMode::TRAILING:

lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use function explode;
2121
use function func_get_args;
2222
use function implode;
23+
use function in_array;
2324
use function is_array;
2425
use function is_bool;
2526
use function is_numeric;
@@ -1035,23 +1036,25 @@ public function getModExpression($expression1, $expression2)
10351036
/**
10361037
* {@inheritDoc}
10371038
*/
1038-
public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
1039+
public function getTrimExpression(string $str, int $mode = TrimMode::UNSPECIFIED, ?string $char = null) : string
10391040
{
1040-
if (! $char) {
1041-
switch ($pos) {
1041+
if (! in_array($mode, [TrimMode::UNSPECIFIED, TrimMode::LEADING, TrimMode::TRAILING, TrimMode::BOTH], true)) {
1042+
throw new InvalidArgumentException(
1043+
sprintf('The value of $mode is expected to be one of the TrimMode constants, %d given', $mode)
1044+
);
1045+
}
1046+
1047+
if ($char === null) {
1048+
switch ($mode) {
10421049
case TrimMode::LEADING:
1043-
$trimFn = 'LTRIM';
1044-
break;
1050+
return 'LTRIM(' . $str . ')';
10451051

10461052
case TrimMode::TRAILING:
1047-
$trimFn = 'RTRIM';
1048-
break;
1053+
return 'RTRIM(' . $str . ')';
10491054

10501055
default:
10511056
return 'LTRIM(RTRIM(' . $str . '))';
10521057
}
1053-
1054-
return $trimFn . '(' . $str . ')';
10551058
}
10561059

10571060
/** Original query used to get those expressions
@@ -1065,11 +1068,11 @@ public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = fa
10651068
*/
10661069
$pattern = "'%[^' + " . $char . " + ']%'";
10671070

1068-
if ($pos === TrimMode::LEADING) {
1071+
if ($mode === TrimMode::LEADING) {
10691072
return 'stuff(' . $str . ', 1, patindex(' . $pattern . ', ' . $str . ') - 1, null)';
10701073
}
10711074

1072-
if ($pos === TrimMode::TRAILING) {
1075+
if ($mode === TrimMode::TRAILING) {
10731076
return 'reverse(stuff(reverse(' . $str . '), 1, patindex(' . $pattern . ', reverse(' . $str . ')) - 1, null))';
10741077
}
10751078

lib/Doctrine/DBAL/Platforms/SqlitePlatform.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Doctrine\DBAL\Schema\TableDiff;
1313
use Doctrine\DBAL\TransactionIsolationLevel;
1414
use Doctrine\DBAL\Types;
15+
use InvalidArgumentException;
1516
use function array_merge;
1617
use function array_unique;
1718
use function array_values;
@@ -60,11 +61,14 @@ public function getNowExpression($type = 'timestamp')
6061
/**
6162
* {@inheritDoc}
6263
*/
63-
public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
64+
public function getTrimExpression(string $str, int $mode = TrimMode::UNSPECIFIED, ?string $char = null) : string
6465
{
65-
$trimChar = $char !== false ? (', ' . $char) : '';
66+
switch ($mode) {
67+
case TrimMode::UNSPECIFIED:
68+
case TrimMode::BOTH:
69+
$trimFn = 'TRIM';
70+
break;
6671

67-
switch ($pos) {
6872
case TrimMode::LEADING:
6973
$trimFn = 'LTRIM';
7074
break;
@@ -74,10 +78,21 @@ public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = fa
7478
break;
7579

7680
default:
77-
$trimFn = 'TRIM';
81+
throw new InvalidArgumentException(
82+
sprintf(
83+
'The value of $mode is expected to be one of the TrimMode constants, %d given',
84+
$mode
85+
)
86+
);
87+
}
88+
89+
$arguments = [$str];
90+
91+
if ($char !== null) {
92+
$arguments[] = $char;
7893
}
7994

80-
return $trimFn . '(' . $str . $trimChar . ')';
95+
return sprintf('%s(%s)', $trimFn, implode(', ', $arguments));
8196
}
8297

8398
/**

tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Doctrine\DBAL\Statement;
1919
use Doctrine\DBAL\Types\Types;
2020
use Doctrine\Tests\DbalFunctionalTestCase;
21+
use InvalidArgumentException;
2122
use PDO;
2223
use const CASE_LOWER;
2324
use const PHP_EOL;
@@ -530,10 +531,10 @@ public function testTrimExpression(string $value, int $position, $char, string $
530531
public static function getTrimExpressionData() : iterable
531532
{
532533
return [
533-
['test_string', TrimMode::UNSPECIFIED, false, 'foo'],
534-
['test_string', TrimMode::LEADING, false, 'foo'],
535-
['test_string', TrimMode::TRAILING, false, 'foo'],
536-
['test_string', TrimMode::BOTH, false, 'foo'],
534+
['test_string', TrimMode::UNSPECIFIED, null, 'foo'],
535+
['test_string', TrimMode::LEADING, null, 'foo'],
536+
['test_string', TrimMode::TRAILING, null, 'foo'],
537+
['test_string', TrimMode::BOTH, null, 'foo'],
537538
['test_string', TrimMode::UNSPECIFIED, "'f'", 'oo'],
538539
['test_string', TrimMode::UNSPECIFIED, "'o'", 'f'],
539540
['test_string', TrimMode::UNSPECIFIED, "'.'", 'foo'],
@@ -546,10 +547,10 @@ public static function getTrimExpressionData() : iterable
546547
['test_string', TrimMode::BOTH, "'f'", 'oo'],
547548
['test_string', TrimMode::BOTH, "'o'", 'f'],
548549
['test_string', TrimMode::BOTH, "'.'", 'foo'],
549-
["' foo '", TrimMode::UNSPECIFIED, false, 'foo'],
550-
["' foo '", TrimMode::LEADING, false, 'foo '],
551-
["' foo '", TrimMode::TRAILING, false, ' foo'],
552-
["' foo '", TrimMode::BOTH, false, 'foo'],
550+
["' foo '", TrimMode::UNSPECIFIED, null, 'foo'],
551+
["' foo '", TrimMode::LEADING, null, 'foo '],
552+
["' foo '", TrimMode::TRAILING, null, ' foo'],
553+
["' foo '", TrimMode::BOTH, null, 'foo'],
553554
["' foo '", TrimMode::UNSPECIFIED, "'f'", ' foo '],
554555
["' foo '", TrimMode::UNSPECIFIED, "'o'", ' foo '],
555556
["' foo '", TrimMode::UNSPECIFIED, "'.'", ' foo '],
@@ -569,6 +570,12 @@ public static function getTrimExpressionData() : iterable
569570
];
570571
}
571572

573+
public function testTrimExpressionInvalidMode() : void
574+
{
575+
$this->expectException(InvalidArgumentException::class);
576+
$this->connection->getDatabasePlatform()->getTrimExpression('Trim me!', 0xBEEF);
577+
}
578+
572579
/**
573580
* @group DDC-1014
574581
*/

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -697,11 +697,6 @@ public function testGeneratesSQLSnippets() : void
697697
"REVERSE(SUBSTR(REVERSE(column), PATINDEX('%[^' + c + ']%', REVERSE(column))))",
698698
$this->platform->getTrimExpression('column', TrimMode::TRAILING, 'c')
699699
);
700-
self::assertEquals(
701-
"REVERSE(SUBSTR(REVERSE(SUBSTR(column, PATINDEX('%[^' + c + ']%', column))), PATINDEX('%[^' + c + ']%', " .
702-
"REVERSE(SUBSTR(column, PATINDEX('%[^' + c + ']%', column))))))",
703-
$this->platform->getTrimExpression('column', null, 'c')
704-
);
705700
self::assertEquals(
706701
"REVERSE(SUBSTR(REVERSE(SUBSTR(column, PATINDEX('%[^' + c + ']%', column))), PATINDEX('%[^' + c + ']%', " .
707702
"REVERSE(SUBSTR(column, PATINDEX('%[^' + c + ']%', column))))))",

0 commit comments

Comments
 (0)