Completed
Push — master ( 666c25...3184dc )
by mw
35:50
created

PropertyAliasFinder::registerAliasByFixedLabel()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5.2

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 3
nop 2
dl 0
loc 15
ccs 4
cts 5
cp 0.8
crap 5.2
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SMW;
4
5
/**
6
 * @license GNU GPL v2
7
 * @since 2.4
8
 *
9
 * @author mwjames
10
 */
11
class PropertyAliasFinder {
12
13
	/**
14
	 * Array with entries "property alias" => "property id"
15
	 *
16
	 * @var string[]
17
	 */
18
	private $propertyAliases = array();
19
20
	/**
21
	 * @var string[]
22
	 */
23
	private $propertyAliasesByMsgKey = array();
24
25
	/**
26
	 * @var string[]
27
	 */
28
	private $canonicalPropertyAliases = array();
29
30
	/**
31
	 * @since 2.4
32
	 *
33
	 * @param array $propertyAliases
34
	 * @param array $canonicalPropertyAliases
35
	 */
36
	public function __construct( array $propertyAliases = array(), array $canonicalPropertyAliases = array() ) {
37
		$this->canonicalPropertyAliases = $canonicalPropertyAliases;
38 275
39 275
		foreach ( $propertyAliases as $alias => $id ) {
40
			$this->registerAliasByFixedLabel( $id, $alias );
41 275
		}
42 272
	}
43
44 275
	/**
45
	 * @since 2.4
46
	 *
47
	 * @return array
48
	 */
49
	public function getKnownPropertyAliases() {
50
		return $this->propertyAliases;
51 1
	}
52 1
53
	/**
54
	 * @since 2.4
55
	 *
56
	 * @return array
57
	 */
58
	public function getKnownPropertyAliasesWithMsgKey() {
59
		return $this->propertyAliasesByMsgKey;
60 202
	}
61 202
62
	/**
63
	 * Add a new alias label to an existing property ID. Note that every ID
64
	 * should have a primary label.
65
	 *
66
	 * @param string $id string
67
	 * @param string $label
68
	 */
69
	public function registerAliasByFixedLabel( $id, $label ) {
70
71 273
		// Prevent an extension to register an already known
72
		// label
73
		if ( isset( $this->canonicalPropertyAliases[$label] ) && $this->canonicalPropertyAliases[$label] !== $id ) {
74 273
			return;
75
		}
76
77
		// Indicates an untranslated MW message key
78 273
		if ( $label !== '' && $label{0} === '<' ) {
79 273
			return null;
80
		}
81
82
		$this->propertyAliases[$label] = $id;
83
	}
84
85
	/**
86
	 * Register an alias using a message key to allow fetching localized
87
	 * labels dynamically.
88
	 *
89
	 * @since 2.4
90
	 *
91
	 * @param string $id
92
	 * @param string $msgKey
93
	 */
94
	public function registerAliasByMsgKey( $id, $msgKey ) {
95
		$this->propertyAliasesByMsgKey[$msgKey] = $id;
96
	}
97
98
	/**
99
	 * @since 2.4
100
	 *
101
	 * @param string $id
102
	 *
103
	 * @return string|boolean
104
	 */
105
	public function findCanonicalPropertyAliasById( $id ) {
106
		return array_search( $id, $this->canonicalPropertyAliases );
107
	}
108
109
	/**
110
	 * @since 2.4
111
	 *
112 1
	 * @param string $id
113 1
	 *
114
	 * @return string|boolean
115
	 */
116
	public function findPropertyAliasById( $id ) {
117
		return array_search( $id, $this->propertyAliases );
118
	}
119
120
	/**
121
	 * Find and return the ID for the pre-defined property of the given
122
	 * local label. If the label does not belong to a pre-defined property,
123
	 * return false.
124
	 *
125 233
	 * @param string $alias
126
	 *
127 233
	 * @return string|boolean
128 7
	 */
129 228
	public function findPropertyIdByAlias( $alias ) {
130 6
131
		if ( isset( $this->propertyAliases[$alias] ) ) {
132
			return $this->propertyAliases[$alias];
133 226
		} elseif ( isset( $this->canonicalPropertyAliases[$alias] ) ) {
134
			return $this->canonicalPropertyAliases[$alias];
135
		}
136
137
		return false;
138
	}
139
140
}
141