@@ -74,19 +74,19 @@ discard block |
||
74 | 74 | * @param int $options |
75 | 75 | * @throws InvalidArgumentException |
76 | 76 | */ |
77 | - public function validate( $expression, $options ) { |
|
78 | - if ( !is_string( $expression ) ) { |
|
79 | - throw new InvalidArgumentException( '$expression has to be a string.' ); |
|
77 | + public function validate($expression, $options) { |
|
78 | + if (!is_string($expression)) { |
|
79 | + throw new InvalidArgumentException('$expression has to be a string.'); |
|
80 | 80 | } |
81 | 81 | |
82 | - if ( !$this->matches( $expression, $options ) ) { |
|
83 | - throw new InvalidArgumentException( '$expression has to be a ' . |
|
84 | - implode( ' or a ', $this->getOptionNames( $options ) ) . ', got ' . $expression |
|
82 | + if (!$this->matches($expression, $options)) { |
|
83 | + throw new InvalidArgumentException('$expression has to be a ' . |
|
84 | + implode(' or a ', $this->getOptionNames($options)) . ', got ' . $expression |
|
85 | 85 | ); |
86 | 86 | } |
87 | 87 | } |
88 | 88 | |
89 | - private function getOptionNames( $options ) { |
|
89 | + private function getOptionNames($options) { |
|
90 | 90 | $names = array( |
91 | 91 | 'variable' => self::VALIDATE_VARIABLE, |
92 | 92 | 'IRI' => self::VALIDATE_IRI, |
@@ -98,69 +98,69 @@ discard block |
||
98 | 98 | 'function with variable assignment' => self::VALIDATE_FUNCTION_AS |
99 | 99 | ); |
100 | 100 | |
101 | - $names = array_filter( $names, function( $key ) use ( $options ) { |
|
101 | + $names = array_filter($names, function($key) use ($options) { |
|
102 | 102 | return $options & $key; |
103 | 103 | } ); |
104 | 104 | |
105 | - return array_keys( $names ); |
|
105 | + return array_keys($names); |
|
106 | 106 | } |
107 | 107 | |
108 | - private function matches( $expression, $options ) { |
|
109 | - return $this->isVariable( $expression, $options ) || |
|
110 | - $this->isIRI( $expression, $options ) || |
|
111 | - $this->isPrefix( $expression, $options ) || |
|
112 | - $this->isPrefixedIri( $expression, $options ) || |
|
113 | - $this->isValue( $expression, $options ) || |
|
114 | - $this->isPath( $expression, $options ) || |
|
115 | - $this->isFunction( $expression, $options ) || |
|
116 | - $this->isFunctionAs( $expression, $options ); |
|
108 | + private function matches($expression, $options) { |
|
109 | + return $this->isVariable($expression, $options) || |
|
110 | + $this->isIRI($expression, $options) || |
|
111 | + $this->isPrefix($expression, $options) || |
|
112 | + $this->isPrefixedIri($expression, $options) || |
|
113 | + $this->isValue($expression, $options) || |
|
114 | + $this->isPath($expression, $options) || |
|
115 | + $this->isFunction($expression, $options) || |
|
116 | + $this->isFunctionAs($expression, $options); |
|
117 | 117 | } |
118 | 118 | |
119 | - private function isVariable( $expression, $options ) { |
|
119 | + private function isVariable($expression, $options) { |
|
120 | 120 | return $options & self::VALIDATE_VARIABLE && |
121 | - $this->regexHelper->matchesRegex( '\{variable}', $expression ); |
|
121 | + $this->regexHelper->matchesRegex('\{variable}', $expression); |
|
122 | 122 | } |
123 | 123 | |
124 | - private function isIRI( $expression, $options ) { |
|
124 | + private function isIRI($expression, $options) { |
|
125 | 125 | return $options & self::VALIDATE_IRI && |
126 | - $this->regexHelper->matchesRegex( '\{iri}', $expression ); |
|
126 | + $this->regexHelper->matchesRegex('\{iri}', $expression); |
|
127 | 127 | } |
128 | 128 | |
129 | - private function isPrefix( $expression, $options ) { |
|
129 | + private function isPrefix($expression, $options) { |
|
130 | 130 | return $options & self::VALIDATE_PREFIX && |
131 | - $this->regexHelper->matchesRegex( '\{prefix}', $expression ); |
|
131 | + $this->regexHelper->matchesRegex('\{prefix}', $expression); |
|
132 | 132 | } |
133 | 133 | |
134 | - private function isPrefixedIri( $expression, $options ) { |
|
134 | + private function isPrefixedIri($expression, $options) { |
|
135 | 135 | return $options & self::VALIDATE_PREFIXED_IRI && |
136 | - $this->regexHelper->matchesRegex( '\{prefixed_iri}', $expression ); |
|
136 | + $this->regexHelper->matchesRegex('\{prefixed_iri}', $expression); |
|
137 | 137 | } |
138 | 138 | |
139 | - private function isValue( $expression, $options ) { |
|
139 | + private function isValue($expression, $options) { |
|
140 | 140 | return $options & self::VALIDATE_NATIVE && |
141 | - $this->regexHelper->matchesRegex( '\{native}', $expression ); |
|
141 | + $this->regexHelper->matchesRegex('\{native}', $expression); |
|
142 | 142 | } |
143 | 143 | |
144 | - private function isPath( $expression, $options ) { |
|
144 | + private function isPath($expression, $options) { |
|
145 | 145 | return $options & self::VALIDATE_PATH && |
146 | - $this->regexHelper->matchesRegex( '\{path}', $expression ); |
|
146 | + $this->regexHelper->matchesRegex('\{path}', $expression); |
|
147 | 147 | } |
148 | 148 | |
149 | - private function isFunction( $expression, $options ) { |
|
149 | + private function isFunction($expression, $options) { |
|
150 | 150 | // @todo this might not be complete |
151 | 151 | return $options & self::VALIDATE_FUNCTION && |
152 | - $this->regexHelper->matchesRegex( '\{function}', $expression ) && |
|
153 | - $this->checkBrackets( $expression ); |
|
152 | + $this->regexHelper->matchesRegex('\{function}', $expression) && |
|
153 | + $this->checkBrackets($expression); |
|
154 | 154 | } |
155 | 155 | |
156 | - private function checkBrackets( $expression ) { |
|
157 | - $expression = $this->regexHelper->escapeSequences( $expression ); |
|
158 | - return substr_count( $expression, '(' ) === substr_count( $expression, ')' ); |
|
156 | + private function checkBrackets($expression) { |
|
157 | + $expression = $this->regexHelper->escapeSequences($expression); |
|
158 | + return substr_count($expression, '(') === substr_count($expression, ')'); |
|
159 | 159 | } |
160 | 160 | |
161 | - private function isFunctionAs( $expression, $options ) { |
|
161 | + private function isFunctionAs($expression, $options) { |
|
162 | 162 | return $options & self::VALIDATE_FUNCTION_AS && |
163 | - $this->regexHelper->matchesRegex( '\(\{function} AS \{variable}\)', $expression ); |
|
163 | + $this->regexHelper->matchesRegex('\(\{function} AS \{variable}\)', $expression); |
|
164 | 164 | } |
165 | 165 | |
166 | 166 | } |
@@ -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 | } |