1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Asparagus; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Formatter for SPARQL queries |
7
|
|
|
* |
8
|
|
|
* @since 0.1 |
9
|
|
|
* |
10
|
|
|
* @license GNU GPL v2+ |
11
|
|
|
* @author Bene* < [email protected] > |
12
|
|
|
*/ |
13
|
|
|
class QueryFormatter { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string[] |
17
|
|
|
*/ |
18
|
|
|
private $formattedParts; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $indentationLevel; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string[] |
27
|
|
|
*/ |
28
|
|
|
private $replacements; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Formats the given SPARQL string. |
32
|
|
|
* Note that there have to be spaces before and after brackets and dots. |
33
|
|
|
* |
34
|
|
|
* @param string $sparql |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function format( $sparql ) { |
38
|
|
|
$this->formattedParts = array(); |
39
|
|
|
$this->indentationLevel = 0; |
40
|
|
|
$this->replacements = array(); |
41
|
|
|
|
42
|
|
|
$regexHelper = new RegexHelper(); |
43
|
|
|
$sparql = $regexHelper->escapeSequences( $sparql, $this->replacements ); |
44
|
|
|
|
45
|
|
|
foreach ( $this->split( $sparql ) as $part ) { |
46
|
|
|
if ( !empty( $this->formattedParts ) ) { |
47
|
|
|
$this->before( $part ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->indentation( $part ); |
51
|
|
|
$this->append( $part ); |
52
|
|
|
$this->after( $part ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->trimEnd(); |
56
|
|
|
$this->formattedParts[] = "\n"; |
57
|
|
|
|
58
|
|
|
return strtr( implode( $this->formattedParts ), $this->replacements ); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function split( $string ) { |
62
|
|
|
return preg_split( |
63
|
|
|
'/(\W)/', |
64
|
|
|
$string, |
65
|
|
|
-1, |
66
|
|
|
PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function before( $part ) { |
71
|
|
|
if ( in_array( $part, array( 'PREFIX', 'FILTER', 'OPTIONAL', 'LIMIT', 'GROUP', 'ORDER', 'HAVING', '}' ) ) ) { |
72
|
|
|
$this->trimEnd(); |
73
|
|
|
$this->formattedParts[] = "\n"; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ( $part === 'SELECT' && $this->indentationLevel === 0 ) { |
77
|
|
|
$this->trimEnd(); |
78
|
|
|
$this->formattedParts[] = "\n\n"; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// UNION is the only part we want to have in-line with "}", ie. } UNION { |
82
|
|
|
if ( $part === 'UNION' || end( $this->formattedParts ) !== "\n" && |
83
|
|
|
in_array( $part, array( '.', '=', '(', '<', '{', '?', '$' ) ) |
84
|
|
|
) { |
85
|
|
|
$this->trimEnd(); |
86
|
|
|
$this->append( ' ' ); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function indentation( $part ) { |
91
|
|
|
if ( $part === '}' ) { |
92
|
|
|
$this->indentationLevel--; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ( !ctype_space( $part ) && substr( end( $this->formattedParts ), 0, 1 ) === "\n" ) { |
96
|
|
|
$this->formattedParts[] = str_repeat( "\t", $this->indentationLevel ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ( $part === '{' ) { |
100
|
|
|
$this->indentationLevel++; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function append( $part ) { |
105
|
|
|
if ( !ctype_space( $part ) ) { |
106
|
|
|
$this->formattedParts[] = $part; |
107
|
|
|
} else if ( // ctype_space( $part ) && |
|
|
|
|
108
|
|
|
!ctype_space( end( $this->formattedParts ) ) && |
109
|
|
|
end( $this->formattedParts ) !== '(' |
110
|
|
|
) { |
111
|
|
|
$this->formattedParts[] = ' '; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function after( $part ) { |
116
|
|
|
if ( in_array( $part, array( '{', '}', '.' ) ) ) { |
117
|
|
|
$this->formattedParts[] = "\n"; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ( $part === ';' ) { |
121
|
|
|
$this->formattedParts[] = "\n\t"; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function trimEnd() { |
126
|
|
|
while ( ctype_space( end( $this->formattedParts ) ) ) { |
127
|
|
|
array_pop( $this->formattedParts ); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.