Skip to content

Commit d847709

Browse files
committed
Add mergeFormats() back
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
1 parent 5499b01 commit d847709

File tree

3 files changed

+163
-71
lines changed

3 files changed

+163
-71
lines changed

src/Utils/Formatter.php

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use function str_contains;
2020
use function str_repeat;
2121
use function str_replace;
22-
use function strtolower;
2322
use function strtoupper;
2423

2524
use const ENT_NOQUOTES;
@@ -79,74 +78,6 @@ protected function __construct(protected FormattingOptions $options = new Format
7978
{
8079
}
8180

82-
/**
83-
* The styles used for HTML formatting.
84-
* [$type, $flags, $span, $callback].
85-
*
86-
* @return list<array{type: TokenType, flags: int, html: string, cli: string, function: callable|''}>
87-
*/
88-
protected function getDefaultFormats(): array
89-
{
90-
return [
91-
[
92-
'type' => TokenType::Keyword,
93-
'flags' => Token::FLAG_KEYWORD_RESERVED,
94-
'html' => 'sql-reserved',
95-
'cli' => "\x1b[35m",
96-
'function' => strtoupper(...),
97-
],
98-
[
99-
'type' => TokenType::Keyword,
100-
'flags' => 0,
101-
'html' => 'sql-keyword',
102-
'cli' => "\x1b[95m",
103-
'function' => strtoupper(...),
104-
],
105-
[
106-
'type' => TokenType::Comment,
107-
'flags' => 0,
108-
'html' => 'sql-comment',
109-
'cli' => "\x1b[37m",
110-
'function' => '',
111-
],
112-
[
113-
'type' => TokenType::Bool,
114-
'flags' => 0,
115-
'html' => 'sql-atom',
116-
'cli' => "\x1b[36m",
117-
'function' => strtoupper(...),
118-
],
119-
[
120-
'type' => TokenType::Number,
121-
'flags' => 0,
122-
'html' => 'sql-number',
123-
'cli' => "\x1b[92m",
124-
'function' => strtolower(...),
125-
],
126-
[
127-
'type' => TokenType::String,
128-
'flags' => 0,
129-
'html' => 'sql-string',
130-
'cli' => "\x1b[91m",
131-
'function' => '',
132-
],
133-
[
134-
'type' => TokenType::Symbol,
135-
'flags' => Token::FLAG_SYMBOL_PARAMETER,
136-
'html' => 'sql-parameter',
137-
'cli' => "\x1b[31m",
138-
'function' => '',
139-
],
140-
[
141-
'type' => TokenType::Symbol,
142-
'flags' => 0,
143-
'html' => 'sql-variable',
144-
'cli' => "\x1b[36m",
145-
'function' => '',
146-
],
147-
];
148-
}
149-
15081
/**
15182
* Formats the given list of tokens.
15283
*
@@ -450,7 +381,7 @@ public function toString(Token $token): string
450381
$text = $token->token;
451382
static $prev;
452383

453-
foreach ($this->getDefaultFormats() as $format) {
384+
foreach ($this->options->formats as $format) {
454385
if ($token->type !== $format['type'] || ! (($token->flags & $format['flags']) === $format['flags'])) {
455386
continue;
456387
}

src/Utils/FormattingOptions.php

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,124 @@
44

55
namespace PhpMyAdmin\SqlParser\Utils;
66

7+
use PhpMyAdmin\SqlParser\Token;
8+
use PhpMyAdmin\SqlParser\TokenType;
9+
10+
use function strtolower;
11+
use function strtoupper;
12+
713
use const PHP_SAPI;
814

915
final class FormattingOptions
1016
{
1117
public string $lineEnding;
1218
public string $indentation;
1319

14-
/** @param 'cli'|'text'|'html' $type */
20+
/**
21+
* @param 'cli'|'text'|'html' $type
22+
* @param list<array{type: TokenType, flags: int, html: string, cli: string, function: callable|''}> $formats
23+
*/
1524
public function __construct(
1625
public readonly string $type = PHP_SAPI === 'cli' ? 'cli' : 'text',
1726
string|null $lineEnding = null,
1827
string|null $indentation = null,
1928
public bool $removeComments = false,
2029
public bool $clauseNewline = true,
30+
public array $formats = [],
2131
) {
2232
$this->lineEnding = $lineEnding ?? ($this->type === 'html' ? '<br/>' : "\n");
2333
$this->indentation = $indentation ?? ($this->type === 'html' ? '&nbsp;&nbsp;&nbsp;&nbsp;' : ' ');
34+
$this->formats = self::mergeFormats(self::getDefaultFormats(), $this->formats);
35+
}
36+
37+
/**
38+
* @param list<array{type: TokenType, flags: int, html: string, cli: string, function: callable|''}> $formats
39+
* @param list<array{type: TokenType, flags: int, html: string, cli: string, function: callable|''}> $newFormats
40+
*
41+
* @return list<array{type: TokenType, flags: int, html: string, cli: string, function: callable|''}>
42+
*/
43+
private static function mergeFormats(array $formats, array $newFormats): array
44+
{
45+
foreach ($newFormats as $new) {
46+
foreach ($formats as $i => $original) {
47+
if ($new['type'] !== $original['type'] || $original['flags'] !== $new['flags']) {
48+
continue;
49+
}
50+
51+
$formats[$i] = $new;
52+
continue 2;
53+
}
54+
55+
$formats[] = $new;
56+
}
57+
58+
return $formats;
59+
}
60+
61+
/**
62+
* The styles used for HTML formatting.
63+
*
64+
* @return list<array{type: TokenType, flags: int, html: string, cli: string, function: callable|''}>
65+
*/
66+
public static function getDefaultFormats(): array
67+
{
68+
return [
69+
[
70+
'type' => TokenType::Keyword,
71+
'flags' => Token::FLAG_KEYWORD_RESERVED,
72+
'html' => 'sql-reserved',
73+
'cli' => "\x1b[35m",
74+
'function' => strtoupper(...),
75+
],
76+
[
77+
'type' => TokenType::Keyword,
78+
'flags' => 0,
79+
'html' => 'sql-keyword',
80+
'cli' => "\x1b[95m",
81+
'function' => strtoupper(...),
82+
],
83+
[
84+
'type' => TokenType::Comment,
85+
'flags' => 0,
86+
'html' => 'sql-comment',
87+
'cli' => "\x1b[37m",
88+
'function' => '',
89+
],
90+
[
91+
'type' => TokenType::Bool,
92+
'flags' => 0,
93+
'html' => 'sql-atom',
94+
'cli' => "\x1b[36m",
95+
'function' => strtoupper(...),
96+
],
97+
[
98+
'type' => TokenType::Number,
99+
'flags' => 0,
100+
'html' => 'sql-number',
101+
'cli' => "\x1b[92m",
102+
'function' => strtolower(...),
103+
],
104+
[
105+
'type' => TokenType::String,
106+
'flags' => 0,
107+
'html' => 'sql-string',
108+
'cli' => "\x1b[91m",
109+
'function' => '',
110+
],
111+
[
112+
'type' => TokenType::Symbol,
113+
'flags' => Token::FLAG_SYMBOL_PARAMETER,
114+
'html' => 'sql-parameter',
115+
'cli' => "\x1b[31m",
116+
'function' => '',
117+
],
118+
[
119+
'type' => TokenType::Symbol,
120+
'flags' => 0,
121+
'html' => 'sql-variable',
122+
'cli' => "\x1b[36m",
123+
'function' => '',
124+
],
125+
];
24126
}
25127
}

tests/Utils/FormatterTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,71 @@
55
namespace PhpMyAdmin\SqlParser\Tests\Utils;
66

77
use PhpMyAdmin\SqlParser\Tests\TestCase;
8+
use PhpMyAdmin\SqlParser\Token;
9+
use PhpMyAdmin\SqlParser\TokenType;
810
use PhpMyAdmin\SqlParser\Utils\Formatter;
911
use PhpMyAdmin\SqlParser\Utils\FormattingOptions;
1012
use PHPUnit\Framework\Attributes\DataProvider;
1113

14+
use function strtoupper;
15+
1216
class FormatterTest extends TestCase
1317
{
18+
public function testMergeFormats(): void
19+
{
20+
$object = new FormattingOptions(formats: []);
21+
self::assertEquals($object->formats, FormattingOptions::getDefaultFormats());
22+
23+
$object = new FormattingOptions(formats: [
24+
[
25+
'type' => TokenType::Keyword,
26+
'flags' => Token::FLAG_KEYWORD_RESERVED,
27+
'html' => 'sql-foo',
28+
'cli' => "\x1b[35m",
29+
'function' => strtoupper(...),
30+
],
31+
[
32+
'type' => TokenType::Keyword,
33+
'flags' => 0,
34+
'html' => 'sql-bar',
35+
'cli' => "\x1b[95m",
36+
'function' => strtoupper(...),
37+
],
38+
[
39+
'type' => TokenType::Keyword,
40+
'flags' => Token::FLAG_KEYWORD_COMPOSED,
41+
'html' => 'sql-baz',
42+
'cli' => "\x1b[95m",
43+
'function' => strtoupper(...),
44+
],
45+
46+
]);
47+
48+
self::assertContainsEquals([
49+
'type' => TokenType::Keyword,
50+
'flags' => Token::FLAG_KEYWORD_RESERVED,
51+
'html' => 'sql-foo',
52+
'cli' => "\x1b[35m",
53+
'function' => strtoupper(...),
54+
], $object->formats);
55+
56+
self::assertContainsEquals([
57+
'type' => TokenType::Keyword,
58+
'flags' => 0,
59+
'html' => 'sql-bar',
60+
'cli' => "\x1b[95m",
61+
'function' => strtoupper(...),
62+
], $object->formats);
63+
64+
self::assertContainsEquals([
65+
'type' => TokenType::Keyword,
66+
'flags' => Token::FLAG_KEYWORD_COMPOSED,
67+
'html' => 'sql-baz',
68+
'cli' => "\x1b[95m",
69+
'function' => strtoupper(...),
70+
], $object->formats);
71+
}
72+
1473
/** @param array{removeComments?: bool, lineEnding?: string, indentation?: string} $options */
1574
#[DataProvider('formatQueriesProviders')]
1675
public function testFormat(

0 commit comments

Comments
 (0)