Completed
Push — master ( 7776a4...8c5813 )
by mw
35:00
created

registerDataValueFormatter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 1
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace SMW\DataValues;
4
5
use SMW\DataValues\ValueFormatters\DispatchingDataValueFormatter;
6
use SMW\DataValues\ValueFormatters\MonolingualTextValueFormatter;
7
use SMW\DataValues\ValueFormatters\NoValueFormatter;
8
use SMW\DataValues\ValueFormatters\DataValueFormatter;
9
use SMWDataValue as DataValue;
10
11
/**
12
 * @license GNU GPL v2+
13
 * @since 2.4
14
 *
15
 * @author mwjames
16
 */
17
class ValueFormatterRegistry {
18
19
	/**
20
	 * @var ValueFormatterRegistry
21
	 */
22
	private static $instance = null;
23
24
	/**
25
	 * @var DispatchingDataValueFormatter
26
	 */
27
	private $dispatchingDataValueFormatter = null;
28
29
	/**
30
	 * @since 2.4
31
	 *
32
	 * @param DispatchingDataValueFormatter|null $dispatchingDataValueFormatter
33
	 */
34
	public function __construct( DispatchingDataValueFormatter $dispatchingDataValueFormatter = null ) {
35
		$this->dispatchingDataValueFormatter = $dispatchingDataValueFormatter;
36
	}
37
38
	/**
39
	 * @since 2.4
40
	 *
41
	 * @return self
42
	 */
43
	public static function getInstance() {
44
45
		if ( self::$instance === null ) {
46
			self::$instance = new self();
47
		}
48
49
		return self::$instance;
50
	}
51
52
	/**
53
	 * @since 2.4
54
	 */
55
	public static function clear() {
56
		self::$instance = null;
57
	}
58
59
	/**
60
	 * @note This allows extensions to inject their own DataValueFormatter
61
	 * without further violating SRP of the DataType or DataValue.
62
	 *
63
	 * @since 2.4
64
	 *
65
	 * @param DataValueFormatter $dataValueFormatter
66
	 */
67
	public function registerDataValueFormatter( DataValueFormatter $dataValueFormatter ) {
68
69
		if ( $this->dispatchingDataValueFormatter === null ) {
70
			$this->dispatchingDataValueFormatter = $this->newDispatchingDataValueFormatter();
71
		}
72
73
		return $this->dispatchingDataValueFormatter->addDataValueFormatter( $dataValueFormatter );
74
	}
75
76
	/**
77
	 * @since 2.4
78
	 *
79
	 * @param DataValue $dataValue
80
	 *
81
	 * @return DataValueFormatter
82
	 */
83
	public function getDataValueFormatterFor( DataValue $dataValue ) {
84
85
		if ( $this->dispatchingDataValueFormatter === null ) {
86
			$this->dispatchingDataValueFormatter = $this->newDispatchingDataValueFormatter();
87
		}
88
89
		return $this->dispatchingDataValueFormatter->getDataValueFormatterFor( $dataValue );
90
	}
91
92
	private function newDispatchingDataValueFormatter() {
93
94
		$dispatchingDataValueFormatter = new DispatchingDataValueFormatter();
95
		$dispatchingDataValueFormatter->addDataValueFormatter( new MonolingualTextValueFormatter() );
96
97
		// To be checked only after DispatchingDataValueFormatter::addDataValueFormatter did
98
		// not match any previous registered DataValueFormatters
99
		$dispatchingDataValueFormatter->addDefaultDataValueFormatter( new NoValueFormatter() );
100
101
		return $dispatchingDataValueFormatter;
102
	}
103
104
}
105