summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Services/DataValueServiceFactory.php
blob: bcba0db85582cb556182611d7f94b0a37f98a2e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php

namespace SMW\Services;

use Onoi\CallbackContainer\ContainerBuilder;
use SMW\DataValueFactory;
use SMW\DataValues\InfoLinksProvider;
use SMW\DataValues\StringValue;
use SMW\DataValues\ValueFormatters\DispatchingDataValueFormatter;
use SMW\DataValues\ValueFormatters\NoValueFormatter;
use SMW\DataValues\ValueFormatters\ValueFormatter;
use SMW\DataValues\ValueParsers\ValueParser;
use SMW\DataValues\ValueValidators\ConstraintValueValidator;
use SMW\PropertyRestrictionExaminer;
use SMW\PropertySpecificationLookup;
use SMWDataValue as DataValue;
use SMWNumberValue as NumberValue;
use SMWTimeValue as TimeValue;

/**
 * @private
 *
 * This class provides service and factory functions for DataValue objects and
 * are only to be used for those objects.
 *
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class DataValueServiceFactory {

	/**
	 * Indicates a DataValue service
	 */
	const SERVICE_FILE = 'DataValueServices.php';

	/**
	 * Indicates a DataValue service
	 */
	const TYPE_INSTANCE = '__dv.';

	/**
	 * Indicates a ValueParser service
	 */
	const TYPE_PARSER = '__dv.parser.';

	/**
	 * Indicates a ValueFormatter service
	 */
	const TYPE_FORMATTER = '__dv.formatter.';

	/**
	 * Indicates a ValueValidator service
	 */
	const TYPE_VALIDATOR = '__dv.validator.';

	/**
	 * Extraneous service
	 */
	const TYPE_EXT_FUNCTION = '__dv.ext.func.';

	/**
	 * @var ContainerBuilder
	 */
	private $containerBuilder;

	/**
	 * @var DispatchingDataValueFormatter
	 */
	private $dispatchingDataValueFormatter = null;

	/**
	 * @since 2.5
	 */
	public function __construct( ContainerBuilder $containerBuilder ) {
		$this->containerBuilder = $containerBuilder;
	}

	/**
	 * @since 2.5
	 *
	 * @param DataValue $dataValue
	 *
	 * @return InfoLinksProvider
	 */
	public function newInfoLinksProvider( DataValue $dataValue ) {
		return new InfoLinksProvider( $dataValue, $this->getPropertySpecificationLookup() );
	}

	/**
	 * @since 3.0
	 *
	 * @return DataValueFactory
	 */
	public function getDataValueFactory() {
		return DataValueFactory::getInstance();
	}

	/**
	 * Imported functions registered with DataTypeRegistry::registerExtraneousFunction
	 *
	 * @since 2.5
	 *
	 * @param array $extraneousFunctions
	 */
	public function importExtraneousFunctions( array $extraneousFunctions ) {
		foreach ( $extraneousFunctions as $serviceName => $calllback ) {
			$this->containerBuilder->registerCallback( self::TYPE_EXT_FUNCTION . $serviceName, $calllback );
		}
	}

	/**
	 * @since 2.5
	 *
	 * @param string $serviceName
	 *
	 * @return mixed
	 */
	public function newExtraneousFunctionByName( $serviceName ) {
		return $this->containerBuilder->create( self::TYPE_EXT_FUNCTION . $serviceName );
	}

	/**
	 * @since 2.5
	 *
	 * @param string $typeId
	 * @param string $class
	 *
	 * @return DataValue
	 */
	public function newDataValueByType( $typeId, $class ) {

		if ( $this->containerBuilder->isRegistered( self::TYPE_INSTANCE . $typeId ) ) {
			return $this->containerBuilder->create( self::TYPE_INSTANCE . $typeId );
		}

		// Legacy invocation, for those that have not been defined yet!s
		return new $class( $typeId );
	}

	/**
	 * @since 2.5
	 *
	 * @param DataValue $dataValue
	 *
	 * @return ValueParser
	 */
	public function getValueParser( DataValue $dataValue ) {
		return $this->containerBuilder->singleton( self::TYPE_PARSER . $dataValue->getTypeID() );
	}

	/**
	 * @since 2.5
	 *
	 * @param DataValue $dataValue
	 *
	 * @return ValueFormatter
	 */
	public function getValueFormatter( DataValue $dataValue ) {

		$id = self::TYPE_FORMATTER . $dataValue->getTypeID();

		if ( $this->containerBuilder->isRegistered( $id ) ) {
			$dataValueFormatter = $this->containerBuilder->singleton( $id );
		} else {
			$dataValueFormatter = $this->getDispatchableValueFormatter( $dataValue );
		}

		$dataValueFormatter->setDataValue(
			$dataValue
		);

		return $dataValueFormatter;
	}

	/**
	 * @since 2.5
	 *
	 * @return ConstraintValueValidator
	 */
	public function getConstraintValueValidator() {
		return $this->containerBuilder->singleton( self::TYPE_VALIDATOR . 'CompoundConstraintValueValidator' );
	}

	/**
	 * @since 2.5
	 *
	 * @return PropertySpecificationLookup
	 */
	public function getPropertySpecificationLookup() {
		return $this->containerBuilder->singleton( 'PropertySpecificationLookup' );
	}

	/**
	 * @since 3.0
	 *
	 * @return PropertyRestrictionExaminer
	 */
	public function getPropertyRestrictionExaminer() {

		$propertyRestrictionExaminer = $this->containerBuilder->singleton( 'PropertyRestrictionExaminer' );

		$propertyRestrictionExaminer->setUser(
			$GLOBALS['wgUser']
		);

		return $propertyRestrictionExaminer;
	}

	private function getDispatchableValueFormatter( $dataValue ) {

		if ( $this->dispatchingDataValueFormatter === null ) {
			$this->dispatchingDataValueFormatter = $this->newDispatchingDataValueFormatter();
		}

		return $this->dispatchingDataValueFormatter->getDataValueFormatterFor( $dataValue );
	}

	private function newDispatchingDataValueFormatter() {

		$dispatchingDataValueFormatter = new DispatchingDataValueFormatter();

		// To be checked only after DispatchingDataValueFormatter::addDataValueFormatter did
		// not match any previous registered DataValueFormatters
		$dispatchingDataValueFormatter->addDefaultDataValueFormatter(
			$this->containerBuilder->singleton( self::TYPE_FORMATTER . StringValue::TYPE_ID )
		);

		$dispatchingDataValueFormatter->addDefaultDataValueFormatter(
			$this->containerBuilder->singleton( self::TYPE_FORMATTER . NumberValue::TYPE_ID )
		);

		$dispatchingDataValueFormatter->addDefaultDataValueFormatter(
			$this->containerBuilder->singleton( self::TYPE_FORMATTER . TimeValue::TYPE_ID )
		);

		$dispatchingDataValueFormatter->addDefaultDataValueFormatter( new NoValueFormatter() );

		return $dispatchingDataValueFormatter;
	}

}