Completed
Push — refact-v2 ( 0317e0 )
by mw
04:47
created

PropertyDefinitions::has()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
crap 6
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
	public function __construct( $propertyDefinitionFile ) {
40
		$this->propertyDefinitionFile = $propertyDefinitionFile;
41
	}
42
43
	/**
44
	 * @since 2.0
45
	 *
46
	 * @param array $localPropertyDefinitions
47
	 */
48
	public function setLocalPropertyDefinitions( array $localPropertyDefinitions ) {
49
		$this->localPropertyDefinitions = $localPropertyDefinitions;
50
	}
51
52
	/**
53
	 * @since 2.0
54
	 *
55
	 * @param array $propertyDefinitions
56
	 */
57
	public function setPropertyDefinitions( array $propertyDefinitions ) {
58
		$this->propertyDefinitions = $propertyDefinitions;
59
	}
60
61
	/**
62
	 * @since 2.0
63
	 *
64
	 * @param string $key
65
	 *
66
	 * @return boolean
67
	 */
68
	public function isLocalDef( $key ) {
69
		return isset( $this->localPropertyDefinitions[$key] ) || array_key_exists( $key, $this->localPropertyDefinitions );
70
	}
71
72
	/**
73
	 * @since 2.0
74
	 *
75
	 * @param string $key
76
	 *
77
	 * @return boolean
78
	 */
79
	public function has( $key ) {
80
		return isset( $this->propertyDefinitions[$key] ) || array_key_exists( $key, $this->propertyDefinitions );
81
	}
82
83
	/**
84
	 * @since 2.3
85
	 *
86
	 * @param string $key
87
	 *
88
	 * @return string
89
	 * @throws InvalidArgumentException
90
	 */
91
	public function get( $key ) {
92
93
		if ( $this->has( $key ) ) {
94
			return $this->propertyDefinitions[$key];
95
		}
96
97
		throw new InvalidArgumentException( "{$key} is an unregistered option" );
98
	}
99
100
	/**
101
	 * @since 2.0
102
	 *
103
	 * @param string $key
104
	 *
105
	 * @return boolean
106
	 */
107
	public function deepHas( $key, $key2 ) {
108
		return isset( $this->propertyDefinitions[$key][$key2] );
109
	}
110
111
	/**
112
	 * @since 2.3
113
	 *
114
	 * @param string $key
115
	 *
116
	 * @return string
117
	 * @throws InvalidArgumentException
118
	 */
119
	public function deepGet( $key, $key2 ) {
120
121
		if ( $this->deepHas( $key, $key2 ) ) {
122
			return $this->propertyDefinitions[$key][$key2];
123
		}
124
125
		throw new InvalidArgumentException( "{$key}{$key2} is an unregistered option" );
126
	}
127
128
	/**
129
	 * @since 3.0
130
	 *
131
	 * @param string $key
132
	 * @param mixed $default
133
	 *
134
	 * @return mixed
135
	 */
136
	public function safeGet( $key, $default = false ) {
137
		return $this->has( $key ) ? $this->propertyDefinitions[$key] : $default;
138
	}
139
140
	/**
141
	 * @see IteratorAggregate::getIterator
142
	 *
143
	 * @since 2.0
144
	 *
145
	 * @return Iterator
146
	 */
147
	public function getIterator() {
148
149
		if ( $this->propertyDefinitions === null ) {
150
			$this->initPropertyDefinitions();
151
		}
152
153
		return new ArrayIterator( $this->propertyDefinitions );
154
	}
155
156
	private function initPropertyDefinitions() {
157
158
		$contents = @file_get_contents(
159
			str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $this->propertyDefinitionFile )
160
		);
161
162
		$this->propertyDefinitions = json_decode(
163
			$contents,
164
			true
165
		);
166
167
		if ( json_last_error() !== JSON_ERROR_NONE ) {
168
			$this->propertyDefinitions = array();
169
		}
170
171
		$this->propertyDefinitions += $this->localPropertyDefinitions;
172
	}
173
174
}
175