-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCodeFormatter.php
More file actions
187 lines (168 loc) · 4.28 KB
/
CodeFormatter.php
File metadata and controls
187 lines (168 loc) · 4.28 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace exface\Core\Widgets\Parts;
use exface\Core\CommonLogic\Traits\ICanBeConvertedToUxonTrait;
use exface\Core\CommonLogic\UxonObject;
use exface\Core\CommonLogic\Workbench;
use exface\Core\Interfaces\WidgetInterface;
use exface\Core\Interfaces\Widgets\WidgetPartInterface;
use exface\Core\Widgets\InputCode;
/**
* The CodeFormatter formats the value before it is displayed in an output.
*
* ## Examples:
* ```
* "code_formatter": {
* "prettify": true,
* "colorize": true,
* "language": "sql",
* "dialect": "tsql"
* }
* ```
*
* @author Sergej Riel
*/
class CodeFormatter implements WidgetPartInterface
{
use ICanBeConvertedToUxonTrait;
const DIALECT_SQL = 'sql';
const DIALECT_TSQL = 'tsql';
const DIALECT_MARIADB = 'mariadb';
const DIALECT_MYSQL = 'mysql';
const DIALECT_ORACLE = 'plsql';
const DIALECT_POSTGRESQL = 'postgresql';
private $inputCode;
private $language;
private $dialect = null;
private $prettify = true;
private $colorize = true;
public function __construct(InputCode $inputCode, ?UxonObject $uxon = null)
{
$this->inputCode = $inputCode;
if ($uxon) {
$this->importUxonObject($uxon);
}
}
public function getWidget(): WidgetInterface
{
return $this->inputCode->getWidget();
}
/**
* @inheritDoc
*/
public function getWorkbench(): Workbench
{
return $this->inputCode->getWorkbench();
}
/**
* @param string|null $default
* @return string|null
*/
public function getLanguage(string $default = null): ?string
{
return $this->language;
}
/**
* Sets the formatted language.
*
* @uxon-property language
* @uxon-type [sql,javascript,json]
*
* @param string $language
* @return $this
*/
public function setLanguage(string $language) : CodeFormatter
{
$this->language = $language;
return $this;
}
/**
* @return string|null
*/
public function getDialect() : ?string
{
return $this->dialect;
}
/**
* Sets the dialect for formatted language. Example: language: "sql" and dialect: "tsql".
*
* @uxon-property dialect
* @uxon-type [sql,tsql,mariadb,mysql,plsql,postgresql]
*
* @param string $dialect
* @return $this
*/
public function setDialect(string $dialect) : CodeFormatter
{
$this->dialect = $this->formatDialectName($dialect);
return $this;
}
/**
*
* @return bool
*/
public function getPrettify() : bool
{
return $this->prettify;
}
/**
* Disables code formatting if set to false
*
* @uxon-property prettify
* @uxon-type boolean
* @uxon-defaul true
*
* @param bool $prettify
* @return $this
*/
public function setPrettify(bool $prettify) : CodeFormatter
{
$this->prettify = $prettify;
return $this;
}
/**
*
* @return bool
*/
public function getColorize() : bool
{
return $this->colorize;
}
/**
* Disables code colouring if set to false
*
* @uxon-property colorize
* @uxon-type boolean
* @uxon-defaul true
*
* @param bool $colorize
* @return $this
*/
public function setColorize(bool $colorize) : CodeFormatter
{
$this->colorize = $colorize;
return $this;
}
/**
* This function maps internal dialect names to formatter-known names.
*
* @param string $value
* @return string
*/
private function formatDialectName(string $value) : string
{
$value = mb_strtolower($value);
switch ($value) {
case 'mssql':
case 't-sql': $value = self::DIALECT_TSQL; break;
case 'mariadb': $value = self::DIALECT_MARIADB; break;
case 'mysql': $value = self::DIALECT_MYSQL; break;
case 'oracle':
case 'pl/sql': $value = self::DIALECT_ORACLE; break;
case 'postgresql':
case 'pgsql': $value = self::DIALECT_POSTGRESQL; break;
case 'other':
case 'sql' : $value = self::DIALECT_SQL; break;
}
return $value;
}
}