Skip to content

Commit e7b6c16

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 bca1228 + a774e63 commit e7b6c16

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
@@ -21,6 +21,7 @@
2121
use function explode;
2222
use function func_get_args;
2323
use function implode;
24+
use function in_array;
2425
use function is_array;
2526
use function is_bool;
2627
use function is_numeric;
@@ -1031,23 +1032,25 @@ public function getModExpression($expression1, $expression2)
10311032
/**
10321033
* {@inheritDoc}
10331034
*/
1034-
public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
1035+
public function getTrimExpression(string $str, int $mode = TrimMode::UNSPECIFIED, ?string $char = null) : string
10351036
{
1036-
if (! $char) {
1037-
switch ($pos) {
1037+
if (! in_array($mode, [TrimMode::UNSPECIFIED, TrimMode::LEADING, TrimMode::TRAILING, TrimMode::BOTH], true)) {
1038+
throw new InvalidArgumentException(
1039+
sprintf('The value of $mode is expected to be one of the TrimMode constants, %d given', $mode)
1040+
);
1041+
}
1042+
1043+
if ($char === null) {
1044+
switch ($mode) {
10381045
case TrimMode::LEADING:
1039-
$trimFn = 'LTRIM';
1040-
break;
1046+
return 'LTRIM(' . $str . ')';
10411047

10421048
case TrimMode::TRAILING:
1043-
$trimFn = 'RTRIM';
1044-
break;
1049+
return 'RTRIM(' . $str . ')';
10451050

10461051
default:
10471052
return 'LTRIM(RTRIM(' . $str . '))';
10481053
}
1049-
1050-
return $trimFn . '(' . $str . ')';
10511054
}
10521055

10531056
/** Original query used to get those expressions
@@ -1061,11 +1064,11 @@ public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = fa
10611064
*/
10621065
$pattern = "'%[^' + " . $char . " + ']%'";
10631066

1064-
if ($pos === TrimMode::LEADING) {
1067+
if ($mode === TrimMode::LEADING) {
10651068
return 'stuff(' . $str . ', 1, patindex(' . $pattern . ', ' . $str . ') - 1, null)';
10661069
}
10671070

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

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;
@@ -59,11 +60,14 @@ public function getNowExpression($type = 'timestamp')
5960
/**
6061
* {@inheritDoc}
6162
*/
62-
public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
63+
public function getTrimExpression(string $str, int $mode = TrimMode::UNSPECIFIED, ?string $char = null) : string
6364
{
64-
$trimChar = $char !== false ? (', ' . $char) : '';
65+
switch ($mode) {
66+
case TrimMode::UNSPECIFIED:
67+
case TrimMode::BOTH:
68+
$trimFn = 'TRIM';
69+
break;
6570

66-
switch ($pos) {
6771
case TrimMode::LEADING:
6872
$trimFn = 'LTRIM';
6973
break;
@@ -73,10 +77,21 @@ public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = fa
7377
break;
7478

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

79-
return $trimFn . '(' . $str . $trimChar . ')';
94+
return sprintf('%s(%s)', $trimFn, implode(', ', $arguments));
8095
}
8196

8297
/**

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\Type;
2020
use Doctrine\Tests\DbalFunctionalTestCase;
21+
use InvalidArgumentException;
2122
use PDO;
2223
use const CASE_LOWER;
2324
use const PHP_EOL;
@@ -508,10 +509,10 @@ public function testTrimExpression($value, $position, $char, $expectedResult)
508509
public function getTrimExpressionData()
509510
{
510511
return [
511-
['test_string', TrimMode::UNSPECIFIED, false, 'foo'],
512-
['test_string', TrimMode::LEADING, false, 'foo'],
513-
['test_string', TrimMode::TRAILING, false, 'foo'],
514-
['test_string', TrimMode::BOTH, false, 'foo'],
512+
['test_string', TrimMode::UNSPECIFIED, null, 'foo'],
513+
['test_string', TrimMode::LEADING, null, 'foo'],
514+
['test_string', TrimMode::TRAILING, null, 'foo'],
515+
['test_string', TrimMode::BOTH, null, 'foo'],
515516
['test_string', TrimMode::UNSPECIFIED, "'f'", 'oo'],
516517
['test_string', TrimMode::UNSPECIFIED, "'o'", 'f'],
517518
['test_string', TrimMode::UNSPECIFIED, "'.'", 'foo'],
@@ -524,10 +525,10 @@ public function getTrimExpressionData()
524525
['test_string', TrimMode::BOTH, "'f'", 'oo'],
525526
['test_string', TrimMode::BOTH, "'o'", 'f'],
526527
['test_string', TrimMode::BOTH, "'.'", 'foo'],
527-
["' foo '", TrimMode::UNSPECIFIED, false, 'foo'],
528-
["' foo '", TrimMode::LEADING, false, 'foo '],
529-
["' foo '", TrimMode::TRAILING, false, ' foo'],
530-
["' foo '", TrimMode::BOTH, false, 'foo'],
528+
["' foo '", TrimMode::UNSPECIFIED, null, 'foo'],
529+
["' foo '", TrimMode::LEADING, null, 'foo '],
530+
["' foo '", TrimMode::TRAILING, null, ' foo'],
531+
["' foo '", TrimMode::BOTH, null, 'foo'],
531532
["' foo '", TrimMode::UNSPECIFIED, "'f'", ' foo '],
532533
["' foo '", TrimMode::UNSPECIFIED, "'o'", ' foo '],
533534
["' foo '", TrimMode::UNSPECIFIED, "'.'", ' foo '],
@@ -547,6 +548,12 @@ public function getTrimExpressionData()
547548
];
548549
}
549550

551+
public function testTrimExpressionInvalidMode() : void
552+
{
553+
$this->expectException(InvalidArgumentException::class);
554+
$this->connection->getDatabasePlatform()->getTrimExpression('Trim me!', 0xBEEF);
555+
}
556+
550557
/**
551558
* @group DDC-1014
552559
*/

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -665,11 +665,6 @@ public function testGeneratesSQLSnippets()
665665
"REVERSE(SUBSTR(REVERSE(column), PATINDEX('%[^' + c + ']%', REVERSE(column))))",
666666
$this->platform->getTrimExpression('column', TrimMode::TRAILING, 'c')
667667
);
668-
self::assertEquals(
669-
"REVERSE(SUBSTR(REVERSE(SUBSTR(column, PATINDEX('%[^' + c + ']%', column))), PATINDEX('%[^' + c + ']%', " .
670-
"REVERSE(SUBSTR(column, PATINDEX('%[^' + c + ']%', column))))))",
671-
$this->platform->getTrimExpression('column', null, 'c')
672-
);
673668
self::assertEquals(
674669
"REVERSE(SUBSTR(REVERSE(SUBSTR(column, PATINDEX('%[^' + c + ']%', column))), PATINDEX('%[^' + c + ']%', " .
675670
"REVERSE(SUBSTR(column, PATINDEX('%[^' + c + ']%', column))))))",

0 commit comments

Comments
 (0)