Completed
Push — master ( 59c431...219a45 )
by mw
36:38
created

findPreferredPropertyLabelByLanguageCode()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 3
nop 2
dl 0
loc 21
ccs 6
cts 6
cp 1
crap 5
rs 8.7624
c 0
b 0
f 0
1
<?php
2
3
namespace SMW;
4
5
/**
6
 * @license GNU GPL v2+
7
 * @since 2.2
8
 *
9
 * @author mwjames
10
 */
11
class PropertyLabelFinder {
12
13
	/**
14
	 * @var Store
15
	 */
16
	private $store;
17
18
	/**
19
	 * Array with entries "property id" => "property label"
20
	 *
21
	 * @var string[]
22
	 */
23
	private $languageDependentPropertyLabels = array();
24
25
	/**
26
	 * Array with entries "property label" => "property id"
27
	 *
28
	 * @var string[]
29
	 */
30
	private $canonicalPropertyLabels = array();
31
32
	/**
33
	 * @since 2.2
34
	 *
35
	 * @param Store $store
36
	 * @param array $languageDependentPropertyLabels
37
	 * @param array $canonicalPropertyLabels
38
	 */
39 259
	public function __construct( Store $store, array $languageDependentPropertyLabels = array(), array $canonicalPropertyLabels = array() ) {
40 259
		$this->store = $store;
41 259
		$this->languageDependentPropertyLabels = $languageDependentPropertyLabels;
42 259
		$this->canonicalPropertyLabels = $canonicalPropertyLabels;
43 259
	}
44
45
	/**
46
	 * @since 2.2
47
	 *
48
	 * @return array
49
	 */
50 1
	public function getKownPredefinedPropertyLabels() {
51 1
		return $this->languageDependentPropertyLabels;
52
	}
53
54
	/**
55
	 * @since 2.4
56
	 *
57
	 * @param string $id
58
	 *
59
	 * @return string|boolean
60
	 */
61 49
	public function findCanonicalPropertyLabelById( $id ) {
62
63
		// This is fixed otherwise the `_txt` assignment interferes with the label
64
		// declaration
65 49
		if ( $id === '_TEXT' ) {
66 5
			return 'Text';
67
		}
68
69 49
		return array_search( $id, $this->canonicalPropertyLabels );
70
	}
71
72
	/**
73
	 * @note An empty string is returned for incomplete translation (language
74
	 * bug) or deliberately invisible property
75
	 *
76
	 * @since 2.2
77
	 *
78
	 * @param string $id
79
	 *
80
	 * @return string
81
	 */
82 258
	public function findPropertyLabelById( $id ) {
83
84 258
		if ( array_key_exists( $id, $this->languageDependentPropertyLabels ) ) {
85 257
			return $this->languageDependentPropertyLabels[$id];
86
		}
87
88 88
		return '';
89
	}
90
91
	/**
92
	 * @note An empty string is returned for incomplete translation (language
93
	 * bug) or deliberately invisible property
94
	 *
95
	 * @since 2.5
96
	 *
97
	 * @param string $id
98
	 * @param string $languageCode
99
	 *
100
	 * @return string
101
	 */
102 4
	public function findPropertyLabelByLanguageCode( $id, $languageCode = '' ) {
103
104 4
		if ( $languageCode === '' ) {
105
			return $this->findPropertyLabelById( $id );
106
		}
107
108 4
		$extraneousLanguage = Localizer::getInstance()->getExtraneousLanguage(
109
			mb_strtolower( trim( $languageCode ) )
110
		);
111
112 4
		$labels = $extraneousLanguage->getPropertyLabels() + $extraneousLanguage->getDatatypeLabels();
113
114 4
		if ( isset( $labels[$id] ) ) {
115 4
			return $labels[$id];
116
		}
117
118
		return '';
119
	}
120
121
	/**
122
	 * @since 2.5
123
	 *
124
	 * @param string $id
125
	 * @param string $languageCode
126
	 *
127
	 * @return string
128 215
	 */
129 215
	public function findPreferredPropertyLabelByLanguageCode( $id, $languageCode = '' ) {
130
131
		if ( $id === '' || $id === false ) {
132
			return '';
133
		}
134
135
		// Lookup is cached in PropertySpecificationLookup
136
		$propertySpecificationLookup = ApplicationFactory::getInstance()->getPropertySpecificationLookup();
137
		$propertySpecificationLookup->setLanguageCode( $languageCode );
138 255
139 255
		$preferredPropertyLabel = $propertySpecificationLookup->getPreferredPropertyLabelBy( $id );
140
141
		// In case someone tried a preferred label on a predefined property like
142
		// _MDAT => '[[Has preferred property label::Foo@en]]' but ensure to find
143
		// a "standard" via extraneousLanguage
144 255
		if ( $id{0} === '_' && $preferredPropertyLabel === '' ) {
145 255
			$preferredPropertyLabel = $this->findPropertyLabelByLanguageCode( $id, $languageCode );
146
		}
147 255
148
		return $preferredPropertyLabel;
149
	}
150
151
	/**
152
	 * @since 2.5
153
	 *
154
	 * @param string $text
155
	 * @param string $languageCode
156
	 *
157
	 * @return DIProperty[]|[]
0 ignored issues
show
Documentation introduced by
The doc-type DIProperty[]|[] could not be parsed: Unknown type name "" at position 13. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
158
	 */
159
	public function findPropertyListFromLabelByLanguageCode( $text, $languageCode = '' ) {
160
161
		if ( $text === '' ) {
162
			return array();
163
		}
164
165
		if ( $languageCode === '' ) {
166
			$languageCode = Localizer::getInstance()->getContentLanguage()->getCode();
167
		}
168
169
		$dataValue = DataValueFactory::getInstance()->newDataValueByProperty(
170
			new DIProperty( '_PPLB' )
171
		);
172
173
		$dataValue->setUserValue(
174
			$dataValue->getTextWithLanguageTag( $text, $languageCode )
175
		);
176
177
		$queryFactory = ApplicationFactory::getInstance()->getQueryFactory();
178
		$descriptionFactory = $queryFactory->newDescriptionFactory();
179
180
		$description = $descriptionFactory->newConjunction( array(
0 ignored issues
show
Documentation introduced by
array($descriptionFactor...mDataValue($dataValue)) is of type array<integer,object<SMW...guage\\SomeProperty>"}>, but the function expects a array<integer,object<SMW...\Language\Description>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
181
			$descriptionFactory->newNamespaceDescription( SMW_NS_PROPERTY ),
182
			$descriptionFactory->newFromDataValue( $dataValue )
183
		) );
184
185
		$propertyList = array();
186
		$queryResult = $this->store->getQueryResult(
187
			$queryFactory->newQuery( $description )
188
		);
189
190
		if ( !$queryResult instanceof \SMWQueryResult ) {
191
			return $propertyList;
192
		}
193
194
		foreach ( $queryResult->getResults() as $result ) {
195
			$propertyList[] = DIProperty::newFromUserLabel( $result->getDBKey() );
196
		}
197
198
		return $propertyList;
199
	}
200
201
	/**
202
	 * @since 2.2
203
	 *
204
	 * @param string $label
205
	 *
206
	 * @return string|false
207
	 */
208
	public function searchPropertyIdByLabel( $label ) {
209
		return array_search( $label, $this->languageDependentPropertyLabels );
210
	}
211
212
	/**
213
	 * @since 2.2
214
	 *
215
	 * @param string $id
216
	 * @param string $label
217
	 */
218
	public function registerPropertyLabel( $id, $label, $asCanonical = true ) {
219
		$this->languageDependentPropertyLabels[$id] = $label;
220
221
		// This is done so extensions can register the property id/label as being
222
		// canonical in their representation while the alias may hold translated
223
		// language depedendant matches
224
		if ( $asCanonical ) {
225
			$this->canonicalPropertyLabels[$label] = $id;
226
		}
227
	}
228
229
}
230