Completed
Push — main ( b55b92...e8d442 )
by
unknown
05:41
created

GenericOptions   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 113
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Addwiki\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
	private array $options = [];
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
21
22
	/**
23
	 * @since 0.2
24
	 *
25
	 * @throws InvalidArgumentException
26
	 * @param mixed[] $options
27
	 */
28
	public function __construct( array $options = [] ) {
29
		foreach ( array_keys( $options ) as $option ) {
30
			if ( !is_string( $option ) ) {
31
				throw new InvalidArgumentException( 'Option names need to be strings' );
32
			}
33
		}
34
35
		$this->options = $options;
36
	}
37
38
	/**
39
	 * Sets the value of the specified option.
40
	 *
41
	 * @since 0.2
42
	 *
43
	 * @param mixed $value
44
	 * @throws InvalidArgumentException
45
	 */
46
	public function setOption( string $option, $value ): void {
47
		if ( !is_string( $option ) ) {
48
			throw new InvalidArgumentException( 'Option name needs to be a string' );
49
		}
50
51
		$this->options[$option] = $value;
52
	}
53
54
	/**
55
	 * Returns the value of the specified option. If the option is not set,
56
	 * an InvalidArgumentException is thrown.
57
	 *
58
	 * @since 0.2
59
	 *
60
	 *
61
	 * @throws OutOfBoundsException
62
	 */
63
	public function getOption( string $option ) {
64
		if ( !array_key_exists( $option, $this->options ) ) {
65
			throw new OutOfBoundsException( sprintf( "Option '%s' has not been set so cannot be obtained", $option ) );
66
		}
67
68
		return $this->options[$option];
69
	}
70
71
	/**
72
	 * Returns if the specified option is set or not.
73
	 *
74
	 * @since 0.2
75
	 *
76
	 *
77
	 */
78
	public function hasOption( string $option ): bool {
79
		return array_key_exists( $option, $this->options );
80
	}
81
82
	/**
83
	 * Sets the value of an option to the provided default in case the option is not set yet.
84
	 *
85
	 * @since 0.2
86
	 *
87
	 * @param mixed $default
88
	 */
89
	public function defaultOption( string $option, $default ): void {
90
		if ( !$this->hasOption( $option ) ) {
91
			$this->setOption( $option, $default );
92
		}
93
	}
94
95
	/**
96
	 * Requires an option to be set.
97
	 * If it's not set, a RuntimeException is thrown.
98
	 *
99
	 * @since 0.2
100
	 *
101
	 *
102
	 * @throws RuntimeException
103
	 */
104
	public function requireOption( string $option ): void {
105
		if ( !$this->hasOption( $option ) ) {
106
			throw new RuntimeException( 'Required option"' . $option . '" is not set' );
107
		}
108
	}
109
110
	/**
111
	 * Returns the array of all options.
112
	 *
113
	 * @since 0.2
114
	 *
115
	 * @return mixed[]
116
	 */
117
	public function getOptions(): array {
118
		return $this->options;
119
	}
120
121
}
122