1 | <?php |
||
17 | class GraphBuilder { |
||
18 | |||
19 | /** |
||
20 | * @var array nested list of conditions, grouped by subject and predicate |
||
21 | */ |
||
22 | private $conditions = array(); |
||
23 | |||
24 | /** |
||
25 | * @var string[] list of optional expressions |
||
26 | */ |
||
27 | private $optionals = array(); |
||
28 | |||
29 | /** |
||
30 | * @var string[] list of filter expressions |
||
31 | */ |
||
32 | private $filters = array(); |
||
33 | |||
34 | /** |
||
35 | * @var string[] list of unions |
||
36 | */ |
||
37 | private $unions = array(); |
||
38 | |||
39 | /** |
||
40 | * @var string[] list of subqueries |
||
41 | */ |
||
42 | private $subqueries = array(); |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $currentSubject = null; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private $currentPredicate = null; |
||
53 | |||
54 | /** |
||
55 | * @var ExpressionValidator |
||
56 | */ |
||
57 | private $expressionValidator; |
||
58 | |||
59 | /** |
||
60 | * @var UsageValidator |
||
61 | */ |
||
62 | private $usageValidator; |
||
63 | |||
64 | /** |
||
65 | * Package-private constructor, use QueryBuilder::newSubgraph instead |
||
66 | * |
||
67 | * @param UsageValidator $usageValidator |
||
68 | * @throws InvalidArgumentException |
||
69 | */ |
||
70 | public function __construct( UsageValidator $usageValidator ) { |
||
74 | |||
75 | /** |
||
76 | * Adds the given triple as a condition. |
||
77 | * |
||
78 | * @param string $subject |
||
79 | * @param string $predicate |
||
80 | * @param string $object |
||
81 | * @return self |
||
82 | * @throws InvalidArgumentException |
||
83 | */ |
||
84 | public function where( $subject, $predicate, $object ) { |
||
104 | |||
105 | /** |
||
106 | * Adds the given triple/double/single value as an additional condition |
||
107 | * to the previously added condition. |
||
108 | * |
||
109 | * @param string $subject |
||
110 | * @param string|null $predicate |
||
111 | * @param string|null $object |
||
112 | * @return self |
||
113 | * @throws InvalidArgumentException |
||
114 | */ |
||
115 | public function also( $subject, $predicate = null, $object = null ) { |
||
126 | |||
127 | /** |
||
128 | * Adds the given expression as a filter to this query. |
||
129 | * |
||
130 | * @param string $expression |
||
131 | * @return self |
||
132 | * @throws InvalidArgumentException |
||
133 | */ |
||
134 | public function filter( $expression ) { |
||
144 | |||
145 | /** |
||
146 | * Adds a filter that the given graph or triple exists. |
||
147 | * |
||
148 | * @param string|GraphBuilder $subject |
||
149 | * @param string|null $predicate |
||
150 | * @param string|null $object |
||
151 | * @return self |
||
152 | * @throws InvalidArgumentException |
||
153 | */ |
||
154 | public function filterExists( $subject, $predicate = null, $object = null ) { |
||
160 | |||
161 | /** |
||
162 | * Adds a filter that the given graph or triple does not exist. |
||
163 | * |
||
164 | * @param string|GraphBuilder $subject |
||
165 | * @param string|null $predicate |
||
166 | * @param string|null $object |
||
167 | * @return self |
||
168 | * @throws InvalidArgumentException |
||
169 | */ |
||
170 | public function filterNotExists( $subject, $predicate = null, $object = null ) { |
||
176 | |||
177 | /** |
||
178 | * Adds the given graph or triple as an optional condition. |
||
179 | * |
||
180 | * @param string|GraphBuilder $subject |
||
181 | * @param string|null $predicate |
||
182 | * @param string|null $object |
||
183 | * @return self |
||
184 | * @throws InvalidArgumentException |
||
185 | */ |
||
186 | public function optional( $subject, $predicate = null, $object = null ) { |
||
191 | |||
192 | private function getGraphBuilder( $subject, $predicate, $object ) { |
||
200 | |||
201 | /** |
||
202 | * Adds the given graphs as alternative conditions. |
||
203 | * |
||
204 | * @param GraphBuilder|GraphBuilder[] $graphs |
||
205 | * @return self |
||
206 | * @throws InvalidArgumentException |
||
207 | */ |
||
208 | public function union( $graphs /* graphs ... */ ) { |
||
217 | |||
218 | /** |
||
219 | * Adds the given subquery. |
||
220 | * |
||
221 | * @param QueryBuilder $queryBuilder |
||
222 | * @return self |
||
223 | * @throws InvalidArgumentException |
||
224 | */ |
||
225 | public function subquery( QueryBuilder $queryBuilder ) { |
||
236 | |||
237 | /** |
||
238 | * Returns the plain SPARQL string of these conditions. |
||
239 | * Surrounding brackets are not included. |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | public function getSPARQL() { |
||
258 | |||
259 | private function formatPredicates( array $predicates ) { |
||
264 | |||
265 | private function formatOptionals() { |
||
270 | |||
271 | private function formatFilters() { |
||
276 | |||
277 | private function formatUnions() { |
||
280 | |||
281 | private function formatSubqueries() { |
||
286 | |||
287 | } |
||
288 |