1 | <?php |
||
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 = array() ) { |
|
31 | 11 | foreach ( array_keys( $options ) as $option ) { |
|
32 | if ( !is_string( $option ) ) { |
||
33 | throw new InvalidArgumentException( 'Option names need to be strings' ); |
||
34 | } |
||
35 | } |
||
36 | |||
37 | 10 | $this->options = $options; |
|
38 | } |
||
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 | 4 | public function setOption( $option, $value ) { |
|
51 | if ( !is_string( $option ) ) { |
||
52 | throw new InvalidArgumentException( 'Option name needs to be a string' ); |
||
53 | } |
||
54 | |||
55 | 4 | $this->options[$option] = $value; |
|
56 | 4 | } |
|
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 | 4 | public function getOption( $option ) { |
|
69 | if ( !array_key_exists( $option, $this->options ) ) { |
||
70 | throw new OutOfBoundsException( "Option '$option' has not been set so cannot be obtained" ); |
||
71 | } |
||
72 | |||
73 | 4 | 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 boolean |
||
84 | */ |
||
85 | public function hasOption( $option ) { |
||
86 | 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 | public function defaultOption( $option, $default ) { |
||
98 | if ( !$this->hasOption( $option ) ) { |
||
99 | $this->setOption( $option, $default ); |
||
100 | } |
||
101 | } |
||
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 | public function requireOption( $option ) { |
||
114 | if ( !$this->hasOption( $option ) ) { |
||
115 | throw new RuntimeException( 'Required option"' . $option . '" is not set' ); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Returns the array of all options. |
||
121 | * |
||
122 | * @since 0.2 |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | 1 | public function getOptions() { |
|
129 | |||
130 | } |
||
131 |