@@ -28,170 +28,170 @@ |
||
| 28 | 28 | abstract class AbstractConfig extends ArrayObject implements ConfigInterface |
| 29 | 29 | { |
| 30 | 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 | - public function __construct(array $config, $delimiter = null) |
|
| 51 | - { |
|
| 52 | - // Make sure the config entries can be accessed as properties. |
|
| 53 | - parent::__construct($config, ArrayObject::ARRAY_AS_PROPS); |
|
| 54 | - |
|
| 55 | - if (null !== $delimiter) { |
|
| 56 | - $this->delimiter = (array)$delimiter; |
|
| 57 | - } |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Check whether the Config has a specific key. |
|
| 62 | - * |
|
| 63 | - * To check 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 bool |
|
| 70 | - */ |
|
| 71 | - public function hasKey() |
|
| 72 | - { |
|
| 73 | - try { |
|
| 74 | - $keys = array_reverse($this->getKeyArguments(func_get_args())); |
|
| 75 | - |
|
| 76 | - $array = $this->getArrayCopy(); |
|
| 77 | - while (count($keys) > 0) { |
|
| 78 | - $key = array_pop($keys); |
|
| 79 | - if ( ! array_key_exists($key, $array)) { |
|
| 80 | - return false; |
|
| 81 | - } |
|
| 82 | - $array = $array[$key]; |
|
| 83 | - } |
|
| 84 | - } catch (Exception $exception) { |
|
| 85 | - return false; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - return true; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Get the value of a specific key. |
|
| 93 | - * |
|
| 94 | - * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 95 | - * |
|
| 96 | - * @since 0.1.0 |
|
| 97 | - * @since 0.1.4 Accepts list of keys. |
|
| 98 | - * |
|
| 99 | - * @param string ... List of keys. |
|
| 100 | - * @return mixed |
|
| 101 | - * @throws BadMethodCallException If no argument was provided. |
|
| 102 | - * @throws OutOfRangeException If an unknown key is requested. |
|
| 103 | - */ |
|
| 104 | - public function getKey() |
|
| 105 | - { |
|
| 106 | - $keys = $this->getKeyArguments(func_get_args()); |
|
| 107 | - |
|
| 108 | - if ( ! $this->hasKey($keys)) { |
|
| 109 | - throw new OutOfRangeException(sprintf(_('The configuration key %1$s does not exist.'), |
|
| 110 | - implode('->', $keys))); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $keys = array_reverse($keys); |
|
| 114 | - $array = $this->getArrayCopy(); |
|
| 115 | - while (count($keys) > 0) { |
|
| 116 | - $key = array_pop($keys); |
|
| 117 | - $array = $array[$key]; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - return $array; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Get a (multi-dimensional) array of all the configuration settings. |
|
| 125 | - * |
|
| 126 | - * @since 0.1.4 |
|
| 127 | - * |
|
| 128 | - * @return array |
|
| 129 | - */ |
|
| 130 | - public function getAll() |
|
| 131 | - { |
|
| 132 | - return $this->getArrayCopy(); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Get the an array with all the keys |
|
| 137 | - * |
|
| 138 | - * @since 0.1.0 |
|
| 139 | - * @return array |
|
| 140 | - */ |
|
| 141 | - public function getKeys() |
|
| 142 | - { |
|
| 143 | - return array_keys((array)$this); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Extract the configuration key arguments from an arbitrary array. |
|
| 148 | - * |
|
| 149 | - * @since 0.1.6 |
|
| 150 | - * |
|
| 151 | - * @param array $arguments Array as fetched through get_func_args(). |
|
| 152 | - * @return array Array of strings. |
|
| 153 | - * @throws BadMethodCallException If no argument was provided. |
|
| 154 | - */ |
|
| 155 | - protected function getKeyArguments($arguments) |
|
| 156 | - { |
|
| 157 | - if (count($arguments) < 1) { |
|
| 158 | - throw new BadMethodCallException(_('No configuration key was provided.')); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - $keys = []; |
|
| 162 | - foreach ($arguments as $argument) { |
|
| 163 | - if (is_array($argument)) { |
|
| 164 | - $keys = array_merge($keys, $this->getKeyArguments($argument)); |
|
| 165 | - } |
|
| 166 | - if (is_string($argument)) { |
|
| 167 | - $keys = array_merge($keys, $this->parseKeysString($argument)); |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - return $keys; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Extract individual keys from a delimited string. |
|
| 176 | - * |
|
| 177 | - * @since 0.1.6 |
|
| 178 | - * |
|
| 179 | - * @param string $keyString Delimited string of keys. |
|
| 180 | - * @return array Array of key strings. |
|
| 181 | - */ |
|
| 182 | - protected function parseKeysString($keyString) |
|
| 183 | - { |
|
| 184 | - // Replace all of the configured delimiters by the first one, so that we can then use explode(). |
|
| 185 | - $normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString); |
|
| 186 | - |
|
| 187 | - return (array)explode($this->delimiter[0], $normalizedString); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Validate the Config file. |
|
| 192 | - * |
|
| 193 | - * @since 0.1.0 |
|
| 194 | - * @return boolean |
|
| 195 | - */ |
|
| 196 | - abstract public function isValid(); |
|
| 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 | + public function __construct(array $config, $delimiter = null) |
|
| 51 | + { |
|
| 52 | + // Make sure the config entries can be accessed as properties. |
|
| 53 | + parent::__construct($config, ArrayObject::ARRAY_AS_PROPS); |
|
| 54 | + |
|
| 55 | + if (null !== $delimiter) { |
|
| 56 | + $this->delimiter = (array)$delimiter; |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Check whether the Config has a specific key. |
|
| 62 | + * |
|
| 63 | + * To check 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 bool |
|
| 70 | + */ |
|
| 71 | + public function hasKey() |
|
| 72 | + { |
|
| 73 | + try { |
|
| 74 | + $keys = array_reverse($this->getKeyArguments(func_get_args())); |
|
| 75 | + |
|
| 76 | + $array = $this->getArrayCopy(); |
|
| 77 | + while (count($keys) > 0) { |
|
| 78 | + $key = array_pop($keys); |
|
| 79 | + if ( ! array_key_exists($key, $array)) { |
|
| 80 | + return false; |
|
| 81 | + } |
|
| 82 | + $array = $array[$key]; |
|
| 83 | + } |
|
| 84 | + } catch (Exception $exception) { |
|
| 85 | + return false; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + return true; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Get the value of a specific key. |
|
| 93 | + * |
|
| 94 | + * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 95 | + * |
|
| 96 | + * @since 0.1.0 |
|
| 97 | + * @since 0.1.4 Accepts list of keys. |
|
| 98 | + * |
|
| 99 | + * @param string ... List of keys. |
|
| 100 | + * @return mixed |
|
| 101 | + * @throws BadMethodCallException If no argument was provided. |
|
| 102 | + * @throws OutOfRangeException If an unknown key is requested. |
|
| 103 | + */ |
|
| 104 | + public function getKey() |
|
| 105 | + { |
|
| 106 | + $keys = $this->getKeyArguments(func_get_args()); |
|
| 107 | + |
|
| 108 | + if ( ! $this->hasKey($keys)) { |
|
| 109 | + throw new OutOfRangeException(sprintf(_('The configuration key %1$s does not exist.'), |
|
| 110 | + implode('->', $keys))); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $keys = array_reverse($keys); |
|
| 114 | + $array = $this->getArrayCopy(); |
|
| 115 | + while (count($keys) > 0) { |
|
| 116 | + $key = array_pop($keys); |
|
| 117 | + $array = $array[$key]; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + return $array; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Get a (multi-dimensional) array of all the configuration settings. |
|
| 125 | + * |
|
| 126 | + * @since 0.1.4 |
|
| 127 | + * |
|
| 128 | + * @return array |
|
| 129 | + */ |
|
| 130 | + public function getAll() |
|
| 131 | + { |
|
| 132 | + return $this->getArrayCopy(); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Get the an array with all the keys |
|
| 137 | + * |
|
| 138 | + * @since 0.1.0 |
|
| 139 | + * @return array |
|
| 140 | + */ |
|
| 141 | + public function getKeys() |
|
| 142 | + { |
|
| 143 | + return array_keys((array)$this); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Extract the configuration key arguments from an arbitrary array. |
|
| 148 | + * |
|
| 149 | + * @since 0.1.6 |
|
| 150 | + * |
|
| 151 | + * @param array $arguments Array as fetched through get_func_args(). |
|
| 152 | + * @return array Array of strings. |
|
| 153 | + * @throws BadMethodCallException If no argument was provided. |
|
| 154 | + */ |
|
| 155 | + protected function getKeyArguments($arguments) |
|
| 156 | + { |
|
| 157 | + if (count($arguments) < 1) { |
|
| 158 | + throw new BadMethodCallException(_('No configuration key was provided.')); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + $keys = []; |
|
| 162 | + foreach ($arguments as $argument) { |
|
| 163 | + if (is_array($argument)) { |
|
| 164 | + $keys = array_merge($keys, $this->getKeyArguments($argument)); |
|
| 165 | + } |
|
| 166 | + if (is_string($argument)) { |
|
| 167 | + $keys = array_merge($keys, $this->parseKeysString($argument)); |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + return $keys; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Extract individual keys from a delimited string. |
|
| 176 | + * |
|
| 177 | + * @since 0.1.6 |
|
| 178 | + * |
|
| 179 | + * @param string $keyString Delimited string of keys. |
|
| 180 | + * @return array Array of key strings. |
|
| 181 | + */ |
|
| 182 | + protected function parseKeysString($keyString) |
|
| 183 | + { |
|
| 184 | + // Replace all of the configured delimiters by the first one, so that we can then use explode(). |
|
| 185 | + $normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString); |
|
| 186 | + |
|
| 187 | + return (array)explode($this->delimiter[0], $normalizedString); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Validate the Config file. |
|
| 192 | + * |
|
| 193 | + * @since 0.1.0 |
|
| 194 | + * @return boolean |
|
| 195 | + */ |
|
| 196 | + abstract public function isValid(); |
|
| 197 | 197 | } |