Completed
Push — master ( 49ddd3...7189a8 )
by mw
02:22
created

PropertyDefinitions::getLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace SESP;
4
5
use Onoi\Cache\Cache;
6
use Onoi\Cache\NullCache;
7
use SMW\Message;
8
use IteratorAggregate;
9
use ArrayIterator;
10
use InvalidArgumentException;
11
12
/**
13
 * @ingroup SESP
14
 *
15
 * @license GNU GPL v2+
16
 * @since 2.0
17
 *
18
 * @author mwjames
19
 */
20
class PropertyDefinitions implements IteratorAggregate {
21
22
	/**
23
	 * @var LabelFetcher
24
	 */
25
	private $labelFetcher;
26
27
	/**
28
	 * @var string
29
	 */
30
	private $propertyDefinitionFile;
31
32
	/**
33
	 * @var string
34
	 */
35
	private $labelCacheVersion = 0;
0 ignored issues
show
Unused Code introduced by
The property $labelCacheVersion is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
36
37
	/**
38
	 * @var array|null
39
	 */
40
	private $propertyDefinitions;
41
42
	/**
43
	 * @var array
44
	 */
45
	private $localPropertyDefinitions = array();
46
47
	/**
48
	 * @since 2.0
49
	 *
50
	 * @param LabelFetcher $labelFetcher
51
	 * @param string $propertyDefinitionFile
52
	 */
53 8
	public function __construct( LabelFetcher $labelFetcher, $propertyDefinitionFile = '' ) {
54 8
		$this->labelFetcher = $labelFetcher;
55 8
		$this->propertyDefinitionFile = $propertyDefinitionFile;
56
57 8
		if ( $this->propertyDefinitionFile === '' ) {
58 8
			$this->propertyDefinitionFile = $GLOBALS['sespPropertyDefinitionFile'];
59 8
		}
60 8
	}
61
62
	/**
63
	 * @since 2.0
64
	 *
65
	 * @param array $localPropertyDefinitions
66
	 */
67 1
	public function setLocalPropertyDefinitions( array $localPropertyDefinitions ) {
68 1
		$this->localPropertyDefinitions = $localPropertyDefinitions;
69 1
	}
70
71
	/**
72
	 * @since 2.0
73
	 *
74
	 * @param array $propertyDefinitions
75
	 */
76 3
	public function setPropertyDefinitions( array $propertyDefinitions ) {
77 3
		$this->propertyDefinitions = $propertyDefinitions;
78 3
	}
79
80
	/**
81
	 * @since 2.0
82
	 *
83
	 * @param string $key
84
	 *
85
	 * @return boolean
86
	 */
87 1
	public function isLocalDef( $key ) {
88 1
		return isset( $this->localPropertyDefinitions[$key] ) || array_key_exists( $key, $this->localPropertyDefinitions );
89
	}
90
91
	/**
92
	 * @since 2.0
93
	 *
94
	 * @param string $key
95
	 *
96
	 * @return boolean
97
	 */
98 2
	public function has( $key ) {
99 2
		return isset( $this->propertyDefinitions[$key] ) || array_key_exists( $key, $this->propertyDefinitions );
100
	}
101
102
	/**
103
	 * @since 2.3
104
	 *
105
	 * @param string $key
106
	 *
107
	 * @return string
108
	 * @throws InvalidArgumentException
109
	 */
110 1
	public function get( $key ) {
111
112 1
		if ( $this->has( $key ) ) {
113 1
			return $this->propertyDefinitions[$key];
114
		}
115
116
		throw new InvalidArgumentException( "{$key} is an unregistered option" );
117
	}
118
119
	/**
120
	 * @since 2.0
121
	 *
122
	 * @param string $key
123
	 *
124
	 * @return boolean
125
	 */
126 1
	public function deepHas( $key, $key2 ) {
127 1
		return isset( $this->propertyDefinitions[$key][$key2] );
128
	}
129
130
	/**
131
	 * @since 2.3
132
	 *
133
	 * @param string $key
134
	 *
135
	 * @return string
136
	 * @throws InvalidArgumentException
137
	 */
138 1
	public function deepGet( $key, $key2 ) {
139
140 1
		if ( $this->deepHas( $key, $key2 ) ) {
141 1
			return $this->propertyDefinitions[$key][$key2];
142
		}
143
144
		throw new InvalidArgumentException( "{$key}{$key2} is an unregistered option" );
145
	}
146
147
	/**
148
	 * @since 3.0
149
	 *
150
	 * @param string $key
151
	 * @param mixed $default
152
	 *
153
	 * @return mixed
154
	 */
155 1
	public function safeGet( $key, $default = false ) {
156 1
		return $this->has( $key ) ? $this->propertyDefinitions[$key] : $default;
157
	}
158
159
	/**
160
	 * @since 2.0
161
	 *
162
	 * @return array
163
	 */
164 1
	public function getLabels() {
165
166 1
		if ( $this->propertyDefinitions === null ) {
167 1
			$this->initPropertyDefinitions();
168 1
		}
169
170 1
		return $this->labelFetcher->getLabelsFrom( $this );
171
	}
172
173
	/**
174
	 * @since 2.0
175
	 *
176
	 * @param string $key
177
	 *
178
	 * @return string
179
	 */
180 1
	public function getLabel( $key ) {
181 1
		return $this->labelFetcher->getLabel( $key );
182
	}
183
184
	/**
185
	 * @see IteratorAggregate::getIterator
186
	 *
187
	 * @since 2.0
188
	 *
189
	 * @return Iterator
190
	 */
191 1
	public function getIterator() {
192
193 1
		if ( $this->propertyDefinitions === null ) {
194 1
			$this->initPropertyDefinitions();
195 1
		}
196
197 1
		return new ArrayIterator( $this->propertyDefinitions );
198
	}
199
200 2
	private function initPropertyDefinitions() {
201
202 2
		$contents = file_get_contents(
203 2
			str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $this->propertyDefinitionFile )
204 2
		);
205
206 2
		$this->propertyDefinitions = json_decode(
207 2
			$contents,
208
			true
209 2
		);
210
211 2
		if ( json_last_error() !== JSON_ERROR_NONE ) {
212
			$this->propertyDefinitions = array();
213
		}
214
215 2
		$this->propertyDefinitions += $this->localPropertyDefinitions;
216 2
	}
217
218
}
219