Passed
Push — master ( 8b5940...060029 )
by Alain
02:31
created
src/ConfigTrait.php 1 patch
Indentation   +99 added lines, -99 removed lines patch added patch discarded remove patch
@@ -1,13 +1,13 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * Config Trait
4
- *
5
- * @package   BrightNucleus\Config
6
- * @author    Alain Schlesser <[email protected]>
7
- * @license   GPL-2.0+
8
- * @link      http://www.brightnucleus.com/
9
- * @copyright 2016 Alain Schlesser, Bright Nucleus
10
- */
3
+	 * Config Trait
4
+	 *
5
+	 * @package   BrightNucleus\Config
6
+	 * @author    Alain Schlesser <[email protected]>
7
+	 * @license   GPL-2.0+
8
+	 * @link      http://www.brightnucleus.com/
9
+	 * @copyright 2016 Alain Schlesser, Bright Nucleus
10
+	 */
11 11
 
12 12
 namespace BrightNucleus\Config;
13 13
 
@@ -18,102 +18,102 @@  discard block
 block discarded – undo
18 18
 trait ConfigTrait
19 19
 {
20 20
 
21
-    /**
22
-     * Reference to the Config object.
23
-     *
24
-     * @since 0.1.2
25
-     *
26
-     * @var ConfigInterface
27
-     */
28
-    protected $config;
21
+	/**
22
+	 * Reference to the Config object.
23
+	 *
24
+	 * @since 0.1.2
25
+	 *
26
+	 * @var ConfigInterface
27
+	 */
28
+	protected $config;
29 29
 
30
-    /**
31
-     * Process the passed-in configuration file.
32
-     *
33
-     * @since 0.1.2
34
-     *
35
-     * @param ConfigInterface $config The Config to process.
36
-     * @param                 string  ... List of keys.
37
-     * @throws RuntimeException If the arguments could not be parsed into a Config.
38
-     */
39
-    protected function processConfig(ConfigInterface $config)
40
-    {
41
-        if (func_num_args() > 1) {
42
-            try {
43
-                $keys = func_get_args();
44
-                array_shift($keys);
45
-                $config = $config->getSubConfig($keys);
46
-            } catch (Exception $exception) {
47
-                throw new RuntimeException(
48
-                    sprintf(
49
-                        _('Could not process the config with the arguments "%1$s".'),
50
-                        print_r(func_get_args(), true)
51
-                    )
52
-                );
53
-            }
54
-        }
55
-        $this->config = $config;
56
-    }
30
+	/**
31
+	 * Process the passed-in configuration file.
32
+	 *
33
+	 * @since 0.1.2
34
+	 *
35
+	 * @param ConfigInterface $config The Config to process.
36
+	 * @param                 string  ... List of keys.
37
+	 * @throws RuntimeException If the arguments could not be parsed into a Config.
38
+	 */
39
+	protected function processConfig(ConfigInterface $config)
40
+	{
41
+		if (func_num_args() > 1) {
42
+			try {
43
+				$keys = func_get_args();
44
+				array_shift($keys);
45
+				$config = $config->getSubConfig($keys);
46
+			} catch (Exception $exception) {
47
+				throw new RuntimeException(
48
+					sprintf(
49
+						_('Could not process the config with the arguments "%1$s".'),
50
+						print_r(func_get_args(), true)
51
+					)
52
+				);
53
+			}
54
+		}
55
+		$this->config = $config;
56
+	}
57 57
 
58
-    /**
59
-     * Check whether the Config has 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.2
64
-     * @since 0.1.5 Accepts list of keys.
65
-     *
66
-     * @param string|array $_ List of keys.
67
-     * @return bool Whether the key is known.
68
-     */
69
-    protected function hasConfigKey($_)
70
-    {
71
-        $keys = func_get_args();
72
-        Assert\that($keys)->all()->string()->notEmpty();
58
+	/**
59
+	 * Check whether the Config has 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.2
64
+	 * @since 0.1.5 Accepts list of keys.
65
+	 *
66
+	 * @param string|array $_ List of keys.
67
+	 * @return bool Whether the key is known.
68
+	 */
69
+	protected function hasConfigKey($_)
70
+	{
71
+		$keys = func_get_args();
72
+		Assert\that($keys)->all()->string()->notEmpty();
73 73
 
74
-        return $this->config->hasKey($keys);
75
-    }
74
+		return $this->config->hasKey($keys);
75
+	}
76 76
 
77
-    /**
78
-     * Get the Config value for a specific key.
79
-     *
80
-     * To get a value several levels deep, add the keys for each level as a comma-separated list.
81
-     *
82
-     * @since 0.1.2
83
-     * @since 0.1.5 Accepts list of keys.
84
-     *
85
-     * @param string|array $_ List of keys.
86
-     * @return mixed Value of the key.
87
-     */
88
-    protected function getConfigKey($_)
89
-    {
90
-        $keys = func_get_args();
91
-        Assert\that($keys)->all()->string()->notEmpty();
77
+	/**
78
+	 * Get the Config value for a specific key.
79
+	 *
80
+	 * To get a value several levels deep, add the keys for each level as a comma-separated list.
81
+	 *
82
+	 * @since 0.1.2
83
+	 * @since 0.1.5 Accepts list of keys.
84
+	 *
85
+	 * @param string|array $_ List of keys.
86
+	 * @return mixed Value of the key.
87
+	 */
88
+	protected function getConfigKey($_)
89
+	{
90
+		$keys = func_get_args();
91
+		Assert\that($keys)->all()->string()->notEmpty();
92 92
 
93
-        return $this->config->getKey($keys);
94
-    }
93
+		return $this->config->getKey($keys);
94
+	}
95 95
 
96
-    /**
97
-     * Get a (multi-dimensional) array of all the configuration settings.
98
-     *
99
-     * @since 0.1.4
100
-     *
101
-     * @return array All the configuration settings.
102
-     */
103
-    protected function getConfigArray()
104
-    {
105
-        return $this->config->getAll();
106
-    }
96
+	/**
97
+	 * Get a (multi-dimensional) array of all the configuration settings.
98
+	 *
99
+	 * @since 0.1.4
100
+	 *
101
+	 * @return array All the configuration settings.
102
+	 */
103
+	protected function getConfigArray()
104
+	{
105
+		return $this->config->getAll();
106
+	}
107 107
 
108
-    /**
109
-     * Get an array of all the keys that are known by the Config.
110
-     *
111
-     * @since 0.1.2
112
-     *
113
-     * @return array Array of strings containing all the keys.
114
-     */
115
-    protected function getConfigKeys()
116
-    {
117
-        return $this->config->getKeys();
118
-    }
108
+	/**
109
+	 * Get an array of all the keys that are known by the Config.
110
+	 *
111
+	 * @since 0.1.2
112
+	 *
113
+	 * @return array Array of strings containing all the keys.
114
+	 */
115
+	protected function getConfigKeys()
116
+	{
117
+		return $this->config->getKeys();
118
+	}
119 119
 }
Please login to merge, or discard this patch.
src/AbstractConfig.php 1 patch
Indentation   +229 added lines, -229 removed lines patch added patch discarded remove patch
@@ -1,13 +1,13 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * Abstract Config Object
4
- *
5
- * @package   BrightNucleus\Config
6
- * @author    Alain Schlesser <[email protected]>
7
- * @license   GPL-2.0+
8
- * @link      http://www.brightnucleus.com/
9
- * @copyright 2016 Alain Schlesser, Bright Nucleus
10
- */
3
+	 * Abstract Config Object
4
+	 *
5
+	 * @package   BrightNucleus\Config
6
+	 * @author    Alain Schlesser <[email protected]>
7
+	 * @license   GPL-2.0+
8
+	 * @link      http://www.brightnucleus.com/
9
+	 * @copyright 2016 Alain Schlesser, Bright Nucleus
10
+	 */
11 11
 
12 12
 namespace BrightNucleus\Config;
13 13
 
@@ -28,225 +28,225 @@  discard block
 block discarded – undo
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
-     * 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|array $_ 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
-    public function getKey($_)
74
-    {
75
-        $keys = $this->validateKeys(func_get_args());
76
-
77
-        $keys  = array_reverse($keys);
78
-        $array = $this->getArrayCopy();
79
-        while (count($keys) > 0) {
80
-            $key   = array_pop($keys);
81
-            $array = $array[$key];
82
-        }
83
-
84
-        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|array $_ List of keys.
96
-     * @return bool
97
-     */
98
-    public function hasKey($_)
99
-    {
100
-        try {
101
-            $keys = array_reverse($this->getKeyArguments(func_get_args()));
102
-
103
-            $array = $this->getArrayCopy();
104
-            while (count($keys) > 0) {
105
-                $key = array_pop($keys);
106
-                if (! array_key_exists($key, $array)) {
107
-                    return false;
108
-                }
109
-                $array = $array[$key];
110
-            }
111
-        } catch (Exception $exception) {
112
-            return false;
113
-        }
114
-
115
-        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
-    public function getAll()
126
-    {
127
-        return $this->getArrayCopy();
128
-    }
129
-
130
-    /**
131
-     * Get the an array with all the keys
132
-     *
133
-     * @since 0.1.0
134
-     * @return array
135
-     */
136
-    public function getKeys()
137
-    {
138
-        return array_keys((array)$this);
139
-    }
140
-
141
-    /**
142
-     * Get a new config at a specific sub-level.
143
-     *
144
-     * @since 0.1.13
145
-     *
146
-     * @param string|array $_ List of keys.
147
-     * @return ConfigInterface
148
-     * @throws BadMethodCallException If no argument was provided.
149
-     * @throws OutOfRangeException If an unknown key is requested.
150
-     */
151
-    public function getSubConfig($_)
152
-    {
153
-        $keys = $this->validateKeys(func_get_args());
154
-
155
-        $subConfig = clone $this;
156
-        $subConfig->reduceToSubKey($keys);
157
-
158
-        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|array $_ 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
-    public function validateKeys($_)
172
-    {
173
-        $keys = $this->getKeyArguments(func_get_args());
174
-
175
-        Assert\that($keys)->all()->string()->notEmpty();
176
-
177
-        if (! $this->hasKey($keys)) {
178
-            throw new OutOfRangeException(
179
-                sprintf(
180
-                    _('The configuration key %1$s does not exist.'),
181
-                    implode('->', $keys)
182
-                )
183
-            );
184
-        }
185
-
186
-        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
-    protected function reduceToSubKey(array $keys)
197
-    {
198
-        $this->exchangeArray($this->getKey($keys));
199
-    }
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
-    protected function getKeyArguments($arguments)
211
-    {
212
-        Assert\that($arguments)->isArray()->notEmpty();
213
-
214
-        $keys = [];
215
-        foreach ($arguments as $argument) {
216
-            if (is_array($argument)) {
217
-                $keys = array_merge($keys, $this->getKeyArguments($argument));
218
-            }
219
-            if (is_string($argument)) {
220
-                $keys = array_merge($keys, $this->parseKeysString($argument));
221
-            }
222
-        }
223
-
224
-        return $keys;
225
-    }
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
-    protected function parseKeysString($keyString)
236
-    {
237
-        Assert\that($keyString)->string()->notEmpty();
238
-
239
-        // Replace all of the configured delimiters by the first one, so that we can then use explode().
240
-        $normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString);
241
-
242
-        return (array)explode($this->delimiter[0], $normalizedString);
243
-    }
244
-
245
-    /**
246
-     * Validate the Config file.
247
-     *
248
-     * @since  0.1.0
249
-     * @return boolean
250
-     */
251
-    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
+	 * 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|array $_ 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
+	public function getKey($_)
74
+	{
75
+		$keys = $this->validateKeys(func_get_args());
76
+
77
+		$keys  = array_reverse($keys);
78
+		$array = $this->getArrayCopy();
79
+		while (count($keys) > 0) {
80
+			$key   = array_pop($keys);
81
+			$array = $array[$key];
82
+		}
83
+
84
+		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|array $_ List of keys.
96
+	 * @return bool
97
+	 */
98
+	public function hasKey($_)
99
+	{
100
+		try {
101
+			$keys = array_reverse($this->getKeyArguments(func_get_args()));
102
+
103
+			$array = $this->getArrayCopy();
104
+			while (count($keys) > 0) {
105
+				$key = array_pop($keys);
106
+				if (! array_key_exists($key, $array)) {
107
+					return false;
108
+				}
109
+				$array = $array[$key];
110
+			}
111
+		} catch (Exception $exception) {
112
+			return false;
113
+		}
114
+
115
+		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
+	public function getAll()
126
+	{
127
+		return $this->getArrayCopy();
128
+	}
129
+
130
+	/**
131
+	 * Get the an array with all the keys
132
+	 *
133
+	 * @since 0.1.0
134
+	 * @return array
135
+	 */
136
+	public function getKeys()
137
+	{
138
+		return array_keys((array)$this);
139
+	}
140
+
141
+	/**
142
+	 * Get a new config at a specific sub-level.
143
+	 *
144
+	 * @since 0.1.13
145
+	 *
146
+	 * @param string|array $_ List of keys.
147
+	 * @return ConfigInterface
148
+	 * @throws BadMethodCallException If no argument was provided.
149
+	 * @throws OutOfRangeException If an unknown key is requested.
150
+	 */
151
+	public function getSubConfig($_)
152
+	{
153
+		$keys = $this->validateKeys(func_get_args());
154
+
155
+		$subConfig = clone $this;
156
+		$subConfig->reduceToSubKey($keys);
157
+
158
+		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|array $_ 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
+	public function validateKeys($_)
172
+	{
173
+		$keys = $this->getKeyArguments(func_get_args());
174
+
175
+		Assert\that($keys)->all()->string()->notEmpty();
176
+
177
+		if (! $this->hasKey($keys)) {
178
+			throw new OutOfRangeException(
179
+				sprintf(
180
+					_('The configuration key %1$s does not exist.'),
181
+					implode('->', $keys)
182
+				)
183
+			);
184
+		}
185
+
186
+		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
+	protected function reduceToSubKey(array $keys)
197
+	{
198
+		$this->exchangeArray($this->getKey($keys));
199
+	}
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
+	protected function getKeyArguments($arguments)
211
+	{
212
+		Assert\that($arguments)->isArray()->notEmpty();
213
+
214
+		$keys = [];
215
+		foreach ($arguments as $argument) {
216
+			if (is_array($argument)) {
217
+				$keys = array_merge($keys, $this->getKeyArguments($argument));
218
+			}
219
+			if (is_string($argument)) {
220
+				$keys = array_merge($keys, $this->parseKeysString($argument));
221
+			}
222
+		}
223
+
224
+		return $keys;
225
+	}
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
+	protected function parseKeysString($keyString)
236
+	{
237
+		Assert\that($keyString)->string()->notEmpty();
238
+
239
+		// Replace all of the configured delimiters by the first one, so that we can then use explode().
240
+		$normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString);
241
+
242
+		return (array)explode($this->delimiter[0], $normalizedString);
243
+	}
244
+
245
+	/**
246
+	 * Validate the Config file.
247
+	 *
248
+	 * @since  0.1.0
249
+	 * @return boolean
250
+	 */
251
+	abstract public function isValid();
252 252
 }
Please login to merge, or discard this patch.