Completed
Push — master ( 30257a...9feb38 )
by Jeroen De
06:50 queued 04:21
created

ElementOptions::hasOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Maps;
4
5
use MWException;
6
7
/**
8
 * Interface for elements that can be placed upon a map.
9
 *
10
 * @since 3.0
11
 *
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
interface Element {
16
17
	/**
18
	 * Returns the value in array form.
19
	 *
20
	 * @since 3.0
21
	 *
22
	 * @return mixed
23
	 */
24
	public function getArrayValue();
25
26
	/**
27
	 * Returns the elements options.
28
	 * Modification of the elements options by mutating the obtained object is allowed.
29
	 *
30
	 * @since 3.0
31
	 *
32
	 * @return ElementOptions
33
	 */
34
	public function getOptions();
35
36
	/**
37
	 * Sets the elements options.
38
	 *
39
	 * @since 3.0
40
	 *
41
	 * @param ElementOptions $options
42
	 */
43
	public function setOptions( ElementOptions $options );
44
45
}
46
47
class ElementOptions {
48
49
	private $options = [];
50
51
	/**
52
	 * @since 3.0
53
	 *
54
	 * @param string $name
55
	 * @param mixed $value
56
	 *
57
	 * @throws \InvalidArgumentException
58
	 */
59
	public function setOption( $name, $value ) {
60
		if ( !is_string( $name ) ) {
61
			throw new \InvalidArgumentException( 'Option name should be a string' );
62
		}
63
64
		$this->options[$name] = $value;
65
	}
66
67
	/**
68
	 * @since 3.0
69
	 *
70
	 * @param string $name
71
	 *
72
	 * @throws \InvalidArgumentException
73
	 * @throws \OutOfBoundsException
74
	 */
75
	public function getOption( $name ) {
76
		if ( !is_string( $name ) ) {
77
			throw new \InvalidArgumentException( 'Option name should be a string' );
78
		}
79
80
		if ( !array_key_exists( $name, $this->options ) ) {
81
			throw new \OutOfBoundsException( 'Tried to obtain option "' . $name . '" while it has not been set' );
82
		}
83
84
		return $this->options[$name];
85
	}
86
87
	/**
88
	 * @since 3.0
89
	 *
90
	 * @param string $name
91
	 *
92
	 * @return boolean
93
	 * @throws \InvalidArgumentException
94
	 */
95
	public function hasOption( $name ) {
96
		if ( !is_string( $name ) ) {
97
			throw new \InvalidArgumentException( 'Option name should be a string' );
98
		}
99
100
		return array_key_exists( $name, $this->options );
101
	}
102
103
}