1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Asparagus; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Package-private class to build the modifiers of a SPARQL query. |
9
|
|
|
* |
10
|
|
|
* @license GNU GPL v2+ |
11
|
|
|
* @author Bene* < [email protected] > |
12
|
|
|
*/ |
13
|
|
|
class QueryModifierBuilder { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string[] list of modifiers including limit, offset and order by |
17
|
|
|
*/ |
18
|
|
|
private $modifiers = array(); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ExpressionValidator |
22
|
|
|
*/ |
23
|
|
|
private $expressionValidator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var UsageValidator |
27
|
|
|
*/ |
28
|
|
|
private $usageValidator; |
29
|
|
|
|
30
|
|
|
public function __construct( UsageValidator $usageValidator ) { |
31
|
|
|
$this->expressionValidator = new ExpressionValidator(); |
32
|
|
|
$this->usageValidator = $usageValidator; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Sets the GROUP BY modifiers. |
37
|
|
|
* |
38
|
|
|
* @param string[] $expressions |
39
|
|
|
*/ |
40
|
|
|
public function groupBy( array $expressions ) { |
41
|
|
|
foreach ( $expressions as $expression ) { |
42
|
|
|
$this->expressionValidator->validate( $expression, |
43
|
|
|
ExpressionValidator::VALIDATE_VARIABLE | ExpressionValidator::VALIDATE_FUNCTION_AS |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$expression = implode( ' ', $expressions ); |
48
|
|
|
$this->usageValidator->trackUsedPrefixes( $expression ); |
49
|
|
|
$this->usageValidator->trackUsedVariables( $expression ); |
50
|
|
|
$this->modifiers['GROUP BY'] = $expression; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Sets the HAVING modifier. |
55
|
|
|
* |
56
|
|
|
* @param string $expression |
57
|
|
|
*/ |
58
|
|
|
public function having( $expression ) { |
59
|
|
|
$this->expressionValidator->validate( $expression, ExpressionValidator::VALIDATE_FUNCTION ); |
60
|
|
|
|
61
|
|
|
$this->usageValidator->trackUsedPrefixes( $expression ); |
62
|
|
|
$this->usageValidator->trackUsedVariables( $expression ); |
63
|
|
|
$this->modifiers['HAVING'] = '(' . $expression . ')'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sets the ORDER BY modifier. |
68
|
|
|
* |
69
|
|
|
* @param string $expression |
70
|
|
|
* @param string $direction one of ASC or DESC |
71
|
|
|
* @throws InvalidArgumentException |
72
|
|
|
*/ |
73
|
|
|
public function orderBy( $expression, $direction = 'ASC' ) { |
74
|
|
|
$direction = strtoupper( $direction ); |
75
|
|
|
if ( !in_array( $direction, array( 'ASC', 'DESC' ) ) ) { |
76
|
|
|
throw new InvalidArgumentException( '$direction has to be either ASC or DESC' ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->expressionValidator->validate( $expression, |
80
|
|
|
ExpressionValidator::VALIDATE_VARIABLE | ExpressionValidator::VALIDATE_FUNCTION |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$this->usageValidator->trackUsedPrefixes( $expression ); |
84
|
|
|
$this->usageValidator->trackUsedVariables( $expression ); |
85
|
|
|
$this->modifiers['ORDER BY'] = $direction . ' (' . $expression . ')'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Sets the LIMIT modifier. |
90
|
|
|
* |
91
|
|
|
* @param int $limit |
92
|
|
|
* @throws InvalidArgumentException |
93
|
|
|
*/ |
94
|
|
|
public function limit( $limit ) { |
95
|
|
|
if ( !is_int( $limit ) ) { |
96
|
|
|
throw new InvalidArgumentException( '$limit has to be an integer' ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->modifiers['LIMIT'] = $limit; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Sets the OFFSET modifier. |
104
|
|
|
* |
105
|
|
|
* @param int $offset |
106
|
|
|
* @throws InvalidArgumentException |
107
|
|
|
*/ |
108
|
|
|
public function offset( $offset ) { |
109
|
|
|
if ( !is_int( $offset ) ) { |
110
|
|
|
throw new InvalidArgumentException( '$offset has to be an integer' ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->modifiers['OFFSET'] = $offset; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns the plain SPARQL string of these modifiers. |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getSPARQL() { |
122
|
|
|
$modifiers = $this->modifiers; |
123
|
|
|
return implode( array_map( function( $key ) use ( $modifiers ) { |
124
|
|
|
if ( isset( $modifiers[$key] ) ) { |
125
|
|
|
return ' ' . $key . ' ' . $modifiers[$key]; |
126
|
|
|
} |
127
|
|
|
}, array( 'GROUP BY', 'HAVING', 'ORDER BY', 'LIMIT', 'OFFSET' ) ) ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|