GenericOptions::requireOption()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Wikibase\Api;
4
5
use InvalidArgumentException;
6
use OutOfBoundsException;
7
use RuntimeException;
8
9
/**
10
 * Object holding options.
11
 *
12
 * @since 0.2
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Jeroen De Dauw < [email protected] >
16
 * @author Addshore
17
 */
18
final class GenericOptions {
19
20
	/**
21
	 * @var array
22
	 */
23
	private $options;
24
25
	/**
26
	 * @since 0.2
27
	 *
28
	 * @throws InvalidArgumentException
29
	 */
30 11
	public function __construct( array $options = [] ) {
31 11
		foreach ( array_keys( $options ) as $option ) {
32 11
			if ( !is_string( $option ) ) {
33 1
				throw new InvalidArgumentException( 'Option names need to be strings' );
34
			}
35 11
		}
36
37 10
		$this->options = $options;
38 10
	}
39
40
	/**
41
	 * Sets the value of the specified option.
42
	 *
43
	 * @since 0.2
44
	 *
45
	 * @param string $option
46
	 * @param mixed $value
47
	 *
48
	 * @throws InvalidArgumentException
49
	 */
50 6
	public function setOption( $option, $value ) {
51 6
		if ( !is_string( $option ) ) {
52
			throw new InvalidArgumentException( 'Option name needs to be a string' );
53
		}
54
55 6
		$this->options[$option] = $value;
56 6
	}
57
58
	/**
59
	 * Returns the value of the specified option. If the option is not set,
60
	 * an InvalidArgumentException is thrown.
61
	 *
62
	 * @since 0.2
63
	 *
64
	 * @param string $option
65
	 *
66
	 * @throws OutOfBoundsException
67
	 */
68 11
	public function getOption( $option ) {
69 11
		if ( !array_key_exists( $option, $this->options ) ) {
70 5
			throw new OutOfBoundsException( "Option '$option' has not been set so cannot be obtained" );
71
		}
72
73 6
		return $this->options[$option];
74
	}
75
76
	/**
77
	 * Returns if the specified option is set or not.
78
	 *
79
	 * @since 0.2
80
	 *
81
	 * @param string $option
82
	 *
83
	 * @return bool
84
	 */
85 4
	public function hasOption( $option ) {
86 4
		return array_key_exists( $option, $this->options );
87
	}
88
89
	/**
90
	 * Sets the value of an option to the provided default in case the option is not set yet.
91
	 *
92
	 * @since 0.2
93
	 *
94
	 * @param string $option
95
	 * @param mixed $default
96
	 */
97 1
	public function defaultOption( $option, $default ) {
98 1
		if ( !$this->hasOption( $option ) ) {
99 1
			$this->setOption( $option, $default );
100 1
		}
101 1
	}
102
103
	/**
104
	 * Requires an option to be set.
105
	 * If it's not set, a RuntimeException is thrown.
106
	 *
107
	 * @since 0.2
108
	 *
109
	 * @param string $option
110
	 *
111
	 * @throws RuntimeException
112
	 */
113 1
	public function requireOption( $option ) {
114 1
		if ( !$this->hasOption( $option ) ) {
115 1
			throw new RuntimeException( 'Required option"' . $option . '" is not set' );
116
		}
117 1
	}
118
119
	/**
120
	 * Returns the array of all options.
121
	 *
122
	 * @since 0.2
123
	 *
124
	 * @return array
125
	 */
126 1
	public function getOptions() {
127 1
		return $this->options;
128
	}
129
130
}
131