Completed
Push — refact-v2 ( 9bddae...f926b0 )
by mw
05:08
created

PropertyDefinitions   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 75.56%

Importance

Changes 0
Metric Value
dl 0
loc 162
ccs 34
cts 45
cp 0.7556
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setLocalPropertyDefinitions() 0 3 1
A isLocalDef() 0 3 2
A safeGet() 0 3 2
A __construct() 0 7 2
A setPropertyDefinitions() 0 3 1
A has() 0 3 2
A get() 0 8 2
A deepHas() 0 3 1
A deepGet() 0 8 2
A getIterator() 0 8 2
A initPropertyDefinitions() 0 17 2
1
<?php
2
3
namespace SESP;
4
5
use IteratorAggregate;
6
use ArrayIterator;
7
use InvalidArgumentException;
8
9
/**
10
 * @ingroup SESP
11
 *
12
 * @license GNU GPL v2+
13
 * @since 2.0
14
 *
15
 * @author mwjames
16
 */
17
class PropertyDefinitions implements IteratorAggregate {
18
19
	/**
20
	 * @var string
21
	 */
22
	private $propertyDefinitionFile;
23
24
	/**
25
	 * @var array|null
26
	 */
27
	private $propertyDefinitions;
28
29
	/**
30
	 * @var array
31
	 */
32
	private $localPropertyDefinitions = array();
33
34
	/**
35
	 * @since 2.0
36
	 *
37
	 * @param string $propertyDefinitionFile
38
	 */
39 4
	public function __construct( $propertyDefinitionFile = '' ) {
40 4
		$this->propertyDefinitionFile = $propertyDefinitionFile;
41
42 4
		if ( $this->propertyDefinitionFile === '' ) {
43 4
			$this->propertyDefinitionFile = $GLOBALS['sespPropertyDefinitionFile'];
44 4
		}
45 4
	}
46
47
	/**
48
	 * @since 2.0
49
	 *
50
	 * @param array $localPropertyDefinitions
51
	 */
52
	public function setLocalPropertyDefinitions( array $localPropertyDefinitions ) {
53
		$this->localPropertyDefinitions = $localPropertyDefinitions;
54
	}
55
56
	/**
57
	 * @since 2.0
58
	 *
59
	 * @param array $propertyDefinitions
60
	 */
61 2
	public function setPropertyDefinitions( array $propertyDefinitions ) {
62 2
		$this->propertyDefinitions = $propertyDefinitions;
63 2
	}
64
65
	/**
66
	 * @since 2.0
67
	 *
68
	 * @param string $key
69
	 *
70
	 * @return boolean
71
	 */
72
	public function isLocalDef( $key ) {
73
		return isset( $this->localPropertyDefinitions[$key] ) || array_key_exists( $key, $this->localPropertyDefinitions );
74
	}
75
76
	/**
77
	 * @since 2.0
78
	 *
79
	 * @param string $key
80
	 *
81
	 * @return boolean
82
	 */
83 1
	public function has( $key ) {
84 1
		return isset( $this->propertyDefinitions[$key] ) || array_key_exists( $key, $this->propertyDefinitions );
85
	}
86
87
	/**
88
	 * @since 2.3
89
	 *
90
	 * @param string $key
91
	 *
92
	 * @return string
93
	 * @throws InvalidArgumentException
94
	 */
95 1
	public function get( $key ) {
96
97 1
		if ( $this->has( $key ) ) {
98 1
			return $this->propertyDefinitions[$key];
99
		}
100
101
		throw new InvalidArgumentException( "{$key} is an unregistered option" );
102
	}
103
104
	/**
105
	 * @since 2.0
106
	 *
107
	 * @param string $key
108
	 *
109
	 * @return boolean
110
	 */
111 1
	public function deepHas( $key, $key2 ) {
112 1
		return isset( $this->propertyDefinitions[$key][$key2] );
113
	}
114
115
	/**
116
	 * @since 2.3
117
	 *
118
	 * @param string $key
119
	 *
120
	 * @return string
121
	 * @throws InvalidArgumentException
122
	 */
123 1
	public function deepGet( $key, $key2 ) {
124
125 1
		if ( $this->deepHas( $key, $key2 ) ) {
126 1
			return $this->propertyDefinitions[$key][$key2];
127
		}
128
129
		throw new InvalidArgumentException( "{$key}{$key2} is an unregistered option" );
130
	}
131
132
	/**
133
	 * @since 3.0
134
	 *
135
	 * @param string $key
136
	 * @param mixed $default
137
	 *
138
	 * @return mixed
139
	 */
140
	public function safeGet( $key, $default = false ) {
141
		return $this->has( $key ) ? $this->propertyDefinitions[$key] : $default;
142
	}
143
144
	/**
145
	 * @see IteratorAggregate::getIterator
146
	 *
147
	 * @since 2.0
148
	 *
149
	 * @return Iterator
150
	 */
151 1
	public function getIterator() {
152
153 1
		if ( $this->propertyDefinitions === null ) {
154 1
			$this->initPropertyDefinitions();
155 1
		}
156
157 1
		return new ArrayIterator( $this->propertyDefinitions );
158
	}
159
160 1
	private function initPropertyDefinitions() {
161
162 1
		$contents = file_get_contents(
163 1
			str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $this->propertyDefinitionFile )
164 1
		);
165
166 1
		$this->propertyDefinitions = json_decode(
167 1
			$contents,
168
			true
169 1
		);
170
171 1
		if ( json_last_error() !== JSON_ERROR_NONE ) {
172
			$this->propertyDefinitions = array();
173
		}
174
175 1
		$this->propertyDefinitions += $this->localPropertyDefinitions;
176 1
	}
177
178
}
179