1 | <?php |
||
27 | abstract class AbstractConfig extends ArrayObject implements ConfigInterface |
||
28 | { |
||
29 | |||
30 | /** |
||
31 | * Array of strings that are used as delimiters to parse configuration keys. |
||
32 | * |
||
33 | * @since 0.1.6 |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $delimiter = ['\\', '/', '.']; |
||
38 | |||
39 | /** |
||
40 | * Instantiate the AbstractConfig object. |
||
41 | * |
||
42 | * @since 0.1.0 |
||
43 | * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
||
44 | * |
||
45 | * @param array $config Array with settings. |
||
46 | * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
||
47 | * configuration keys. Defaults to "\", "/" & ".". |
||
48 | */ |
||
49 | 1 | public function __construct(array $config, $delimiter = null) |
|
58 | |||
59 | /** |
||
60 | * Check whether the Config has a specific key. |
||
61 | * |
||
62 | * To check a value several levels deep, add the keys for each level as a comma-separated list. |
||
63 | * |
||
64 | * @since 0.1.0 |
||
65 | * @since 0.1.4 Accepts list of keys. |
||
66 | * |
||
67 | * @param string ... List of keys. |
||
68 | * @return bool |
||
69 | * @throws BadMethodCallException If no argument was provided. |
||
70 | */ |
||
71 | 2 | public function hasKey() |
|
86 | |||
87 | /** |
||
88 | * Get the value of a specific key. |
||
89 | * |
||
90 | * To get a value several levels deep, add the keys for each level as a comma-separated list. |
||
91 | * |
||
92 | * @since 0.1.0 |
||
93 | * @since 0.1.4 Accepts list of keys. |
||
94 | * |
||
95 | * @param string ... List of keys. |
||
96 | * @return mixed |
||
97 | * @throws BadMethodCallException If no argument was provided. |
||
98 | * @throws OutOfRangeException If an unknown key is requested. |
||
99 | */ |
||
100 | 2 | public function getKey() |
|
118 | |||
119 | /** |
||
120 | * Get a (multi-dimensional) array of all the configuration settings. |
||
121 | * |
||
122 | * @since 0.1.4 |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | 1 | public function getAll() |
|
130 | |||
131 | /** |
||
132 | * Get the an array with all the keys |
||
133 | * |
||
134 | * @since 0.1.0 |
||
135 | * @return array |
||
|
|||
136 | */ |
||
137 | 1 | public function getKeys() |
|
141 | |||
142 | /** |
||
143 | * Extract the configuration key arguments from an arbitrary array. |
||
144 | * |
||
145 | * @since 0.1.6 |
||
146 | * |
||
147 | * @param array $arguments Array as fetched through get_func_args(). |
||
148 | * @return array Array of strings. |
||
149 | * @throws BadMethodCallException If no argument was provided. |
||
150 | */ |
||
151 | 1 | protected function getKeyArguments($arguments) |
|
152 | { |
||
153 | 1 | if (count($arguments) < 1) { |
|
154 | throw new BadMethodCallException(_('No configuration key was provided.')); |
||
155 | } |
||
156 | |||
157 | 1 | $keys = []; |
|
158 | 1 | foreach ($arguments as $argument) { |
|
159 | 1 | if (is_array($argument)) { |
|
160 | 1 | $keys = array_merge($keys, $this->getKeyArguments($argument)); |
|
161 | } |
||
162 | 1 | if (is_string($argument)) { |
|
163 | 1 | $keys = array_merge($keys, $this->parseKeysString($argument)); |
|
164 | } |
||
165 | } |
||
166 | |||
167 | 1 | return $keys; |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * Extract individual keys from a delimited string. |
||
172 | * |
||
173 | * @since 0.1.6 |
||
174 | * |
||
175 | * @param string $keyString Delimited string of keys. |
||
176 | * @return array Array of key strings. |
||
177 | */ |
||
178 | 1 | protected function parseKeysString($keyString) |
|
179 | { |
||
180 | // Replace all of the configured delimiters by the first one, so that we can then use explode(). |
||
181 | 1 | $normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString); |
|
182 | |||
183 | 1 | return (array)explode($this->delimiter[0], $normalizedString); |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * Validate the Config file. |
||
188 | * |
||
189 | * @since 0.1.0 |
||
190 | * @return boolean |
||
191 | */ |
||
192 | abstract public function isValid(); |
||
193 | } |
||
194 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.