Passed
Push — master ( 0e9514...630e5e )
by Alain
04:16
created
src/Config.php 1 patch
Indentation   +184 added lines, -184 removed lines patch added patch discarded remove patch
@@ -32,188 +32,188 @@
 block discarded – undo
32 32
 class Config extends AbstractConfig
33 33
 {
34 34
 
35
-    /**
36
-     * The schema of the Config file.
37
-     *
38
-     * @var Schema
39
-     */
40
-    protected $schema;
41
-
42
-    /**
43
-     * The Validator class that gets asked to do the validation of the config.
44
-     *
45
-     * @since 0.1.0
46
-     *
47
-     * @var Validator
48
-     */
49
-    protected $validator;
50
-
51
-    /**
52
-     * Instantiate the Config object.
53
-     *
54
-     * It accepts either an array with the configuration settings, or a
55
-     * filename pointing to a PHP file it can include.
56
-     *
57
-     * @since 0.1.0
58
-     * @since 0.1.6 Accepts a delimiter to parse configuration keys.
59
-     *
60
-     * @param array|string         $config    Array with settings or filename for the
61
-     *                                        settings file.
62
-     * @param Schema|null          $schema    Optional. Config that contains default
63
-     *                                        values that can get overwritten.
64
-     * @param Validator|null       $validator Optional. Validator class that does the
65
-     *                                        actual validation.
66
-     * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse
67
-     *                                        configuration keys. Defaults to "\", "/" & ".".
68
-     *
69
-     * @throws InvalidConfigurationSourceException If the config source is not a string or array.
70
-     * @throws FailedToInstantiateParentException  If the parent class could not be instantiated.
71
-     * @throws FailedToLoadConfigException         If loading of the config source failed.
72
-     * @throws FailedToResolveConfigException      If the config file could not be resolved.
73
-     * @throws InvalidConfigException              If the config file is not valid.
74
-     */
75
-    public function __construct(
76
-        $config,
77
-        Schema $schema = null,
78
-        Validator $validator = null,
79
-        $delimiter = null
80
-    ) {
81
-        $this->schema    = $schema;
82
-        $this->validator = $validator;
83
-
84
-        // Make sure $config is either a string or array.
85
-        if (! (is_string($config) || is_array($config))) {
86
-            throw new InvalidConfigurationSourceException(
87
-                sprintf(
88
-                    _('Invalid configuration source: %1$s'),
89
-                    print_r($config, true)
90
-                )
91
-            );
92
-        }
93
-
94
-        if (is_string($config)) {
95
-            $config = Loader::load($config);
96
-        }
97
-
98
-        // Run the $config through the OptionsResolver.
99
-        $config = $this->resolveOptions($config);
100
-
101
-        // Instantiate the parent class.
102
-        try {
103
-            parent::__construct($config, $delimiter);
104
-        } catch (Exception $exception) {
105
-            throw new FailedToInstantiateParentException(
106
-                sprintf(
107
-                    _('Could not instantiate the configuration through its parent. Reason: %1$s'),
108
-                    $exception->getMessage()
109
-                )
110
-            );
111
-        }
112
-
113
-        // Finally, validate the resulting config.
114
-        if (! $this->isValid()) {
115
-            throw new InvalidConfigException(
116
-                sprintf(
117
-                    _('ConfigInterface file is not valid: %1$s'),
118
-                    print_r($config, true)
119
-                )
120
-            );
121
-        }
122
-    }
123
-
124
-    /**
125
-     * Validate the Config file.
126
-     *
127
-     * @since  0.1.0
128
-     *
129
-     * @return boolean
130
-     */
131
-    public function isValid()
132
-    {
133
-        if ($this->validator) {
134
-            return $this->validator->isValid($this);
135
-        }
136
-
137
-        return true;
138
-    }
139
-
140
-    /**
141
-     * Process the passed-in defaults and merge them with the new values, while
142
-     * checking that all required options are set.
143
-     *
144
-     * @since 0.1.0
145
-     *
146
-     * @param array $config Configuration settings to resolve.
147
-     *
148
-     * @return array Resolved configuration settings.
149
-     * @throws FailedToResolveConfigException If there are errors while resolving the options.
150
-     */
151
-    protected function resolveOptions($config)
152
-    {
153
-        if (! $this->schema) {
154
-            return $config;
155
-        }
156
-
157
-        try {
158
-            $resolver = new OptionsResolver();
159
-            if ($this->configureOptions($resolver)) {
160
-                $config = $resolver->resolve($config);
161
-            }
162
-        } catch (Exception $exception) {
163
-            throw new FailedToResolveConfigException(
164
-                sprintf(
165
-                    _('Error while resolving config options: %1$s'),
166
-                    $exception->getMessage()
167
-                )
168
-            );
169
-        }
170
-
171
-        return $config;
172
-    }
173
-
174
-    /**
175
-     * Configure the possible and required options for the Config.
176
-     *
177
-     * This should return a bool to let the resolve_options() know whether the
178
-     * actual resolving needs to be done or not.
179
-     *
180
-     * @since 0.1.0
181
-     *
182
-     * @param OptionsResolver $resolver Reference to the OptionsResolver
183
-     *                                  instance.
184
-     *
185
-     * @return bool Whether to do the resolving.
186
-     * @throws FailedToResolveConfigException If there are errors while processing.
187
-     */
188
-    protected function configureOptions(OptionsResolver $resolver)
189
-    {
190
-        $defined  = $this->schema->getDefinedOptions();
191
-        $defaults = $this->schema->getDefaultOptions();
192
-        $required = $this->schema->getRequiredOptions();
193
-
194
-        if (! $defined && ! $defaults && ! $required) {
195
-            return false;
196
-        }
197
-
198
-        try {
199
-            if ($defined) {
200
-                $resolver->setDefined($defined);
201
-            }
202
-            if ($defaults) {
203
-                $resolver->setDefaults($defaults);
204
-            }
205
-            if ($required) {
206
-                $resolver->setRequired($required);
207
-            }
208
-        } catch (Exception $exception) {
209
-            throw new FailedToResolveConfigException(
210
-                sprintf(
211
-                    _('Error while processing config options: %1$s'),
212
-                    $exception->getMessage()
213
-                )
214
-            );
215
-        }
216
-
217
-        return true;
218
-    }
35
+	/**
36
+	 * The schema of the Config file.
37
+	 *
38
+	 * @var Schema
39
+	 */
40
+	protected $schema;
41
+
42
+	/**
43
+	 * The Validator class that gets asked to do the validation of the config.
44
+	 *
45
+	 * @since 0.1.0
46
+	 *
47
+	 * @var Validator
48
+	 */
49
+	protected $validator;
50
+
51
+	/**
52
+	 * Instantiate the Config object.
53
+	 *
54
+	 * It accepts either an array with the configuration settings, or a
55
+	 * filename pointing to a PHP file it can include.
56
+	 *
57
+	 * @since 0.1.0
58
+	 * @since 0.1.6 Accepts a delimiter to parse configuration keys.
59
+	 *
60
+	 * @param array|string         $config    Array with settings or filename for the
61
+	 *                                        settings file.
62
+	 * @param Schema|null          $schema    Optional. Config that contains default
63
+	 *                                        values that can get overwritten.
64
+	 * @param Validator|null       $validator Optional. Validator class that does the
65
+	 *                                        actual validation.
66
+	 * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse
67
+	 *                                        configuration keys. Defaults to "\", "/" & ".".
68
+	 *
69
+	 * @throws InvalidConfigurationSourceException If the config source is not a string or array.
70
+	 * @throws FailedToInstantiateParentException  If the parent class could not be instantiated.
71
+	 * @throws FailedToLoadConfigException         If loading of the config source failed.
72
+	 * @throws FailedToResolveConfigException      If the config file could not be resolved.
73
+	 * @throws InvalidConfigException              If the config file is not valid.
74
+	 */
75
+	public function __construct(
76
+		$config,
77
+		Schema $schema = null,
78
+		Validator $validator = null,
79
+		$delimiter = null
80
+	) {
81
+		$this->schema    = $schema;
82
+		$this->validator = $validator;
83
+
84
+		// Make sure $config is either a string or array.
85
+		if (! (is_string($config) || is_array($config))) {
86
+			throw new InvalidConfigurationSourceException(
87
+				sprintf(
88
+					_('Invalid configuration source: %1$s'),
89
+					print_r($config, true)
90
+				)
91
+			);
92
+		}
93
+
94
+		if (is_string($config)) {
95
+			$config = Loader::load($config);
96
+		}
97
+
98
+		// Run the $config through the OptionsResolver.
99
+		$config = $this->resolveOptions($config);
100
+
101
+		// Instantiate the parent class.
102
+		try {
103
+			parent::__construct($config, $delimiter);
104
+		} catch (Exception $exception) {
105
+			throw new FailedToInstantiateParentException(
106
+				sprintf(
107
+					_('Could not instantiate the configuration through its parent. Reason: %1$s'),
108
+					$exception->getMessage()
109
+				)
110
+			);
111
+		}
112
+
113
+		// Finally, validate the resulting config.
114
+		if (! $this->isValid()) {
115
+			throw new InvalidConfigException(
116
+				sprintf(
117
+					_('ConfigInterface file is not valid: %1$s'),
118
+					print_r($config, true)
119
+				)
120
+			);
121
+		}
122
+	}
123
+
124
+	/**
125
+	 * Validate the Config file.
126
+	 *
127
+	 * @since  0.1.0
128
+	 *
129
+	 * @return boolean
130
+	 */
131
+	public function isValid()
132
+	{
133
+		if ($this->validator) {
134
+			return $this->validator->isValid($this);
135
+		}
136
+
137
+		return true;
138
+	}
139
+
140
+	/**
141
+	 * Process the passed-in defaults and merge them with the new values, while
142
+	 * checking that all required options are set.
143
+	 *
144
+	 * @since 0.1.0
145
+	 *
146
+	 * @param array $config Configuration settings to resolve.
147
+	 *
148
+	 * @return array Resolved configuration settings.
149
+	 * @throws FailedToResolveConfigException If there are errors while resolving the options.
150
+	 */
151
+	protected function resolveOptions($config)
152
+	{
153
+		if (! $this->schema) {
154
+			return $config;
155
+		}
156
+
157
+		try {
158
+			$resolver = new OptionsResolver();
159
+			if ($this->configureOptions($resolver)) {
160
+				$config = $resolver->resolve($config);
161
+			}
162
+		} catch (Exception $exception) {
163
+			throw new FailedToResolveConfigException(
164
+				sprintf(
165
+					_('Error while resolving config options: %1$s'),
166
+					$exception->getMessage()
167
+				)
168
+			);
169
+		}
170
+
171
+		return $config;
172
+	}
173
+
174
+	/**
175
+	 * Configure the possible and required options for the Config.
176
+	 *
177
+	 * This should return a bool to let the resolve_options() know whether the
178
+	 * actual resolving needs to be done or not.
179
+	 *
180
+	 * @since 0.1.0
181
+	 *
182
+	 * @param OptionsResolver $resolver Reference to the OptionsResolver
183
+	 *                                  instance.
184
+	 *
185
+	 * @return bool Whether to do the resolving.
186
+	 * @throws FailedToResolveConfigException If there are errors while processing.
187
+	 */
188
+	protected function configureOptions(OptionsResolver $resolver)
189
+	{
190
+		$defined  = $this->schema->getDefinedOptions();
191
+		$defaults = $this->schema->getDefaultOptions();
192
+		$required = $this->schema->getRequiredOptions();
193
+
194
+		if (! $defined && ! $defaults && ! $required) {
195
+			return false;
196
+		}
197
+
198
+		try {
199
+			if ($defined) {
200
+				$resolver->setDefined($defined);
201
+			}
202
+			if ($defaults) {
203
+				$resolver->setDefaults($defaults);
204
+			}
205
+			if ($required) {
206
+				$resolver->setRequired($required);
207
+			}
208
+		} catch (Exception $exception) {
209
+			throw new FailedToResolveConfigException(
210
+				sprintf(
211
+					_('Error while processing config options: %1$s'),
212
+					$exception->getMessage()
213
+				)
214
+			);
215
+		}
216
+
217
+		return true;
218
+	}
219 219
 }
Please login to merge, or discard this patch.
src/ConfigTrait.php 1 patch
Indentation   +92 added lines, -92 removed lines patch added patch discarded remove patch
@@ -25,103 +25,103 @@
 block discarded – undo
25 25
 trait ConfigTrait
26 26
 {
27 27
 
28
-    /**
29
-     * Reference to the Config object.
30
-     *
31
-     * @since 0.1.2
32
-     *
33
-     * @var ConfigInterface
34
-     */
35
-    protected $config;
28
+	/**
29
+	 * Reference to the Config object.
30
+	 *
31
+	 * @since 0.1.2
32
+	 *
33
+	 * @var ConfigInterface
34
+	 */
35
+	protected $config;
36 36
 
37
-    /**
38
-     * Process the passed-in configuration file.
39
-     *
40
-     * @since 0.1.2
41
-     *
42
-     * @param ConfigInterface $config The Config to process.
43
-     * @param                 string  ... List of keys.
44
-     *
45
-     * @throws FailedToProcessConfigException If the arguments could not be parsed into a Config.
46
-     */
47
-    protected function processConfig(ConfigInterface $config)
48
-    {
49
-        if (func_num_args() > 1) {
50
-            try {
51
-                $keys = func_get_args();
52
-                array_shift($keys);
53
-                $config = $config->getSubConfig($keys);
54
-            } catch (Exception $exception) {
55
-                throw new FailedToProcessConfigException(
56
-                    sprintf(
57
-                        _('Could not process the config with the arguments "%1$s".'),
58
-                        print_r(func_get_args(), true)
59
-                    )
60
-                );
61
-            }
62
-        }
63
-        $this->config = $config;
64
-    }
37
+	/**
38
+	 * Process the passed-in configuration file.
39
+	 *
40
+	 * @since 0.1.2
41
+	 *
42
+	 * @param ConfigInterface $config The Config to process.
43
+	 * @param                 string  ... List of keys.
44
+	 *
45
+	 * @throws FailedToProcessConfigException If the arguments could not be parsed into a Config.
46
+	 */
47
+	protected function processConfig(ConfigInterface $config)
48
+	{
49
+		if (func_num_args() > 1) {
50
+			try {
51
+				$keys = func_get_args();
52
+				array_shift($keys);
53
+				$config = $config->getSubConfig($keys);
54
+			} catch (Exception $exception) {
55
+				throw new FailedToProcessConfigException(
56
+					sprintf(
57
+						_('Could not process the config with the arguments "%1$s".'),
58
+						print_r(func_get_args(), true)
59
+					)
60
+				);
61
+			}
62
+		}
63
+		$this->config = $config;
64
+	}
65 65
 
66
-    /**
67
-     * Check whether the Config has a specific key.
68
-     *
69
-     * To get a value several levels deep, add the keys for each level as a comma-separated list.
70
-     *
71
-     * @since 0.1.2
72
-     * @since 0.1.5 Accepts list of keys.
73
-     *
74
-     * @param string|array $_ List of keys.
75
-     *
76
-     * @return bool Whether the key is known.
77
-     */
78
-    protected function hasConfigKey($_)
79
-    {
80
-        $keys = func_get_args();
66
+	/**
67
+	 * Check whether the Config has a specific key.
68
+	 *
69
+	 * To get a value several levels deep, add the keys for each level as a comma-separated list.
70
+	 *
71
+	 * @since 0.1.2
72
+	 * @since 0.1.5 Accepts list of keys.
73
+	 *
74
+	 * @param string|array $_ List of keys.
75
+	 *
76
+	 * @return bool Whether the key is known.
77
+	 */
78
+	protected function hasConfigKey($_)
79
+	{
80
+		$keys = func_get_args();
81 81
 
82
-        return $this->config->hasKey($keys);
83
-    }
82
+		return $this->config->hasKey($keys);
83
+	}
84 84
 
85
-    /**
86
-     * Get the Config value for a specific key.
87
-     *
88
-     * To get a value several levels deep, add the keys for each level as a comma-separated list.
89
-     *
90
-     * @since 0.1.2
91
-     * @since 0.1.5 Accepts list of keys.
92
-     *
93
-     * @param string|array $_ List of keys.
94
-     *
95
-     * @return mixed Value of the key.
96
-     */
97
-    protected function getConfigKey($_)
98
-    {
99
-        $keys = func_get_args();
85
+	/**
86
+	 * Get the Config value for a specific key.
87
+	 *
88
+	 * To get a value several levels deep, add the keys for each level as a comma-separated list.
89
+	 *
90
+	 * @since 0.1.2
91
+	 * @since 0.1.5 Accepts list of keys.
92
+	 *
93
+	 * @param string|array $_ List of keys.
94
+	 *
95
+	 * @return mixed Value of the key.
96
+	 */
97
+	protected function getConfigKey($_)
98
+	{
99
+		$keys = func_get_args();
100 100
 
101
-        return $this->config->getKey($keys);
102
-    }
101
+		return $this->config->getKey($keys);
102
+	}
103 103
 
104
-    /**
105
-     * Get a (multi-dimensional) array of all the configuration settings.
106
-     *
107
-     * @since 0.1.4
108
-     *
109
-     * @return array All the configuration settings.
110
-     */
111
-    protected function getConfigArray()
112
-    {
113
-        return $this->config->getAll();
114
-    }
104
+	/**
105
+	 * Get a (multi-dimensional) array of all the configuration settings.
106
+	 *
107
+	 * @since 0.1.4
108
+	 *
109
+	 * @return array All the configuration settings.
110
+	 */
111
+	protected function getConfigArray()
112
+	{
113
+		return $this->config->getAll();
114
+	}
115 115
 
116
-    /**
117
-     * Get an array of all the keys that are known by the Config.
118
-     *
119
-     * @since 0.1.2
120
-     *
121
-     * @return array Array of strings containing all the keys.
122
-     */
123
-    protected function getConfigKeys()
124
-    {
125
-        return $this->config->getKeys();
126
-    }
116
+	/**
117
+	 * Get an array of all the keys that are known by the Config.
118
+	 *
119
+	 * @since 0.1.2
120
+	 *
121
+	 * @return array Array of strings containing all the keys.
122
+	 */
123
+	protected function getConfigKeys()
124
+	{
125
+		return $this->config->getKeys();
126
+	}
127 127
 }
Please login to merge, or discard this patch.
src/AbstractConfig.php 1 patch
Indentation   +217 added lines, -217 removed lines patch added patch discarded remove patch
@@ -26,221 +26,221 @@
 block discarded – undo
26 26
 abstract class AbstractConfig extends ArrayObject implements ConfigInterface
27 27
 {
28 28
 
29
-    /**
30
-     * Array of strings that are used as delimiters to parse configuration keys.
31
-     *
32
-     * @since 0.1.6
33
-     *
34
-     * @var array
35
-     */
36
-    protected $delimiter = ['\\', '/', '.'];
37
-
38
-    /**
39
-     * Instantiate the AbstractConfig object.
40
-     *
41
-     * @since 0.1.0
42
-     * @since 0.1.6 Accepts a delimiter to parse configuration keys.
43
-     *
44
-     * @param array                $config    Array with settings.
45
-     * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse
46
-     *                                        configuration keys. Defaults to "\", "/" & ".".
47
-     */
48
-    public function __construct(array $config, $delimiter = null)
49
-    {
50
-        // Make sure the config entries can be accessed as properties.
51
-        parent::__construct($config, ArrayObject::ARRAY_AS_PROPS);
52
-
53
-        if (null !== $delimiter) {
54
-            $this->delimiter = (array)$delimiter;
55
-        }
56
-    }
57
-
58
-    /**
59
-     * Get the value of a specific key.
60
-     *
61
-     * To get a value several levels deep, add the keys for each level as a comma-separated list.
62
-     *
63
-     * @since 0.1.0
64
-     * @since 0.1.4 Accepts list of keys.
65
-     *
66
-     * @param string|array $_ List of keys.
67
-     *
68
-     * @return mixed
69
-     * @throws KeyNotFoundException If an unknown key is requested.
70
-     */
71
-    public function getKey($_)
72
-    {
73
-        $keys = $this->validateKeys(func_get_args());
74
-
75
-        $keys  = array_reverse($keys);
76
-        $array = $this->getArrayCopy();
77
-        while (count($keys) > 0) {
78
-            $key   = array_pop($keys);
79
-            $array = $array[$key];
80
-        }
81
-
82
-        return $array;
83
-    }
84
-
85
-    /**
86
-     * Check whether the Config has a specific key.
87
-     *
88
-     * To check a value several levels deep, add the keys for each level as a comma-separated list.
89
-     *
90
-     * @since 0.1.0
91
-     * @since 0.1.4 Accepts list of keys.
92
-     *
93
-     * @param string|array $_ List of keys.
94
-     *
95
-     * @return bool
96
-     */
97
-    public function hasKey($_)
98
-    {
99
-        try {
100
-            $keys = array_reverse($this->getKeyArguments(func_get_args()));
101
-
102
-            $array = $this->getArrayCopy();
103
-            while (count($keys) > 0) {
104
-                $key = array_pop($keys);
105
-                if (! array_key_exists($key, $array)) {
106
-                    return false;
107
-                }
108
-                $array = $array[$key];
109
-            }
110
-        } catch (Exception $exception) {
111
-            return false;
112
-        }
113
-
114
-        return true;
115
-    }
116
-
117
-    /**
118
-     * Get a (multi-dimensional) array of all the configuration settings.
119
-     *
120
-     * @since 0.1.4
121
-     *
122
-     * @return array
123
-     */
124
-    public function getAll()
125
-    {
126
-        return $this->getArrayCopy();
127
-    }
128
-
129
-    /**
130
-     * Get the an array with all the keys
131
-     *
132
-     * @since 0.1.0
133
-     * @return array
134
-     */
135
-    public function getKeys()
136
-    {
137
-        return array_keys((array)$this);
138
-    }
139
-
140
-    /**
141
-     * Get a new config at a specific sub-level.
142
-     *
143
-     * @since 0.1.13
144
-     *
145
-     * @param string|array $_ List of keys.
146
-     *
147
-     * @return ConfigInterface
148
-     * @throws KeyNotFoundException If an unknown key is requested.
149
-     */
150
-    public function getSubConfig($_)
151
-    {
152
-        $keys = $this->validateKeys(func_get_args());
153
-
154
-        $subConfig = clone $this;
155
-        $subConfig->reduceToSubKey($keys);
156
-
157
-        return $subConfig;
158
-    }
159
-
160
-    /**
161
-     * Validate a set of keys to make sure they exist.
162
-     *
163
-     * @since 0.1.13
164
-     *
165
-     * @param string|array $_ List of keys.
166
-     *
167
-     * @return array List of keys.
168
-     * @throws KeyNotFoundException If an unknown key is requested.
169
-     */
170
-    public function validateKeys($_)
171
-    {
172
-        $keys = $this->getKeyArguments(func_get_args());
173
-
174
-        if (! $this->hasKey($keys)) {
175
-            throw new KeyNotFoundException(
176
-                sprintf(
177
-                    _('The configuration key %1$s does not exist.'),
178
-                    implode('->', $keys)
179
-                )
180
-            );
181
-        }
182
-
183
-        return $keys;
184
-    }
185
-
186
-    /**
187
-     * Reduce the currently stored config array to a subarray at a specific level.
188
-     *
189
-     * @since 0.1.13
190
-     *
191
-     * @param array $keys Array of keys that point to a key down in the hierarchy.
192
-     */
193
-    protected function reduceToSubKey(array $keys)
194
-    {
195
-        $this->exchangeArray($this->getKey($keys));
196
-    }
197
-
198
-    /**
199
-     * Recursively extract the configuration key arguments from an arbitrary array.
200
-     *
201
-     * @since 0.1.6
202
-     *
203
-     * @param array $arguments Array as fetched through get_func_args().
204
-     *
205
-     * @return array Array of strings.
206
-     */
207
-    protected function getKeyArguments($arguments)
208
-    {
209
-        $keys = [];
210
-        foreach ($arguments as $argument) {
211
-            if (is_array($argument)) {
212
-                $keys = array_merge($keys, $this->getKeyArguments($argument));
213
-            }
214
-            if (is_string($argument)) {
215
-                $keys = array_merge($keys, $this->parseKeysString($argument));
216
-            }
217
-        }
218
-
219
-        return $keys;
220
-    }
221
-
222
-    /**
223
-     * Extract individual keys from a delimited string.
224
-     *
225
-     * @since 0.1.6
226
-     *
227
-     * @param string $keyString Delimited string of keys.
228
-     *
229
-     * @return array Array of key strings.
230
-     */
231
-    protected function parseKeysString($keyString)
232
-    {
233
-        // Replace all of the configured delimiters by the first one, so that we can then use explode().
234
-        $normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString);
235
-
236
-        return (array)explode($this->delimiter[0], $normalizedString);
237
-    }
238
-
239
-    /**
240
-     * Validate the Config file.
241
-     *
242
-     * @since  0.1.0
243
-     * @return boolean
244
-     */
245
-    abstract public function isValid();
29
+	/**
30
+	 * Array of strings that are used as delimiters to parse configuration keys.
31
+	 *
32
+	 * @since 0.1.6
33
+	 *
34
+	 * @var array
35
+	 */
36
+	protected $delimiter = ['\\', '/', '.'];
37
+
38
+	/**
39
+	 * Instantiate the AbstractConfig object.
40
+	 *
41
+	 * @since 0.1.0
42
+	 * @since 0.1.6 Accepts a delimiter to parse configuration keys.
43
+	 *
44
+	 * @param array                $config    Array with settings.
45
+	 * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse
46
+	 *                                        configuration keys. Defaults to "\", "/" & ".".
47
+	 */
48
+	public function __construct(array $config, $delimiter = null)
49
+	{
50
+		// Make sure the config entries can be accessed as properties.
51
+		parent::__construct($config, ArrayObject::ARRAY_AS_PROPS);
52
+
53
+		if (null !== $delimiter) {
54
+			$this->delimiter = (array)$delimiter;
55
+		}
56
+	}
57
+
58
+	/**
59
+	 * Get the value of a specific key.
60
+	 *
61
+	 * To get a value several levels deep, add the keys for each level as a comma-separated list.
62
+	 *
63
+	 * @since 0.1.0
64
+	 * @since 0.1.4 Accepts list of keys.
65
+	 *
66
+	 * @param string|array $_ List of keys.
67
+	 *
68
+	 * @return mixed
69
+	 * @throws KeyNotFoundException If an unknown key is requested.
70
+	 */
71
+	public function getKey($_)
72
+	{
73
+		$keys = $this->validateKeys(func_get_args());
74
+
75
+		$keys  = array_reverse($keys);
76
+		$array = $this->getArrayCopy();
77
+		while (count($keys) > 0) {
78
+			$key   = array_pop($keys);
79
+			$array = $array[$key];
80
+		}
81
+
82
+		return $array;
83
+	}
84
+
85
+	/**
86
+	 * Check whether the Config has a specific key.
87
+	 *
88
+	 * To check a value several levels deep, add the keys for each level as a comma-separated list.
89
+	 *
90
+	 * @since 0.1.0
91
+	 * @since 0.1.4 Accepts list of keys.
92
+	 *
93
+	 * @param string|array $_ List of keys.
94
+	 *
95
+	 * @return bool
96
+	 */
97
+	public function hasKey($_)
98
+	{
99
+		try {
100
+			$keys = array_reverse($this->getKeyArguments(func_get_args()));
101
+
102
+			$array = $this->getArrayCopy();
103
+			while (count($keys) > 0) {
104
+				$key = array_pop($keys);
105
+				if (! array_key_exists($key, $array)) {
106
+					return false;
107
+				}
108
+				$array = $array[$key];
109
+			}
110
+		} catch (Exception $exception) {
111
+			return false;
112
+		}
113
+
114
+		return true;
115
+	}
116
+
117
+	/**
118
+	 * Get a (multi-dimensional) array of all the configuration settings.
119
+	 *
120
+	 * @since 0.1.4
121
+	 *
122
+	 * @return array
123
+	 */
124
+	public function getAll()
125
+	{
126
+		return $this->getArrayCopy();
127
+	}
128
+
129
+	/**
130
+	 * Get the an array with all the keys
131
+	 *
132
+	 * @since 0.1.0
133
+	 * @return array
134
+	 */
135
+	public function getKeys()
136
+	{
137
+		return array_keys((array)$this);
138
+	}
139
+
140
+	/**
141
+	 * Get a new config at a specific sub-level.
142
+	 *
143
+	 * @since 0.1.13
144
+	 *
145
+	 * @param string|array $_ List of keys.
146
+	 *
147
+	 * @return ConfigInterface
148
+	 * @throws KeyNotFoundException If an unknown key is requested.
149
+	 */
150
+	public function getSubConfig($_)
151
+	{
152
+		$keys = $this->validateKeys(func_get_args());
153
+
154
+		$subConfig = clone $this;
155
+		$subConfig->reduceToSubKey($keys);
156
+
157
+		return $subConfig;
158
+	}
159
+
160
+	/**
161
+	 * Validate a set of keys to make sure they exist.
162
+	 *
163
+	 * @since 0.1.13
164
+	 *
165
+	 * @param string|array $_ List of keys.
166
+	 *
167
+	 * @return array List of keys.
168
+	 * @throws KeyNotFoundException If an unknown key is requested.
169
+	 */
170
+	public function validateKeys($_)
171
+	{
172
+		$keys = $this->getKeyArguments(func_get_args());
173
+
174
+		if (! $this->hasKey($keys)) {
175
+			throw new KeyNotFoundException(
176
+				sprintf(
177
+					_('The configuration key %1$s does not exist.'),
178
+					implode('->', $keys)
179
+				)
180
+			);
181
+		}
182
+
183
+		return $keys;
184
+	}
185
+
186
+	/**
187
+	 * Reduce the currently stored config array to a subarray at a specific level.
188
+	 *
189
+	 * @since 0.1.13
190
+	 *
191
+	 * @param array $keys Array of keys that point to a key down in the hierarchy.
192
+	 */
193
+	protected function reduceToSubKey(array $keys)
194
+	{
195
+		$this->exchangeArray($this->getKey($keys));
196
+	}
197
+
198
+	/**
199
+	 * Recursively extract the configuration key arguments from an arbitrary array.
200
+	 *
201
+	 * @since 0.1.6
202
+	 *
203
+	 * @param array $arguments Array as fetched through get_func_args().
204
+	 *
205
+	 * @return array Array of strings.
206
+	 */
207
+	protected function getKeyArguments($arguments)
208
+	{
209
+		$keys = [];
210
+		foreach ($arguments as $argument) {
211
+			if (is_array($argument)) {
212
+				$keys = array_merge($keys, $this->getKeyArguments($argument));
213
+			}
214
+			if (is_string($argument)) {
215
+				$keys = array_merge($keys, $this->parseKeysString($argument));
216
+			}
217
+		}
218
+
219
+		return $keys;
220
+	}
221
+
222
+	/**
223
+	 * Extract individual keys from a delimited string.
224
+	 *
225
+	 * @since 0.1.6
226
+	 *
227
+	 * @param string $keyString Delimited string of keys.
228
+	 *
229
+	 * @return array Array of key strings.
230
+	 */
231
+	protected function parseKeysString($keyString)
232
+	{
233
+		// Replace all of the configured delimiters by the first one, so that we can then use explode().
234
+		$normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString);
235
+
236
+		return (array)explode($this->delimiter[0], $normalizedString);
237
+	}
238
+
239
+	/**
240
+	 * Validate the Config file.
241
+	 *
242
+	 * @since  0.1.0
243
+	 * @return boolean
244
+	 */
245
+	abstract public function isValid();
246 246
 }
Please login to merge, or discard this patch.