| 1 | <?php |
||
| 28 | abstract class AbstractConfig extends ArrayObject implements ConfigInterface |
||
| 29 | { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Array of strings that are used as delimiters to parse configuration keys. |
||
| 33 | * |
||
| 34 | * @since 0.1.6 |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $delimiter = ['\\', '/', '.']; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Instantiate the AbstractConfig object. |
||
| 42 | * |
||
| 43 | * @since 0.1.0 |
||
| 44 | * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
||
| 45 | * |
||
| 46 | * @param array $config Array with settings. |
||
| 47 | * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
||
| 48 | * configuration keys. Defaults to "\", "/" & ".". |
||
| 49 | */ |
||
| 50 | 3 | public function __construct(array $config, $delimiter = null) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Get the value of a specific key. |
||
| 62 | * |
||
| 63 | * To get a value several levels deep, add the keys for each level as a comma-separated list. |
||
| 64 | * |
||
| 65 | * @since 0.1.0 |
||
| 66 | * @since 0.1.4 Accepts list of keys. |
||
| 67 | * |
||
| 68 | * @param string $_ List of keys. |
||
| 69 | * @return mixed |
||
| 70 | * @throws BadMethodCallException If no argument was provided. |
||
| 71 | * @throws OutOfRangeException If an unknown key is requested. |
||
| 72 | */ |
||
| 73 | 3 | public function getKey($_) |
|
|
1 ignored issue
–
show
|
|||
| 74 | { |
||
| 75 | 3 | $keys = $this->validateKeys(func_get_args()); |
|
| 76 | |||
| 77 | 3 | $keys = array_reverse($keys); |
|
| 78 | 3 | $array = $this->getArrayCopy(); |
|
| 79 | 3 | while (count($keys) > 0) { |
|
| 80 | 3 | $key = array_pop($keys); |
|
| 81 | 3 | $array = $array[$key]; |
|
| 82 | } |
||
| 83 | |||
| 84 | 3 | return $array; |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Check whether the Config has a specific key. |
||
| 89 | * |
||
| 90 | * To check 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 bool |
||
| 97 | */ |
||
| 98 | 3 | public function hasKey($_) |
|
|
1 ignored issue
–
show
|
|||
| 99 | { |
||
| 100 | try { |
||
| 101 | 3 | $keys = array_reverse($this->getKeyArguments(func_get_args())); |
|
| 102 | |||
| 103 | 3 | $array = $this->getArrayCopy(); |
|
| 104 | 3 | while (count($keys) > 0) { |
|
| 105 | 3 | $key = array_pop($keys); |
|
| 106 | 3 | if (! array_key_exists($key, $array)) { |
|
| 107 | 3 | return false; |
|
| 108 | } |
||
| 109 | 3 | $array = $array[$key]; |
|
| 110 | } |
||
| 111 | 1 | } catch (Exception $exception) { |
|
| 112 | 1 | return false; |
|
| 113 | } |
||
| 114 | |||
| 115 | 3 | return true; |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get a (multi-dimensional) array of all the configuration settings. |
||
| 120 | * |
||
| 121 | * @since 0.1.4 |
||
| 122 | * |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | 1 | public function getAll() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Get the an array with all the keys |
||
| 132 | * |
||
| 133 | * @since 0.1.0 |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | 1 | public function getKeys() |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Get a new config at a specific sub-level. |
||
| 143 | * |
||
| 144 | * @since 0.1.13 |
||
| 145 | * |
||
| 146 | * @param string $_ List of keys. |
||
| 147 | * @return ConfigInterface |
||
|
1 ignored issue
–
show
|
|||
| 148 | * @throws BadMethodCallException If no argument was provided. |
||
| 149 | * @throws OutOfRangeException If an unknown key is requested. |
||
| 150 | */ |
||
| 151 | 1 | public function getSubConfig($_) |
|
|
1 ignored issue
–
show
|
|||
| 152 | { |
||
| 153 | 1 | $keys = $this->validateKeys(func_get_args()); |
|
| 154 | |||
| 155 | 1 | $subConfig = clone $this; |
|
| 156 | 1 | $subConfig->reduceToSubKey($keys); |
|
| 157 | |||
| 158 | 1 | return $subConfig; |
|
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Validate a set of keys to make sure they exist. |
||
| 163 | * |
||
| 164 | * @since 0.1.13 |
||
| 165 | * |
||
| 166 | * @param string $_ List of keys. |
||
| 167 | * @return array List of keys. |
||
| 168 | * @throws BadMethodCallException If no argument was provided. |
||
| 169 | * @throws OutOfRangeException If an unknown key is requested. |
||
| 170 | */ |
||
| 171 | 1 | public function validateKeys($_) |
|
|
2 ignored issues
–
show
|
|||
| 172 | { |
||
| 173 | 1 | $keys = $this->getKeyArguments(func_get_args()); |
|
| 174 | |||
| 175 | 1 | Assert\that($keys)->all()->string()->notEmpty(); |
|
| 176 | |||
| 177 | 1 | if (! $this->hasKey($keys)) { |
|
| 178 | 1 | throw new OutOfRangeException( |
|
| 179 | sprintf( |
||
| 180 | 1 | _('The configuration key %1$s does not exist.'), |
|
| 181 | 1 | implode('->', $keys) |
|
| 182 | ) |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | |||
| 186 | 1 | return $keys; |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Reduce the currently stored config array to a subarray at a specific level. |
||
| 191 | * |
||
| 192 | * @since 0.1.13 |
||
| 193 | * |
||
| 194 | * @param array $keys Array of keys that point to a key down in the hierarchy. |
||
| 195 | */ |
||
| 196 | 1 | protected function reduceToSubKey(array $keys) |
|
| 197 | { |
||
| 198 | 1 | $this->exchangeArray($this->getKey($keys)); |
|
| 199 | 1 | } |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Recursively extract the configuration key arguments from an arbitrary array. |
||
| 203 | * |
||
| 204 | * @since 0.1.6 |
||
| 205 | * |
||
| 206 | * @param array $arguments Array as fetched through get_func_args(). |
||
| 207 | * @return array Array of strings. |
||
| 208 | * @throws BadMethodCallException If no argument was provided. |
||
| 209 | */ |
||
| 210 | 5 | protected function getKeyArguments($arguments) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Extract individual keys from a delimited string. |
||
| 229 | * |
||
| 230 | * @since 0.1.6 |
||
| 231 | * |
||
| 232 | * @param string $keyString Delimited string of keys. |
||
| 233 | * @return array Array of key strings. |
||
| 234 | */ |
||
| 235 | 2 | protected function parseKeysString($keyString) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Validate the Config file. |
||
| 247 | * |
||
| 248 | * @since 0.1.0 |
||
| 249 | * @return boolean |
||
| 250 | */ |
||
| 251 | abstract public function isValid(); |
||
| 252 | } |
||
| 253 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.