summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/includes/QueryPrinterFactory.php
blob: 3481405e54fab019c0a79138ea6fe83940641bf5 (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
<?php

namespace SMW;

use InvalidArgumentException;
use SMW\Query\Exception\ResultFormatNotFoundException;

/**
 * Factory for "result formats", ie classes implementing QueryResultPrinter.
 *
 * @license GNU GPL v2+
 * @since 2.5 (since 1.9, renamed in 2.5)
 *
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
final class QueryPrinterFactory {

	/**
	 * Returns the global instance of the factory.
	 *
	 * @since 2.5
	 *
	 * @return QueryPrinterFactory
	 */
	public static function singleton() {
		static $instance = null;

		if ( $instance === null ) {
			$instance = self::newFromGlobalState();
		}

		return $instance;
	}

	private static function newFromGlobalState() {
		$instance = new self();

		foreach ( $GLOBALS['smwgResultFormats'] as $formatName => $printerClass ) {
			$instance->registerFormat( $formatName, $printerClass );
		}

		foreach ( $GLOBALS['smwgResultAliases'] as $formatName => $aliases ) {
			$instance->registerAliases( $formatName, $aliases );
		}

		return $instance;
	}

	/**
	 * Format registry. Format names pointing to their associated QueryResultPrinter implementing classes.
	 *
	 * @var string[]
	 */
	private $formats = [];

	/**
	 * Form alias registry. Aliases pointing to their canonical format name.
	 *
	 * @var string[]
	 */
	private $aliases = [];

	/**
	 * Registers a format.
	 * If there is a format already with the provided name,
	 * it will be overridden with the newly provided data.
	 *
	 * @since 2.5
	 *
	 * @param string $formatName
	 * @param string $class
	 *
	 * @throws InvalidArgumentException
	 */
	public function registerFormat( $formatName, $class ) {

		if ( !is_string( $formatName ) ) {
			throw new InvalidArgumentException( 'Format names can only be of type string' );
		}

		if ( !is_string( $class ) ) {
			throw new InvalidArgumentException( 'Format class names can only be of type string' );
		}

		$this->formats[$formatName] = $class;
	}

	/**
	 * Registers the provided format aliases.
	 * If an aliases is already registered, it will
	 * be overridden with the newly provided data.
	 *
	 * @since 2.5
	 *
	 * @param string $formatName
	 * @param array $aliases
	 *
	 * @throws InvalidArgumentException
	 */
	public function registerAliases( $formatName, array $aliases ) {

		if ( !is_string( $formatName ) ) {
			throw new InvalidArgumentException( 'Format names can only be of type string' );
		}

		foreach ( $aliases as $alias ) {
			if ( !is_string( $alias ) ) {
				throw new InvalidArgumentException( 'Format aliases can only be of type string' );
			}

			$this->aliases[$alias] = $formatName;
		}
	}

	/**
	 * Returns the canonical format names.
	 *
	 * @since 2.5
	 *
	 * @return string[]
	 */
	public function getFormats() {
		return array_keys( $this->formats );
	}

	/**
	 * Returns if there is a format or format alias with the provided name.
	 *
	 * @since 2.5
	 *
	 * @param string $formatName Format name or alias
	 *
	 * @return boolean
	 */
	public function hasFormat( $formatName ) {
		$formatName = $this->getCanonicalName( $formatName );
		return array_key_exists( $formatName, $this->formats );
	}

	/**
	 * Returns a new instance of the handling result printer for the provided format.
	 *
	 * @since 2.5
	 *
	 * @param string $formatName
	 *
	 * @return QueryResultPrinter
	 * @throws ResultFormatNotFoundException
	 */
	public function getPrinter( $formatName ) {
		$class = $this->getPrinterClass( $formatName );
		return new $class( $formatName );
	}

	/**
	 * Returns the QueryResultPrinter implementing class that is the printer for the provided format.
	 *
	 * @param string $formatName Format name or alias
	 *
	 * @return string
	 * @throws ResultFormatNotFoundException
	 */
	private function getPrinterClass( $formatName ) {
		$formatName = $this->getCanonicalName( $formatName );

		if ( !array_key_exists( $formatName, $this->formats ) ) {
			throw new ResultFormatNotFoundException( 'Unknown format name "' . $formatName . '" has no associated printer class' );
		}

		return $this->formats[$formatName];
	}

	/**
	 * Resolves format aliases into their associated canonical format name.
	 *
	 * @since 2.5
	 *
	 * @param string $formatName Format name or alias
	 *
	 * @return string
	 * @throws InvalidArgumentException
	 */
	public function getCanonicalName( $formatName ) {

		if ( !is_string( $formatName ) ) {
			throw new InvalidArgumentException( 'Format names can only be of type string' );
		}

		if ( array_key_exists( $formatName, $this->aliases ) ) {
			$formatName = $this->aliases[$formatName];
		}

		return $formatName;
	}

}