IriFormatter::format()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 4
nc 5
nop 1
1
<?php
2
3
namespace ValueFormatters;
4
5
use DataValues\IriValue;
6
use InvalidArgumentException;
7
8
/**
9
 * Formatter for string values
10
 *
11
 * @since 0.1
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class IriFormatter extends ValueFormatterBase {
17
18
	/**
19
	 * Formats a StringValue data value
20
	 *
21
	 * @since 0.1
22
	 *
23
	 * @param mixed $dataValue value to format
24
	 *
25
	 * @return string
26
	 * @throws InvalidArgumentException
27
	 */
28
	public function format( $dataValue ) {
29
		if ( !( $dataValue instanceof IriValue ) ) {
30
			throw new InvalidArgumentException( 'DataValue is not a IriValue.' );
31
		}
32
33
		$formatted = $dataValue->getScheme() . ':'
34
			. $dataValue->getHierarchicalPart()
35
			. ( $dataValue->getQuery() === '' ? '' : '?' . $dataValue->getQuery() )
36
			. ( $dataValue->getFragment() === '' ? '' : '#' . $dataValue->getFragment() );
37
38
		return $formatted;
39
	}
40
41
}
42