Completed
Push — master ( 69614a...9cb597 )
by mw
14s
created

Highlighter   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 248
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
dl 0
loc 248
ccs 100
cts 105
cp 0.9524
rs 9.3999
c 0
b 0
f 0
wmc 33
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A factory() 0 7 3
A getHtml() 0 4 1
A setContent() 0 8 1
D getTypeId() 0 25 10
B getContainer() 0 35 6
C getTypeConfiguration() 0 57 11
1
<?php
2
3
namespace SMW;
4
5
use Html;
6
use SMWOutputs;
7
8
/**
9
 * Highlighter utility function for Semantic MediaWiki
10
 *
11
 * @license GNU GPL v2+
12
 * @since   1.9
13
 *
14
 * @author mwjames
15
 */
16
class Highlighter {
17
18
	// Highlighter ID for no types
19
	const TYPE_NOTYPE    = 0;
20
	// Highlighter ID for properties
21
	const TYPE_PROPERTY  = 1;
22
	// Highlighter ID for text
23
	const TYPE_TEXT      = 2;
24
	// Highlighter ID for quantities
25
	const TYPE_QUANTITY  = 3;
26
	//  Highlighter ID for warnings
27
	const TYPE_WARNING   = 4;
28
	//  Highlighter ID for informations
29
	const TYPE_INFO      = 5;
30
	//  Highlighter ID for help
31
	const TYPE_HELP      = 6;
32
	//  Highlighter ID for notes
33
	const TYPE_NOTE      = 7;
34
	//  Highlighter ID for service links
35
	const TYPE_SERVICE   = 8;
36
	const TYPE_REFERENCE = 9;
37
38
	/**
39
	 * @var array $options
40
	 */
41
	private $options;
42
43
	/**
44
	 * @var int $type
45
	 */
46
	private $type;
47
48
	/**
49
	 * @var string|null
50
	 */
51
	private $language = null;
52
53
	/**
54
	 * @since 1.9
55
	 *
56
	 * @param int $type
57
	 * @param string|null $language
58
	 */
59 78
	public function __construct( $type, $language = null ) {
60 78
		$this->type = $type;
61 78
		$this->language = $language;
62 78
	}
63
64
	/**
65
	 * @since 1.9
66
	 *
67
	 * @param string|int $type
68
	 * @param string|null $language
69
	 *
70
	 * @return Highlighter
71
	 */
72 78
	public static function factory( $type, $language = null ) {
73 78
		if ( $type === '' || !is_int( $type ) ) {
74 52
			$type = self::getTypeId( $type );
75
		}
76
77 78
		return new Highlighter( $type, $language );
78
	}
79
80
	/**
81
	 * Returns html output
82
	 *
83
	 * @since 1.9
84
	 *
85
	 * @return string
86
	 */
87 65
	public function getHtml() {
88 65
		SMWOutputs::requireResource( 'ext.smw.tooltips' );
89 65
		return $this->getContainer();
90
	}
91
92
	/**
93
	 * Set content
94
	 *
95
	 * An external class that invokes content via setContent has to ensure
96
	 * that all data are appropriately escaped.
97
	 *
98
	 * @since 1.9
99
	 *
100
	 * @param array $content
101
	 *
102
	 * @return array
103
	 */
104 65
	public function setContent( array $content ) {
105
		/**
106
		 * @var $content
107
		 * $content['caption'] = a text or null
108
		 * $content['context'] = a text or null
109
		 */
110 65
		return $this->options = array_merge( $this->getTypeConfiguration( $this->type ), $content );
111
	}
112
113
	/**
114
	 * Returns type id
115
	 *
116
	 * @since 1.9
117
	 *
118
	 * @param string $type
119
	 *
120
	 * @return integer
121
	 */
122 65
	public static function getTypeId( $type ) {
123
		// TODO: why do we have a htmlspecialchars here?!
124 65
		switch ( strtolower ( htmlspecialchars ( $type ) ) ) {
125 65
			case 'property':
126 6
			return self::TYPE_PROPERTY;
127 59
			case 'text':
128 3
			return self::TYPE_TEXT;
129 56
			case 'quantity':
130 3
			return self::TYPE_QUANTITY;
131 53
			case 'warning':
132 31
			return self::TYPE_WARNING;
133 23
			case 'info':
134 3
			return self::TYPE_INFO;
135 20
			case 'help':
136 3
			return self::TYPE_HELP;
137 17
			case 'note':
138 4
			return self::TYPE_NOTE;
139 13
			case 'service':
140 3
			return self::TYPE_SERVICE;
141 10
			case 'reference':
142
			return self::TYPE_REFERENCE;
143
			default:
144 10
			return self::TYPE_NOTYPE;
145
		}
146
	}
147
148
	/**
149
	 * Builds Html container
150
	 *
151
	 * Content that is being invoked has to be escaped
152
	 * @see Highlighter::setContent
153
	 *
154
	 * @since 1.9
155
	 *
156
	 * @return string
157
	 */
158 65
	private function getContainer() {
159
160 65
		$captionclass = $this->options['captionclass'];
161
162
		// 2.4+ can display context for user-defined properties, here we ensure
163
		// to keep the style otherwise it displays italic which is by convention
164
		// reserved for predefined properties
165 65
		if ( $this->type === self::TYPE_PROPERTY && isset( $this->options['userDefined'] ) ) {
166 14
			$captionclass = $this->options['userDefined'] ? 'smwtext' : $captionclass;
167
		}
168
169 65
		$language = is_string( $this->language ) ? $this->language : Message::USER_LANGUAGE;
170
171 65
		return Html::rawElement(
172 65
			'span',
173
			array(
174 65
				'class'        => 'smw-highlighter',
175 65
				'data-type'    => $this->options['type'],
176 65
				'data-content' => isset( $this->options['data-content'] ) ? $this->options['data-content'] : null,
177 65
				'data-state'   => $this->options['state'],
178 65
				'data-title'   => Message::get( $this->options['title'], Message::TEXT, $language ),
179 65
				'title'        => strip_tags( htmlspecialchars_decode( $this->options['content'] ) )
180 65
			), Html::rawElement(
181 65
					'span',
182
					array(
183 65
						'class' => $captionclass
184 65
					), $this->options['caption']
185 65
				) . Html::rawElement(
186 65
					'div',
187
					array(
188
						'class' => 'smwttcontent'
189 65
					), $this->options['content']
190
				)
191
			);
192
	}
193
194
	/**
195
	 * Returns initial configuation settings
196
	 *
197
	 * @note You could create a class per entity type but does this
198
	 * really make sense just to get some configuration parameters?
199
	 *
200
	 * @since 1.9
201
	 *
202
	 * @param string $type
203
	 *
204
	 * @return array
205
	 */
206 65
	private function getTypeConfiguration( $type ) {
207 65
		$settings = array();
208 65
		$settings['type'] = $type;
209 65
		$settings['caption'] = '';
210 65
		$settings['content'] = '';
211
212
		switch ( $type ) {
213 65
			case self::TYPE_PROPERTY:
214 17
				$settings['state'] = 'inline';
215 17
				$settings['title'] = 'smw-ui-tooltip-title-property';
216 17
				$settings['captionclass'] = 'smwbuiltin';
217 17
				break;
218 53
			case self::TYPE_TEXT:
219 1
				$settings['state'] = 'persistent';
220 1
				$settings['title'] = 'smw-ui-tooltip-title-info';
221 1
				$settings['captionclass'] = 'smwtext';
222 1
				break;
223 52
			case self::TYPE_QUANTITY:
224 17
				$settings['state'] = 'inline';
225 17
				$settings['title'] = 'smw-ui-tooltip-title-quantity';
226 17
				$settings['captionclass'] = 'smwtext';
227 17
				break;
228 36
			case self::TYPE_NOTE:
229 2
				$settings['state'] = 'inline';
230 2
				$settings['title'] = 'smw-ui-tooltip-title-note';
231 2
				$settings['captionclass'] = 'smwtticon note';
232 2
				break;
233 35
			case self::TYPE_WARNING:
234 29
				$settings['state'] = 'inline';
235 29
				$settings['title'] = 'smw-ui-tooltip-title-warning';
236 29
				$settings['captionclass'] = 'smwtticon warning';
237 29
				break;
238 6
			case self::TYPE_SERVICE:
239 1
				$settings['state'] = 'persistent';
240 1
				$settings['title'] = 'smw-ui-tooltip-title-service';
241 1
				$settings['captionclass'] = 'smwtticon service';
242 1
				break;
243 5
			case self::TYPE_REFERENCE:
244
				$settings['state'] = 'persistent';
245
				$settings['title'] = 'smw-ui-tooltip-title-reference';
246
				$settings['captionclass'] = 'smwtext';
247
				break;
248 5
			case self::TYPE_HELP:
249 4
			case self::TYPE_INFO:
250 2
				$settings['state'] = 'persistent';
251 2
				$settings['title'] = 'smw-ui-tooltip-title-info';
252 2
				$settings['captionclass'] = 'smwtticon info';
253 2
				break;
254 3
			case self::TYPE_NOTYPE:
255
			default:
256 3
				$settings['state'] = 'persistent';
257 3
				$settings['title'] = 'smw-ui-tooltip-title-info';
258 3
				$settings['captionclass'] = 'smwbuiltin';
259
		};
260
261 65
		return $settings;
262
	}
263
}
264