@@ -34,31 +34,31 @@ discard block |
||
34 | 34 | * @param string $sparql |
35 | 35 | * @return string |
36 | 36 | */ |
37 | - public function format( $sparql ) { |
|
37 | + public function format($sparql) { |
|
38 | 38 | $this->formattedParts = array(); |
39 | 39 | $this->indentationLevel = 0; |
40 | 40 | $this->replacements = array(); |
41 | 41 | |
42 | 42 | $regexHelper = new RegexHelper(); |
43 | - $sparql = $regexHelper->escapeSequences( $sparql, $this->replacements ); |
|
43 | + $sparql = $regexHelper->escapeSequences($sparql, $this->replacements); |
|
44 | 44 | |
45 | - foreach ( $this->split( $sparql ) as $part ) { |
|
46 | - if ( !empty( $this->formattedParts ) ) { |
|
47 | - $this->before( $part ); |
|
45 | + foreach ($this->split($sparql) as $part) { |
|
46 | + if (!empty($this->formattedParts)) { |
|
47 | + $this->before($part); |
|
48 | 48 | } |
49 | 49 | |
50 | - $this->indentation( $part ); |
|
51 | - $this->append( $part ); |
|
52 | - $this->after( $part ); |
|
50 | + $this->indentation($part); |
|
51 | + $this->append($part); |
|
52 | + $this->after($part); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | $this->trimEnd(); |
56 | 56 | $this->formattedParts[] = "\n"; |
57 | 57 | |
58 | - return strtr( implode( $this->formattedParts ), $this->replacements ); |
|
58 | + return strtr(implode($this->formattedParts), $this->replacements); |
|
59 | 59 | } |
60 | 60 | |
61 | - private function split( $string ) { |
|
61 | + private function split($string) { |
|
62 | 62 | return preg_split( |
63 | 63 | '/(\W)/', |
64 | 64 | $string, |
@@ -67,64 +67,64 @@ discard block |
||
67 | 67 | ); |
68 | 68 | } |
69 | 69 | |
70 | - private function before( $part ) { |
|
71 | - if ( in_array( $part, array( 'PREFIX', 'FILTER', 'OPTIONAL', 'LIMIT', 'GROUP', 'ORDER', 'HAVING', '}' ) ) ) { |
|
70 | + private function before($part) { |
|
71 | + if (in_array($part, array('PREFIX', 'FILTER', 'OPTIONAL', 'LIMIT', 'GROUP', 'ORDER', 'HAVING', '}'))) { |
|
72 | 72 | $this->trimEnd(); |
73 | 73 | $this->formattedParts[] = "\n"; |
74 | 74 | } |
75 | 75 | |
76 | - if ( $part === 'SELECT' && $this->indentationLevel === 0 ) { |
|
76 | + if ($part === 'SELECT' && $this->indentationLevel === 0) { |
|
77 | 77 | $this->trimEnd(); |
78 | 78 | $this->formattedParts[] = "\n\n"; |
79 | 79 | } |
80 | 80 | |
81 | 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( '.', '=', '(', '<', '{', '?', '$' ) ) |
|
82 | + if ($part === 'UNION' || end($this->formattedParts) !== "\n" && |
|
83 | + in_array($part, array('.', '=', '(', '<', '{', '?', '$')) |
|
84 | 84 | ) { |
85 | 85 | $this->trimEnd(); |
86 | - $this->append( ' ' ); |
|
86 | + $this->append(' '); |
|
87 | 87 | } |
88 | 88 | } |
89 | 89 | |
90 | - private function indentation( $part ) { |
|
91 | - if ( $part === '}' ) { |
|
90 | + private function indentation($part) { |
|
91 | + if ($part === '}') { |
|
92 | 92 | $this->indentationLevel--; |
93 | 93 | } |
94 | 94 | |
95 | - if ( !ctype_space( $part ) && substr( end( $this->formattedParts ), 0, 1 ) === "\n" ) { |
|
96 | - $this->formattedParts[] = str_repeat( "\t", $this->indentationLevel ); |
|
95 | + if (!ctype_space($part) && substr(end($this->formattedParts), 0, 1) === "\n") { |
|
96 | + $this->formattedParts[] = str_repeat("\t", $this->indentationLevel); |
|
97 | 97 | } |
98 | 98 | |
99 | - if ( $part === '{' ) { |
|
99 | + if ($part === '{') { |
|
100 | 100 | $this->indentationLevel++; |
101 | 101 | } |
102 | 102 | } |
103 | 103 | |
104 | - private function append( $part ) { |
|
105 | - if ( !ctype_space( $part ) ) { |
|
104 | + private function append($part) { |
|
105 | + if (!ctype_space($part)) { |
|
106 | 106 | $this->formattedParts[] = $part; |
107 | 107 | } else if ( // ctype_space( $part ) && |
108 | - !ctype_space( end( $this->formattedParts ) ) && |
|
109 | - end( $this->formattedParts ) !== '(' |
|
108 | + !ctype_space(end($this->formattedParts)) && |
|
109 | + end($this->formattedParts) !== '(' |
|
110 | 110 | ) { |
111 | 111 | $this->formattedParts[] = ' '; |
112 | 112 | } |
113 | 113 | } |
114 | 114 | |
115 | - private function after( $part ) { |
|
116 | - if ( in_array( $part, array( '{', '}', '.' ) ) ) { |
|
115 | + private function after($part) { |
|
116 | + if (in_array($part, array('{', '}', '.'))) { |
|
117 | 117 | $this->formattedParts[] = "\n"; |
118 | 118 | } |
119 | 119 | |
120 | - if ( $part === ';' ) { |
|
120 | + if ($part === ';') { |
|
121 | 121 | $this->formattedParts[] = "\n\t"; |
122 | 122 | } |
123 | 123 | } |
124 | 124 | |
125 | 125 | private function trimEnd() { |
126 | - while ( ctype_space( end( $this->formattedParts ) ) ) { |
|
127 | - array_pop( $this->formattedParts ); |
|
126 | + while (ctype_space(end($this->formattedParts))) { |
|
127 | + array_pop($this->formattedParts); |
|
128 | 128 | } |
129 | 129 | } |
130 | 130 |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | * @param UsageValidator $usageValidator |
68 | 68 | * @throws InvalidArgumentException |
69 | 69 | */ |
70 | - public function __construct( UsageValidator $usageValidator ) { |
|
70 | + public function __construct(UsageValidator $usageValidator) { |
|
71 | 71 | $this->expressionValidator = new ExpressionValidator(); |
72 | 72 | $this->usageValidator = $usageValidator; |
73 | 73 | } |
@@ -81,19 +81,19 @@ discard block |
||
81 | 81 | * @return self |
82 | 82 | * @throws InvalidArgumentException |
83 | 83 | */ |
84 | - public function where( $subject, $predicate, $object ) { |
|
85 | - $this->expressionValidator->validate( $subject, |
|
84 | + public function where($subject, $predicate, $object) { |
|
85 | + $this->expressionValidator->validate($subject, |
|
86 | 86 | ExpressionValidator::VALIDATE_VARIABLE | ExpressionValidator::VALIDATE_PREFIXED_IRI |
87 | 87 | ); |
88 | - $this->expressionValidator->validate( $predicate, |
|
88 | + $this->expressionValidator->validate($predicate, |
|
89 | 89 | ExpressionValidator::VALIDATE_VARIABLE | ExpressionValidator::VALIDATE_PATH |
90 | 90 | ); |
91 | - $this->expressionValidator->validate( $object, |
|
91 | + $this->expressionValidator->validate($object, |
|
92 | 92 | ExpressionValidator::VALIDATE_VARIABLE | ExpressionValidator::VALIDATE_PREFIXED_IRI | ExpressionValidator::VALIDATE_NATIVE |
93 | 93 | ); |
94 | 94 | |
95 | - $this->usageValidator->trackUsedPrefixes( $subject . ' ' . $predicate . ' ' . $object ); |
|
96 | - $this->usageValidator->trackDefinedVariables( $subject . ' ' . $predicate . ' ' . $object ); |
|
95 | + $this->usageValidator->trackUsedPrefixes($subject . ' ' . $predicate . ' ' . $object); |
|
96 | + $this->usageValidator->trackDefinedVariables($subject . ' ' . $predicate . ' ' . $object); |
|
97 | 97 | |
98 | 98 | $this->currentSubject = $subject; |
99 | 99 | $this->currentPredicate = $predicate; |
@@ -112,13 +112,13 @@ discard block |
||
112 | 112 | * @return self |
113 | 113 | * @throws InvalidArgumentException |
114 | 114 | */ |
115 | - public function also( $subject, $predicate = null, $object = null ) { |
|
116 | - if ( $predicate === null ) { |
|
117 | - $this->where( $this->currentSubject, $this->currentPredicate, $subject ); |
|
118 | - } else if ( $object === null ) { |
|
119 | - $this->where( $this->currentSubject, $subject, $predicate ); |
|
115 | + public function also($subject, $predicate = null, $object = null) { |
|
116 | + if ($predicate === null) { |
|
117 | + $this->where($this->currentSubject, $this->currentPredicate, $subject); |
|
118 | + } else if ($object === null) { |
|
119 | + $this->where($this->currentSubject, $subject, $predicate); |
|
120 | 120 | } else { |
121 | - $this->where( $subject, $predicate, $object ); |
|
121 | + $this->where($subject, $predicate, $object); |
|
122 | 122 | } |
123 | 123 | |
124 | 124 | return $this; |
@@ -131,11 +131,11 @@ discard block |
||
131 | 131 | * @return self |
132 | 132 | * @throws InvalidArgumentException |
133 | 133 | */ |
134 | - public function filter( $expression ) { |
|
135 | - $this->expressionValidator->validate( $expression, ExpressionValidator::VALIDATE_FUNCTION ); |
|
134 | + public function filter($expression) { |
|
135 | + $this->expressionValidator->validate($expression, ExpressionValidator::VALIDATE_FUNCTION); |
|
136 | 136 | |
137 | - $this->usageValidator->trackUsedPrefixes( $expression ); |
|
138 | - $this->usageValidator->trackUsedVariables( $expression ); |
|
137 | + $this->usageValidator->trackUsedPrefixes($expression); |
|
138 | + $this->usageValidator->trackUsedVariables($expression); |
|
139 | 139 | |
140 | 140 | $this->filters[] = '(' . $expression . ')'; |
141 | 141 | |
@@ -151,8 +151,8 @@ discard block |
||
151 | 151 | * @return self |
152 | 152 | * @throws InvalidArgumentException |
153 | 153 | */ |
154 | - public function filterExists( $subject, $predicate = null, $object = null ) { |
|
155 | - $graphBuilder = $this->getGraphBuilder( $subject, $predicate, $object ); |
|
154 | + public function filterExists($subject, $predicate = null, $object = null) { |
|
155 | + $graphBuilder = $this->getGraphBuilder($subject, $predicate, $object); |
|
156 | 156 | $this->filters[] = 'EXISTS {' . $graphBuilder->getSPARQL() . ' }'; |
157 | 157 | |
158 | 158 | return $this; |
@@ -167,8 +167,8 @@ discard block |
||
167 | 167 | * @return self |
168 | 168 | * @throws InvalidArgumentException |
169 | 169 | */ |
170 | - public function filterNotExists( $subject, $predicate = null, $object = null ) { |
|
171 | - $graphBuilder = $this->getGraphBuilder( $subject, $predicate, $object ); |
|
170 | + public function filterNotExists($subject, $predicate = null, $object = null) { |
|
171 | + $graphBuilder = $this->getGraphBuilder($subject, $predicate, $object); |
|
172 | 172 | $this->filters[] = 'NOT EXISTS {' . $graphBuilder->getSPARQL() . ' }'; |
173 | 173 | |
174 | 174 | return $this; |
@@ -183,19 +183,19 @@ discard block |
||
183 | 183 | * @return self |
184 | 184 | * @throws InvalidArgumentException |
185 | 185 | */ |
186 | - public function optional( $subject, $predicate = null, $object = null ) { |
|
187 | - $graphBuilder = $this->getGraphBuilder( $subject, $predicate, $object ); |
|
186 | + public function optional($subject, $predicate = null, $object = null) { |
|
187 | + $graphBuilder = $this->getGraphBuilder($subject, $predicate, $object); |
|
188 | 188 | $this->optionals[] = $graphBuilder->getSPARQL(); |
189 | 189 | return $this; |
190 | 190 | } |
191 | 191 | |
192 | - private function getGraphBuilder( $subject, $predicate, $object ) { |
|
193 | - if ( $subject instanceof GraphBuilder ) { |
|
192 | + private function getGraphBuilder($subject, $predicate, $object) { |
|
193 | + if ($subject instanceof GraphBuilder) { |
|
194 | 194 | return $subject; |
195 | 195 | } |
196 | 196 | |
197 | - $graphBuilder = new GraphBuilder( $this->usageValidator ); |
|
198 | - return $graphBuilder->where( $subject, $predicate, $object ); |
|
197 | + $graphBuilder = new GraphBuilder($this->usageValidator); |
|
198 | + return $graphBuilder->where($subject, $predicate, $object); |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | /** |
@@ -205,12 +205,12 @@ discard block |
||
205 | 205 | * @return self |
206 | 206 | * @throws InvalidArgumentException |
207 | 207 | */ |
208 | - public function union( $graphs /* graphs ... */ ) { |
|
209 | - $graphs = is_array( $graphs ) ? $graphs : func_get_args(); |
|
208 | + public function union($graphs /* graphs ... */) { |
|
209 | + $graphs = is_array($graphs) ? $graphs : func_get_args(); |
|
210 | 210 | |
211 | - $this->unions[] = implode( ' UNION', array_map( function( GraphBuilder $graph ) { |
|
211 | + $this->unions[] = implode(' UNION', array_map(function(GraphBuilder $graph) { |
|
212 | 212 | return ' {' . $graph->getSPARQL() . ' }'; |
213 | - }, $graphs ) ); |
|
213 | + }, $graphs)); |
|
214 | 214 | |
215 | 215 | return $this; |
216 | 216 | } |
@@ -222,14 +222,14 @@ discard block |
||
222 | 222 | * @return self |
223 | 223 | * @throws InvalidArgumentException |
224 | 224 | */ |
225 | - public function subquery( QueryBuilder $queryBuilder ) { |
|
226 | - $this->subqueries[] = $queryBuilder->getSPARQL( false ); |
|
227 | - $this->usageValidator->trackDefinedVariables( implode( ' ', $queryBuilder->getSelects() ) ); |
|
225 | + public function subquery(QueryBuilder $queryBuilder) { |
|
226 | + $this->subqueries[] = $queryBuilder->getSPARQL(false); |
|
227 | + $this->usageValidator->trackDefinedVariables(implode(' ', $queryBuilder->getSelects())); |
|
228 | 228 | |
229 | 229 | // @todo temp hack to add AS definitions to defined variables |
230 | 230 | $regexHelper = new RegexHelper(); |
231 | - $matches = $regexHelper->getMatches( 'AS \{variable}', implode( ' ', $queryBuilder->getSelects() ) ); |
|
232 | - $this->usageValidator->trackDefinedVariables( $matches ); |
|
231 | + $matches = $regexHelper->getMatches('AS \{variable}', implode(' ', $queryBuilder->getSelects())); |
|
232 | + $this->usageValidator->trackDefinedVariables($matches); |
|
233 | 233 | |
234 | 234 | return $this; |
235 | 235 | } |
@@ -244,9 +244,9 @@ discard block |
||
244 | 244 | // add subqueries to the beginning because they are logically evaluated first |
245 | 245 | $sparql = $this->formatSubqueries(); |
246 | 246 | |
247 | - foreach ( $this->conditions as $subject => $predicates ) { |
|
247 | + foreach ($this->conditions as $subject => $predicates) { |
|
248 | 248 | $sparql .= ' ' . $subject; |
249 | - $sparql .= $this->formatPredicates( $predicates ) . ' .'; |
|
249 | + $sparql .= $this->formatPredicates($predicates) . ' .'; |
|
250 | 250 | } |
251 | 251 | |
252 | 252 | $sparql .= $this->formatOptionals(); |
@@ -256,32 +256,32 @@ discard block |
||
256 | 256 | return $sparql; |
257 | 257 | } |
258 | 258 | |
259 | - private function formatPredicates( array $predicates ) { |
|
260 | - return implode( ' ;', array_map( function( $predicate, $objects ) { |
|
261 | - return ' ' . $predicate . ' ' . implode( ' , ', $objects ); |
|
262 | - }, array_keys( $predicates ), $predicates ) ); |
|
259 | + private function formatPredicates(array $predicates) { |
|
260 | + return implode(' ;', array_map(function($predicate, $objects) { |
|
261 | + return ' ' . $predicate . ' ' . implode(' , ', $objects); |
|
262 | + }, array_keys($predicates), $predicates)); |
|
263 | 263 | } |
264 | 264 | |
265 | 265 | private function formatOptionals() { |
266 | - return implode( array_map( function( $optional ) { |
|
266 | + return implode(array_map(function($optional) { |
|
267 | 267 | return ' OPTIONAL {' . $optional . ' }'; |
268 | - }, $this->optionals ) ); |
|
268 | + }, $this->optionals)); |
|
269 | 269 | } |
270 | 270 | |
271 | 271 | private function formatFilters() { |
272 | - return implode( array_map( function( $filter ) { |
|
272 | + return implode(array_map(function($filter) { |
|
273 | 273 | return ' FILTER ' . $filter; |
274 | - }, $this->filters ) ); |
|
274 | + }, $this->filters)); |
|
275 | 275 | } |
276 | 276 | |
277 | 277 | private function formatUnions() { |
278 | - return implode( $this->unions ); |
|
278 | + return implode($this->unions); |
|
279 | 279 | } |
280 | 280 | |
281 | 281 | private function formatSubqueries() { |
282 | - return implode( array_map( function( $subquery ) { |
|
282 | + return implode(array_map(function($subquery) { |
|
283 | 283 | return ' { ' . $subquery . ' }'; |
284 | - }, $this->subqueries ) ); |
|
284 | + }, $this->subqueries)); |
|
285 | 285 | } |
286 | 286 | |
287 | 287 | } |
@@ -13,67 +13,67 @@ |
||
13 | 13 | */ |
14 | 14 | class QueryExecuterTest extends \PHPUnit_Framework_TestCase { |
15 | 15 | |
16 | - private function getHttpMock( $params ) { |
|
17 | - $http = $this->getMockBuilder( 'Asparagus\Http' ) |
|
16 | + private function getHttpMock($params) { |
|
17 | + $http = $this->getMockBuilder('Asparagus\Http') |
|
18 | 18 | ->disableOriginalConstructor() |
19 | 19 | ->getMock(); |
20 | 20 | |
21 | - $http->expects( $this->once() ) |
|
22 | - ->method( 'request' ) |
|
21 | + $http->expects($this->once()) |
|
22 | + ->method('request') |
|
23 | 23 | ->with( |
24 | - $this->equalTo( 'test.example.com' ), |
|
25 | - $this->equalTo( $params ) |
|
24 | + $this->equalTo('test.example.com'), |
|
25 | + $this->equalTo($params) |
|
26 | 26 | ) |
27 | - ->will( $this->returnValue( '{"results":{"bindings":"~=[,,_,,]:3"}}' ) ); |
|
27 | + ->will($this->returnValue('{"results":{"bindings":"~=[,,_,,]:3"}}')); |
|
28 | 28 | |
29 | 29 | return $http; |
30 | 30 | } |
31 | 31 | |
32 | 32 | public function testExecute() { |
33 | - $http = $this->getHttpMock( array( |
|
33 | + $http = $this->getHttpMock(array( |
|
34 | 34 | 'query' => 'FooBar', |
35 | 35 | 'format' => 'json' |
36 | - ) ); |
|
36 | + )); |
|
37 | 37 | |
38 | - $queryExecuter = new QueryExecuter( 'test.example.com', array(), $http ); |
|
39 | - $result = $queryExecuter->execute( 'FooBar' ); |
|
38 | + $queryExecuter = new QueryExecuter('test.example.com', array(), $http); |
|
39 | + $result = $queryExecuter->execute('FooBar'); |
|
40 | 40 | |
41 | - $this->assertEquals( '~=[,,_,,]:3', $result ); |
|
41 | + $this->assertEquals('~=[,,_,,]:3', $result); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | public function testExecuteCustomParams() { |
45 | - $http = $this->getHttpMock( array( |
|
45 | + $http = $this->getHttpMock(array( |
|
46 | 46 | 'fancy-query' => 'FooBar', |
47 | 47 | 'format-nyan' => 'json' |
48 | - ) ); |
|
48 | + )); |
|
49 | 49 | |
50 | - $queryExecuter = new QueryExecuter( 'test.example.com', array( |
|
50 | + $queryExecuter = new QueryExecuter('test.example.com', array( |
|
51 | 51 | 'queryParam' => 'fancy-query', |
52 | 52 | 'formatParam' => 'format-nyan' |
53 | - ), $http ); |
|
53 | + ), $http); |
|
54 | 54 | |
55 | - $result = $queryExecuter->execute( 'FooBar' ); |
|
55 | + $result = $queryExecuter->execute('FooBar'); |
|
56 | 56 | |
57 | - $this->assertEquals( '~=[,,_,,]:3', $result ); |
|
57 | + $this->assertEquals('~=[,,_,,]:3', $result); |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | public function testExecuteQueryBuilder() { |
61 | - $http = $this->getHttpMock( array( |
|
61 | + $http = $this->getHttpMock(array( |
|
62 | 62 | 'query' => 'SELECT * WHERE { }', |
63 | 63 | 'format' => 'json' |
64 | - ) ); |
|
64 | + )); |
|
65 | 65 | |
66 | - $queryExecuter = new QueryExecuter( 'test.example.com', array(), $http ); |
|
67 | - $result = $queryExecuter->execute( new QueryBuilder() ); |
|
66 | + $queryExecuter = new QueryExecuter('test.example.com', array(), $http); |
|
67 | + $result = $queryExecuter->execute(new QueryBuilder()); |
|
68 | 68 | |
69 | - $this->assertEquals( '~=[,,_,,]:3', $result ); |
|
69 | + $this->assertEquals('~=[,,_,,]:3', $result); |
|
70 | 70 | } |
71 | 71 | |
72 | 72 | public function testExecuteInvalidArgument() { |
73 | - $queryExecuter = new QueryExecuter( 'test.example.com' ); |
|
74 | - $this->setExpectedException( 'InvalidArgumentException' ); |
|
73 | + $queryExecuter = new QueryExecuter('test.example.com'); |
|
74 | + $this->setExpectedException('InvalidArgumentException'); |
|
75 | 75 | |
76 | - $queryExecuter->execute( null ); |
|
76 | + $queryExecuter->execute(null); |
|
77 | 77 | } |
78 | 78 | |
79 | 79 | } |