QueryPrefixBuilder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 9
Bugs 1 Features 3
Metric Value
wmc 5
c 9
b 1
f 3
lcom 1
cbo 2
dl 0
loc 62
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setPrefixes() 0 10 2
A getPrefixes() 0 3 1
A getSPARQL() 0 5 1
1
<?php
2
3
namespace Asparagus;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Package-private class to build the prefixes of a SPARQL query.
9
 *
10
 * @license GNU GPL v2+
11
 * @author Bene* < [email protected] >
12
 */
13
class QueryPrefixBuilder {
14
15
	/**
16
	 * @var string[] map of prefixes to IRIs
17
	 */
18
	private $prefixes = array();
19
20
	/**
21
	 * @var ExpressionValidator
22
	 */
23
	private $expressionValidator;
24
25
	/**
26
	 * @var UsageValidator
27
	 */
28
	private $usageValidator;
29
30
	/**
31
	 * @param string[] $prefixes
32
	 */
33
	public function __construct( array $prefixes, UsageValidator $usageValidator ) {
34
		$this->expressionValidator = new ExpressionValidator();
35
		$this->usageValidator = $usageValidator;
36
		$this->setPrefixes( $prefixes );
37
	}
38
39
	/**
40
	 * Sets the prefixes for the given IRIs.
41
	 *
42
	 * @param string[] $prefixes
43
	 * @throws InvalidArgumentException
44
	 */
45
	private function setPrefixes( array $prefixes ) {
46
		foreach ( $prefixes as $prefix => $iri ) {
47
			$this->expressionValidator->validate( $prefix, ExpressionValidator::VALIDATE_PREFIX );
48
			$this->expressionValidator->validate( $iri, ExpressionValidator::VALIDATE_IRI );
49
50
			$this->prefixes[$prefix] = $iri;
51
		}
52
53
		$this->usageValidator->trackDefinedPrefixes( array_keys( $this->prefixes ) );
54
	}
55
56
	/**
57
	 * @return string[]
58
	 */
59
	public function getPrefixes() {
60
		return $this->prefixes;
61
	}
62
63
	/**
64
	 * Returns the plain SPARQL string of these prefixes.
65
	 *
66
	 * @return string
67
	 */
68
	public function getSPARQL() {
69
		return implode( array_map( function( $prefix, $iri ) {
70
			return 'PREFIX ' . $prefix . ': <' . $iri . '> '; 
71
		}, array_keys( $this->prefixes ), $this->prefixes ) );
72
	}
73
74
}
75