Completed
Push — master ( 829da2...8585d5 )
by David
03:05
created

Wordlift_Sparql_Service::format()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 42
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 17
nc 8
nop 2
dl 0
loc 42
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Define the Wordlift_Sparql_Service class.
5
 */
6
7
/**
8
 * The Wordlift_Sparql_Service class provides functions related to SPARQL queries.
9
 *
10
 * @since 3.6.0
11
 */
12
class Wordlift_Sparql_Service {
13
14
	/**
15
	 * A {@link Wordlift_Log_Service} instance.
16
	 *
17
	 * @since 3.6.0
18
	 * @access private
19
	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
20
	 */
21
	private $log;
22
23
	/**
24
	 * The {@link Wordlift_Sparql_Service} singleton instance.
25
	 *
26
	 * @since 3.6.0
27
	 * @access private
28
	 * @var \Wordlift_Sparql_Service $instance The {@link Wordlift_Sparql_Service} singleton instance.
29
	 */
30
	private static $instance;
31
32
	/**
33
	 * Create a {@link Wordlift_Sparql_Service} instance.
34
	 *
35
	 * @since 3.6.0
36
	 */
37
	public function __construct() {
38
39
		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Sparql_Service' );
40
41
		self::$instance = $this;
42
43
	}
44
45
	/**
46
	 * Get the singleton instance of the {@link Wordlift_Sparql_Service}.
47
	 *
48
	 * @since 3.6.0
49
	 * @return \Wordlift_Sparql_Service
50
	 */
51
	public static function get_instance() {
52
53
		return self::$instance;
54
	}
55
56
	/**
57
	 * Queue a SPARQL statement for execution.
58
	 *
59
	 * @since 3.6.0
60
	 *
61
	 * @param string $stmt The SPARQL statement.
62
	 */
63
	public function queue( $stmt ) {
64
65
		rl_execute_sparql_update_query( $stmt );
66
67
	}
68
69
	/**
70
	 * Formats the provided value according to the specified type in order to
71
	 * insert the value using SPARQL. The value is also escaped.
72
	 *
73
	 * @since 3.6.0
74
	 *
75
	 * @param string $value The value.
76
	 * @param string $type The value type.
77
	 *
78
	 * @return string The formatted value for SPARQL statements.
79
	 */
80
	public function format( $value, $type ) {
81
82
		// see https://www.w3.org/TR/sparql11-query/.
83
84
		switch ( $type ) {
85
86
			case Wordlift_Schema_Service::DATA_TYPE_BOOLEAN:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
87
88
				// SPARQL supports 'true' and 'false', so we evaluate the $value
89
				// and return true/false accordingly.
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
90
				return $value ? 'true' : 'false';
91
92
			case Wordlift_Schema_Service::DATA_TYPE_DATE:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
93
94
				return sprintf( '"%s"^^xsd:date', self::escape( $value ) );
95
96
97
			case Wordlift_Schema_Service::DATA_TYPE_DOUBLE:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
98
99
				return sprintf( '"%s"^^xsd:double', self::escape( $value ) );
100
101
			case Wordlift_Schema_Service::DATA_TYPE_INTEGER:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
102
103
				return sprintf( '"%s"^^xsd:integer', self::escape( $value ) );
104
105
			case Wordlift_Schema_Service::DATA_TYPE_STRING:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
106
107
				return sprintf( '"%s"^^xsd:string', self::escape( $value ) );
108
109
			case Wordlift_Schema_Service::DATA_TYPE_URI:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
110
111
				return sprintf( '<%s>', self::escape_uri( $value ) );
112
113
			default:
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
114
115
				$this->log->warn( "Unknown data type [ type :: $type ]" );
116
117
				// Try to insert the value anyway.
118
				return sprintf( '"%s"', self::escape( $value ) );
119
		}
120
121
	}
122
123
	/**
124
	 * Escapes an URI for a SPARQL statement.
125
	 *
126
	 * @since 3.6.0
127
	 *
128
	 * @param string $uri The URI to escape.
129
	 *
130
	 * @return string The escaped URI.
131
	 */
132
	public static function escape_uri( $uri ) {
133
134
		// Should we validate the IRI?
135
		// http://www.w3.org/TR/sparql11-query/#QSynIRI
136
137
		$uri = str_replace( '<', '\<', $uri );
138
		$uri = str_replace( '>', '\>', $uri );
139
140
		return $uri;
141
	}
142
143
	/**
144
	 * Escapes a string for a SPARQL statement.
145
	 *
146
	 * @since 3.6.0
147
	 *
148
	 * @param string $string The string to escape.
149
	 *
150
	 * @return string The escaped string.
151
	 */
152
	public static function escape( $string ) {
153
154
		// see http://www.w3.org/TR/rdf-sparql-query/
155
		//    '\t'	U+0009 (tab)
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
156
		//    '\n'	U+000A (line feed)
157
		//    '\r'	U+000D (carriage return)
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
158
		//    '\b'	U+0008 (backspace)
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
159
		//    '\f'	U+000C (form feed)
160
		//    '\"'	U+0022 (quotation mark, double quote mark)
161
		//    "\'"	U+0027 (apostrophe-quote, single quote mark)
162
		//    '\\'	U+005C (backslash)
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
163
164
		$string = str_replace( '\\', '\\\\', $string );
165
		$string = str_replace( '\'', '\\\'', $string );
166
		$string = str_replace( '"', '\\"', $string );
167
		$string = str_replace( "\f", '\\f', $string );
168
		$string = str_replace( "\b", '\\b', $string );
169
		$string = str_replace( "\r", '\\r', $string );
170
		$string = str_replace( "\n", '\\n', $string );
171
		$string = str_replace( "\t", '\\t', $string );
172
173
		return $string;
174
	}
175
176
}
177