forked from phpmyadmin/sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormattingOptions.php
More file actions
127 lines (115 loc) · 4.01 KB
/
Copy pathFormattingOptions.php
File metadata and controls
127 lines (115 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Utils;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokenType;
use function strtolower;
use function strtoupper;
use const PHP_SAPI;
final class FormattingOptions
{
public string $lineEnding;
public string $indentation;
/**
* @param 'cli'|'text'|'html' $type
* @param list<array{type: TokenType, flags: int, html: string, cli: string, function: callable|''}> $formats
*/
public function __construct(
public readonly string $type = PHP_SAPI === 'cli' ? 'cli' : 'text',
string|null $lineEnding = null,
string|null $indentation = null,
public bool $removeComments = false,
public bool $clauseNewline = true,
public array $formats = [],
) {
$this->lineEnding = $lineEnding ?? ($this->type === 'html' ? '<br/>' : "\n");
$this->indentation = $indentation ?? ($this->type === 'html' ? ' ' : ' ');
$this->formats = self::mergeFormats(self::getDefaultFormats(), $this->formats);
}
/**
* @param list<array{type: TokenType, flags: int, html: string, cli: string, function: callable|''}> $formats
* @param list<array{type: TokenType, flags: int, html: string, cli: string, function: callable|''}> $newFormats
*
* @return list<array{type: TokenType, flags: int, html: string, cli: string, function: callable|''}>
*/
private static function mergeFormats(array $formats, array $newFormats): array
{
foreach ($newFormats as $new) {
foreach ($formats as $i => $original) {
if ($new['type'] !== $original['type'] || $original['flags'] !== $new['flags']) {
continue;
}
$formats[$i] = $new;
continue 2;
}
$formats[] = $new;
}
return $formats;
}
/**
* The styles used for HTML formatting.
*
* @return list<array{type: TokenType, flags: int, html: string, cli: string, function: callable|''}>
*/
public static function getDefaultFormats(): array
{
return [
[
'type' => TokenType::Keyword,
'flags' => Token::FLAG_KEYWORD_RESERVED,
'html' => 'sql-reserved',
'cli' => "\x1b[35m",
'function' => strtoupper(...),
],
[
'type' => TokenType::Keyword,
'flags' => 0,
'html' => 'sql-keyword',
'cli' => "\x1b[95m",
'function' => strtoupper(...),
],
[
'type' => TokenType::Comment,
'flags' => 0,
'html' => 'sql-comment',
'cli' => "\x1b[37m",
'function' => '',
],
[
'type' => TokenType::Bool,
'flags' => 0,
'html' => 'sql-atom',
'cli' => "\x1b[36m",
'function' => strtoupper(...),
],
[
'type' => TokenType::Number,
'flags' => 0,
'html' => 'sql-number',
'cli' => "\x1b[92m",
'function' => strtolower(...),
],
[
'type' => TokenType::String,
'flags' => 0,
'html' => 'sql-string',
'cli' => "\x1b[91m",
'function' => '',
],
[
'type' => TokenType::Symbol,
'flags' => Token::FLAG_SYMBOL_PARAMETER,
'html' => 'sql-parameter',
'cli' => "\x1b[31m",
'function' => '',
],
[
'type' => TokenType::Symbol,
'flags' => 0,
'html' => 'sql-variable',
'cli' => "\x1b[36m",
'function' => '',
],
];
}
}