diff --git a/src/SqlFormatter.php b/src/SqlFormatter.php index 4fe4c46..009cdc3 100644 --- a/src/SqlFormatter.php +++ b/src/SqlFormatter.php @@ -47,7 +47,7 @@ public function __construct(?Highlighter $highlighter = null) * * @return string The SQL string with HTML styles and formatting wrapped in a
tag */ - public function format(string $string, string $indentString = ' '): string + public function format(string $string, string $indentString = ' ', bool $forceUppercase = false): string { // This variable will be populated with formatted html $return = ''; @@ -71,9 +71,23 @@ public function format(string $string, string $indentString = ' '): string // Format token by token while ($token = $cursor->next(Token::TOKEN_TYPE_WHITESPACE)) { + // Uppercase reserved words + $uppercaseTypes = [ + Token::TOKEN_TYPE_RESERVED, + Token::TOKEN_TYPE_RESERVED_NEWLINE, + Token::TOKEN_TYPE_RESERVED_TOPLEVEL + ]; + + // Uppercase transformation if desired + if ($forceUppercase && in_array($token->type(), $uppercaseTypes)) { + $tokenValue = strtoupper($token->value()); + } else { + $tokenValue = $token->value(); + } + $highlighted = $this->highlighter->highlightToken( $token->type(), - $token->value() + $tokenValue ); // If we are increasing the special indent level now diff --git a/tests/SqlFormatterTest.php b/tests/SqlFormatterTest.php index 68d90cd..4edb9d9 100644 --- a/tests/SqlFormatterTest.php +++ b/tests/SqlFormatterTest.php @@ -60,6 +60,16 @@ public function testFormat(string $sql, string $html): void $this->assertEquals(trim($html), trim($formatter->format($sql))); } + public function testFormatUpperCase(): void + { + $formatter = new SqlFormatter(new NullHighlighter()); + + $actual = $formatter->format('select a from b where c = d;', ' ', true); + $expected = "SELECT\n a\nFROM\n b\nWHERE\n c = d;"; + + $this->assertEquals(trim($expected), trim($actual)); + } + /** * @dataProvider highlightData */