1 | <?php |
||
18 | final class GenericOptions { |
||
19 | |||
20 | private array $options = []; |
||
|
|||
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 |