@@ -67,8 +67,8 @@ discard block |
||
67 | 67 | * @param string $expression |
68 | 68 | * @return bool |
69 | 69 | */ |
70 | - public function matchesRegex( $regex, $expression ) { |
|
71 | - return preg_match( '/^' . $this->resolveMagic( $regex ) . '$/i', $expression ) === 1; |
|
70 | + public function matchesRegex($regex, $expression) { |
|
71 | + return preg_match('/^' . $this->resolveMagic($regex) . '$/i', $expression) === 1; |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
@@ -80,12 +80,12 @@ discard block |
||
80 | 80 | * @param int $group |
81 | 81 | * @return string[] |
82 | 82 | */ |
83 | - public function getMatches( $regex, $expression, $group = 1 ) { |
|
84 | - if ( preg_match_all( |
|
85 | - '/' . $this->resolveMagic( $regex ) . '/', |
|
86 | - $this->escapeSequences( $expression ), |
|
83 | + public function getMatches($regex, $expression, $group = 1) { |
|
84 | + if (preg_match_all( |
|
85 | + '/' . $this->resolveMagic($regex) . '/', |
|
86 | + $this->escapeSequences($expression), |
|
87 | 87 | $matches |
88 | - ) ) { |
|
88 | + )) { |
|
89 | 89 | return $matches[$group]; |
90 | 90 | } |
91 | 91 | |
@@ -99,14 +99,14 @@ discard block |
||
99 | 99 | * @param string[] $replacements |
100 | 100 | * @return string |
101 | 101 | */ |
102 | - public function escapeSequences( $expression, &$replacements = null ) { |
|
102 | + public function escapeSequences($expression, &$replacements = null) { |
|
103 | 103 | $replacements = array(); |
104 | 104 | // @todo this is not completely safe but works in most cases |
105 | 105 | // @todo for strings use http://stackoverflow.com/questions/171480/regex-grabbing-values-between-quotation-marks |
106 | 106 | return preg_replace_callback( |
107 | 107 | '/("([^\"]*)"|\<([^\>]*)\>)/', |
108 | - function( $match ) use ( &$replacements ) { |
|
109 | - $key = '<' . md5( $match[0] ) . '>'; |
|
108 | + function($match) use (&$replacements) { |
|
109 | + $key = '<' . md5($match[0]) . '>'; |
|
110 | 110 | $replacements[$key] = $match[0]; |
111 | 111 | return $key; |
112 | 112 | }, |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | ); |
115 | 115 | } |
116 | 116 | |
117 | - private function resolveMagic( $regex ) { |
|
117 | + private function resolveMagic($regex) { |
|
118 | 118 | $magics = array( |
119 | 119 | '\{variable}' => self::$variable, |
120 | 120 | '\{iri}' => self::$iri, |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | '\{function}' => $this->getFunctionRegex() |
127 | 127 | ); |
128 | 128 | |
129 | - return strtr( $regex, $magics ); |
|
129 | + return strtr($regex, $magics); |
|
130 | 130 | } |
131 | 131 | |
132 | 132 | private function getPrefixedIriRegex() { |
@@ -139,8 +139,8 @@ discard block |
||
139 | 139 | } |
140 | 140 | |
141 | 141 | private function getFunctionRegex() { |
142 | - $allowed = array_merge( self::$functions, array( '\<' . self::$iri . '\>', self::$prefix . ':', self::$variable, '!' ) ); |
|
143 | - return '(' . implode( '|', $allowed ) . ').*'; |
|
142 | + $allowed = array_merge(self::$functions, array('\<' . self::$iri . '\>', self::$prefix . ':', self::$variable, '!')); |
|
143 | + return '(' . implode('|', $allowed) . ').*'; |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | } |
@@ -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 | } |