1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Error related utilities. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace PhpMyAdmin\SqlParser\Utils; |
8
|
|
|
|
9
|
|
|
use PhpMyAdmin\SqlParser\Lexer; |
10
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Error related utilities. |
14
|
|
|
* |
15
|
|
|
* @category Exceptions |
16
|
|
|
* |
17
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ |
18
|
|
|
*/ |
19
|
|
|
class Error |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Gets the errors of a lexer and a parser. |
23
|
|
|
* |
24
|
|
|
* @param array $objs objects from where the errors will be extracted |
25
|
|
|
* |
26
|
|
|
* @return array Each element of the array represents an error. |
27
|
|
|
* `$err[0]` holds the error message. |
28
|
|
|
* `$err[1]` holds the error code. |
29
|
|
|
* `$err[2]` holds the string that caused the issue. |
30
|
|
|
* `$err[3]` holds the position of the string. |
31
|
|
|
* (i.e. `array($msg, $code, $str, $pos)`) |
32
|
|
|
*/ |
33
|
4 |
|
public static function get($objs) |
34
|
|
|
{ |
35
|
4 |
|
$ret = array(); |
36
|
|
|
|
37
|
4 |
|
foreach ($objs as $obj) { |
38
|
4 |
|
if ($obj instanceof Lexer) { |
39
|
4 |
|
foreach ($obj->errors as $err) { |
40
|
1 |
|
$ret[] = array( |
41
|
1 |
|
$err->getMessage(), |
42
|
1 |
|
$err->getCode(), |
43
|
1 |
|
$err->ch, |
44
|
4 |
|
$err->pos, |
45
|
|
|
); |
46
|
|
|
} |
47
|
4 |
|
} elseif ($obj instanceof Parser) { |
48
|
4 |
|
foreach ($obj->errors as $err) { |
49
|
2 |
|
$ret[] = array( |
50
|
2 |
|
$err->getMessage(), |
51
|
2 |
|
$err->getCode(), |
52
|
2 |
|
$err->token->token, |
53
|
4 |
|
$err->token->position, |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
return $ret; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Formats the specified errors. |
64
|
|
|
* |
65
|
|
|
* @param array $errors the errors to be formatted |
66
|
|
|
* @param string $format The format of an error. |
67
|
|
|
* '$1$d' is replaced by the position of this error. |
68
|
|
|
* '$2$s' is replaced by the error message. |
69
|
|
|
* '$3$d' is replaced by the error code. |
70
|
|
|
* '$4$s' is replaced by the string that caused the |
71
|
|
|
* issue. |
72
|
|
|
* '$5$d' is replaced by the position of the string. |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
2 |
|
public static function format( |
77
|
|
|
$errors, |
78
|
|
|
$format = '#%1$d: %2$s (near "%4$s" at position %5$d)' |
79
|
|
|
) { |
80
|
2 |
|
$ret = array(); |
81
|
|
|
|
82
|
2 |
|
$i = 0; |
83
|
2 |
|
foreach ($errors as $key => $err) { |
84
|
2 |
|
$ret[$key] = sprintf( |
85
|
2 |
|
$format, |
86
|
|
|
++$i, |
87
|
2 |
|
$err[0], |
88
|
2 |
|
$err[1], |
89
|
2 |
|
htmlspecialchars($err[2]), |
90
|
2 |
|
$err[3] |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
return $ret; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|