@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Abstract Config Schema Class. |
|
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 Schema Class. |
|
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 | |
@@ -24,84 +24,84 @@ discard block |
||
24 | 24 | abstract class AbstractConfigSchema implements ConfigSchemaInterface |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * The defined values that are recognized. |
|
29 | - * |
|
30 | - * @var ConfigInterface |
|
31 | - */ |
|
32 | - protected $defined; |
|
27 | + /** |
|
28 | + * The defined values that are recognized. |
|
29 | + * |
|
30 | + * @var ConfigInterface |
|
31 | + */ |
|
32 | + protected $defined; |
|
33 | 33 | |
34 | - /** |
|
35 | - * The default values that can be overwritten. |
|
36 | - * |
|
37 | - * @var ConfigInterface |
|
38 | - */ |
|
39 | - protected $defaults; |
|
34 | + /** |
|
35 | + * The default values that can be overwritten. |
|
36 | + * |
|
37 | + * @var ConfigInterface |
|
38 | + */ |
|
39 | + protected $defaults; |
|
40 | 40 | |
41 | - /** |
|
42 | - * The required values that need to be set. |
|
43 | - * |
|
44 | - * @var ConfigInterface |
|
45 | - */ |
|
46 | - protected $required; |
|
41 | + /** |
|
42 | + * The required values that need to be set. |
|
43 | + * |
|
44 | + * @var ConfigInterface |
|
45 | + */ |
|
46 | + protected $required; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Get the set of defined options. |
|
50 | - * |
|
51 | - * @since 0.1.0 |
|
52 | - * |
|
53 | - * @return array|null |
|
54 | - */ |
|
55 | - public function getDefinedOptions() |
|
56 | - { |
|
57 | - if ( ! $this->defined) { |
|
58 | - return null; |
|
59 | - } |
|
48 | + /** |
|
49 | + * Get the set of defined options. |
|
50 | + * |
|
51 | + * @since 0.1.0 |
|
52 | + * |
|
53 | + * @return array|null |
|
54 | + */ |
|
55 | + public function getDefinedOptions() |
|
56 | + { |
|
57 | + if ( ! $this->defined) { |
|
58 | + return null; |
|
59 | + } |
|
60 | 60 | |
61 | - if ($this->defined instanceof ConfigInterface) { |
|
62 | - return $this->defined->getArrayCopy(); |
|
63 | - } |
|
61 | + if ($this->defined instanceof ConfigInterface) { |
|
62 | + return $this->defined->getArrayCopy(); |
|
63 | + } |
|
64 | 64 | |
65 | - return (array)$this->defined; |
|
66 | - } |
|
65 | + return (array)$this->defined; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Get the set of default options. |
|
70 | - * |
|
71 | - * @since 0.1.0 |
|
72 | - * |
|
73 | - * @return array|null |
|
74 | - */ |
|
75 | - public function getDefaultOptions() |
|
76 | - { |
|
77 | - if ( ! $this->defaults) { |
|
78 | - return null; |
|
79 | - } |
|
68 | + /** |
|
69 | + * Get the set of default options. |
|
70 | + * |
|
71 | + * @since 0.1.0 |
|
72 | + * |
|
73 | + * @return array|null |
|
74 | + */ |
|
75 | + public function getDefaultOptions() |
|
76 | + { |
|
77 | + if ( ! $this->defaults) { |
|
78 | + return null; |
|
79 | + } |
|
80 | 80 | |
81 | - if ($this->defaults instanceof ConfigInterface) { |
|
82 | - return $this->defaults->getArrayCopy(); |
|
83 | - } |
|
81 | + if ($this->defaults instanceof ConfigInterface) { |
|
82 | + return $this->defaults->getArrayCopy(); |
|
83 | + } |
|
84 | 84 | |
85 | - return (array)$this->defaults; |
|
86 | - } |
|
85 | + return (array)$this->defaults; |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Get the set of required options. |
|
90 | - * |
|
91 | - * @since 0.1.0 |
|
92 | - * |
|
93 | - * @return array|null |
|
94 | - */ |
|
95 | - public function getRequiredOptions() |
|
96 | - { |
|
97 | - if ( ! $this->required) { |
|
98 | - return null; |
|
99 | - } |
|
88 | + /** |
|
89 | + * Get the set of required options. |
|
90 | + * |
|
91 | + * @since 0.1.0 |
|
92 | + * |
|
93 | + * @return array|null |
|
94 | + */ |
|
95 | + public function getRequiredOptions() |
|
96 | + { |
|
97 | + if ( ! $this->required) { |
|
98 | + return null; |
|
99 | + } |
|
100 | 100 | |
101 | - if ($this->required instanceof ConfigInterface) { |
|
102 | - return $this->required->getArrayCopy(); |
|
103 | - } |
|
101 | + if ($this->required instanceof ConfigInterface) { |
|
102 | + return $this->required->getArrayCopy(); |
|
103 | + } |
|
104 | 104 | |
105 | - return (array)$this->required; |
|
106 | - } |
|
105 | + return (array)$this->required; |
|
106 | + } |
|
107 | 107 | } |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | return $this->defined->getArrayCopy(); |
63 | 63 | } |
64 | 64 | |
65 | - return (array)$this->defined; |
|
65 | + return (array) $this->defined; |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | /** |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | return $this->defaults->getArrayCopy(); |
83 | 83 | } |
84 | 84 | |
85 | - return (array)$this->defaults; |
|
85 | + return (array) $this->defaults; |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | /** |
@@ -102,6 +102,6 @@ discard block |
||
102 | 102 | return $this->required->getArrayCopy(); |
103 | 103 | } |
104 | 104 | |
105 | - return (array)$this->required; |
|
105 | + return (array) $this->required; |
|
106 | 106 | } |
107 | 107 | } |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | if ( ! file_exists($config)) { |
119 | 119 | throw new RuntimeException(sprintf( |
120 | 120 | _('Non-existing configuration source: %1$s'), |
121 | - (string)$config |
|
121 | + (string) $config |
|
122 | 122 | )); |
123 | 123 | } |
124 | 124 | |
@@ -137,13 +137,13 @@ discard block |
||
137 | 137 | } catch (Exception $exception) { |
138 | 138 | throw new RuntimeException(sprintf( |
139 | 139 | _('Loading from configuration source %1$s failed. Reason: %2$s'), |
140 | - (string)$configString, |
|
141 | - (string)$exception->getMessage() |
|
140 | + (string) $configString, |
|
141 | + (string) $exception->getMessage() |
|
142 | 142 | )); |
143 | 143 | } |
144 | 144 | } |
145 | 145 | |
146 | - return (array)$config; |
|
146 | + return (array) $config; |
|
147 | 147 | } |
148 | 148 | |
149 | 149 | /** |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Generic Config contract implementation |
|
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 | + * Generic Config contract implementation |
|
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 | |
@@ -30,213 +30,213 @@ discard block |
||
30 | 30 | class Config extends AbstractConfig |
31 | 31 | { |
32 | 32 | |
33 | - /** |
|
34 | - * The schema of the Config file. |
|
35 | - * |
|
36 | - * @var Schema |
|
37 | - */ |
|
38 | - protected $schema; |
|
39 | - |
|
40 | - /** |
|
41 | - * The Validator class that gets asked to do the validation of the config. |
|
42 | - * |
|
43 | - * @since 0.1.0 |
|
44 | - * |
|
45 | - * @var Validator |
|
46 | - */ |
|
47 | - protected $validator; |
|
48 | - |
|
49 | - /** |
|
50 | - * Instantiate the Config object. |
|
51 | - * |
|
52 | - * It accepts either an array with the configuration settings, or a |
|
53 | - * filename pointing to a PHP file it can include. |
|
54 | - * |
|
55 | - * @since 0.1.0 |
|
56 | - * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
|
57 | - * |
|
58 | - * @param array|string $config Array with settings or filename for the |
|
59 | - * settings file. |
|
60 | - * @param Schema|null $schema Optional. Config that contains default |
|
61 | - * values that can get overwritten. |
|
62 | - * @param Validator|null $validator Optional. Validator class that does the |
|
63 | - * actual validation. |
|
64 | - * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
|
65 | - * configuration keys. Defaults to "\", "/" & ".". |
|
66 | - * @throws InvalidArgumentException If the config source is not a string or |
|
67 | - * array. |
|
68 | - * @throws RuntimeException If loading of the config source failed. |
|
69 | - * @throws UnexpectedValueException If the config file is not valid. |
|
70 | - */ |
|
71 | - public function __construct( |
|
72 | - $config, |
|
73 | - Schema $schema = null, |
|
74 | - Validator $validator = null, |
|
75 | - $delimiter = null |
|
76 | - ) { |
|
77 | - $this->schema = $schema; |
|
78 | - $this->validator = $validator; |
|
79 | - |
|
80 | - // Make sure $config is either a string or array. |
|
81 | - if ( ! (is_string($config) || is_array($config))) { |
|
82 | - throw new InvalidArgumentException(sprintf( |
|
83 | - _('Invalid configuration source: %1$s'), |
|
84 | - print_r($config, true) |
|
85 | - )); |
|
86 | - } |
|
87 | - |
|
88 | - if (is_string($config)) { |
|
89 | - $config = $this->fetchArrayData($config); |
|
90 | - } |
|
91 | - |
|
92 | - $config = $this->resolveOptions($config); |
|
93 | - |
|
94 | - parent::__construct($config, $delimiter); |
|
95 | - |
|
96 | - // Finally, validate the resulting config. |
|
97 | - if ( ! $this->isValid()) { |
|
98 | - throw new UnexpectedValueException(sprintf( |
|
99 | - _('ConfigInterface file is not valid: %1$s'), |
|
100 | - print_r($config, true) |
|
101 | - )); |
|
102 | - } |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Fetch array data from a string pointing to a file. |
|
107 | - * |
|
108 | - * @since 0.1.0 |
|
109 | - * |
|
110 | - * @param string $config Filename for the settings file. |
|
111 | - * @return array Array with configuration settings. |
|
112 | - * @throws RuntimeException If the config source is a non-existing |
|
113 | - * file. |
|
114 | - * @throws RuntimeException If loading of the config source failed. |
|
115 | - */ |
|
116 | - protected function fetchArrayData($config) |
|
117 | - { |
|
118 | - if (is_string($config) && ! ('' === $config)) { |
|
119 | - |
|
120 | - // $config is a valid string, make sure it is an existing file. |
|
121 | - if ( ! file_exists($config)) { |
|
122 | - throw new RuntimeException(sprintf( |
|
123 | - _('Non-existing configuration source: %1$s'), |
|
124 | - (string)$config |
|
125 | - )); |
|
126 | - } |
|
127 | - |
|
128 | - // Try to load the file through PHP's include(). |
|
129 | - $configString = $config; |
|
130 | - try { |
|
131 | - // Make sure we don't accidentally create output. |
|
132 | - ob_get_contents(); |
|
133 | - $config = include($configString); |
|
134 | - ob_clean(); |
|
135 | - |
|
136 | - // The included should return an array. |
|
137 | - if ( ! is_array($config)) { |
|
138 | - throw new RuntimeException(_('File inclusion did not return an array.')); |
|
139 | - } |
|
140 | - } catch (Exception $exception) { |
|
141 | - throw new RuntimeException(sprintf( |
|
142 | - _('Loading from configuration source %1$s failed. Reason: %2$s'), |
|
143 | - (string)$configString, |
|
144 | - (string)$exception->getMessage() |
|
145 | - )); |
|
146 | - } |
|
147 | - } |
|
148 | - |
|
149 | - return (array)$config; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Process the passed-in defaults and merge them with the new values, while |
|
154 | - * checking that all required options are set. |
|
155 | - * |
|
156 | - * @since 0.1.0 |
|
157 | - * |
|
158 | - * @param array $config Configuration settings to resolve. |
|
159 | - * @return array Resolved configuration settings. |
|
160 | - * @throws UnexpectedValueException If there are errors while resolving the |
|
161 | - * options. |
|
162 | - */ |
|
163 | - protected function resolveOptions($config) |
|
164 | - { |
|
165 | - if ( ! $this->schema) { |
|
166 | - return $config; |
|
167 | - } |
|
168 | - |
|
169 | - try { |
|
170 | - $resolver = new OptionsResolver(); |
|
171 | - if ($this->configureOptions($resolver)) { |
|
172 | - $config = $resolver->resolve($config); |
|
173 | - } |
|
174 | - } catch (Exception $exception) { |
|
175 | - throw new UnexpectedValueException(sprintf( |
|
176 | - _('Error while resolving config options: %1$s'), |
|
177 | - $exception->getMessage() |
|
178 | - )); |
|
179 | - } |
|
180 | - |
|
181 | - return $config; |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Configure the possible and required options for the Config. |
|
186 | - * |
|
187 | - * This should return a bool to let the resolve_options() know whether the |
|
188 | - * actual resolving needs to be done or not. |
|
189 | - * |
|
190 | - * @since 0.1.0 |
|
191 | - * |
|
192 | - * @param OptionsResolver $resolver Reference to the OptionsResolver |
|
193 | - * instance. |
|
194 | - * @return bool Whether to do the resolving. |
|
195 | - * @throws UnexpectedValueException If there are errors while processing. |
|
196 | - */ |
|
197 | - protected function configureOptions(OptionsResolver $resolver) |
|
198 | - { |
|
199 | - $defined = $this->schema->getDefinedOptions(); |
|
200 | - $defaults = $this->schema->getDefaultOptions(); |
|
201 | - $required = $this->schema->getRequiredOptions(); |
|
202 | - |
|
203 | - if ( ! $defined && ! $defaults && ! $required) { |
|
204 | - return false; |
|
205 | - } |
|
206 | - |
|
207 | - try { |
|
208 | - if ($defined) { |
|
209 | - $resolver->setDefined($defined); |
|
210 | - } |
|
211 | - if ($defaults) { |
|
212 | - $resolver->setDefaults($defaults); |
|
213 | - } |
|
214 | - if ($required) { |
|
215 | - $resolver->setRequired($required); |
|
216 | - } |
|
217 | - } catch (Exception $exception) { |
|
218 | - throw new UnexpectedValueException(sprintf( |
|
219 | - _('Error while processing config options: %1$s'), |
|
220 | - $exception->getMessage() |
|
221 | - )); |
|
222 | - } |
|
223 | - |
|
224 | - return true; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Validate the Config file. |
|
229 | - * |
|
230 | - * @since 0.1.0 |
|
231 | - * |
|
232 | - * @return boolean |
|
233 | - */ |
|
234 | - public function isValid() |
|
235 | - { |
|
236 | - if ($this->validator) { |
|
237 | - return $this->validator->isValid($this); |
|
238 | - } |
|
239 | - |
|
240 | - return true; |
|
241 | - } |
|
33 | + /** |
|
34 | + * The schema of the Config file. |
|
35 | + * |
|
36 | + * @var Schema |
|
37 | + */ |
|
38 | + protected $schema; |
|
39 | + |
|
40 | + /** |
|
41 | + * The Validator class that gets asked to do the validation of the config. |
|
42 | + * |
|
43 | + * @since 0.1.0 |
|
44 | + * |
|
45 | + * @var Validator |
|
46 | + */ |
|
47 | + protected $validator; |
|
48 | + |
|
49 | + /** |
|
50 | + * Instantiate the Config object. |
|
51 | + * |
|
52 | + * It accepts either an array with the configuration settings, or a |
|
53 | + * filename pointing to a PHP file it can include. |
|
54 | + * |
|
55 | + * @since 0.1.0 |
|
56 | + * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
|
57 | + * |
|
58 | + * @param array|string $config Array with settings or filename for the |
|
59 | + * settings file. |
|
60 | + * @param Schema|null $schema Optional. Config that contains default |
|
61 | + * values that can get overwritten. |
|
62 | + * @param Validator|null $validator Optional. Validator class that does the |
|
63 | + * actual validation. |
|
64 | + * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
|
65 | + * configuration keys. Defaults to "\", "/" & ".". |
|
66 | + * @throws InvalidArgumentException If the config source is not a string or |
|
67 | + * array. |
|
68 | + * @throws RuntimeException If loading of the config source failed. |
|
69 | + * @throws UnexpectedValueException If the config file is not valid. |
|
70 | + */ |
|
71 | + public function __construct( |
|
72 | + $config, |
|
73 | + Schema $schema = null, |
|
74 | + Validator $validator = null, |
|
75 | + $delimiter = null |
|
76 | + ) { |
|
77 | + $this->schema = $schema; |
|
78 | + $this->validator = $validator; |
|
79 | + |
|
80 | + // Make sure $config is either a string or array. |
|
81 | + if ( ! (is_string($config) || is_array($config))) { |
|
82 | + throw new InvalidArgumentException(sprintf( |
|
83 | + _('Invalid configuration source: %1$s'), |
|
84 | + print_r($config, true) |
|
85 | + )); |
|
86 | + } |
|
87 | + |
|
88 | + if (is_string($config)) { |
|
89 | + $config = $this->fetchArrayData($config); |
|
90 | + } |
|
91 | + |
|
92 | + $config = $this->resolveOptions($config); |
|
93 | + |
|
94 | + parent::__construct($config, $delimiter); |
|
95 | + |
|
96 | + // Finally, validate the resulting config. |
|
97 | + if ( ! $this->isValid()) { |
|
98 | + throw new UnexpectedValueException(sprintf( |
|
99 | + _('ConfigInterface file is not valid: %1$s'), |
|
100 | + print_r($config, true) |
|
101 | + )); |
|
102 | + } |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Fetch array data from a string pointing to a file. |
|
107 | + * |
|
108 | + * @since 0.1.0 |
|
109 | + * |
|
110 | + * @param string $config Filename for the settings file. |
|
111 | + * @return array Array with configuration settings. |
|
112 | + * @throws RuntimeException If the config source is a non-existing |
|
113 | + * file. |
|
114 | + * @throws RuntimeException If loading of the config source failed. |
|
115 | + */ |
|
116 | + protected function fetchArrayData($config) |
|
117 | + { |
|
118 | + if (is_string($config) && ! ('' === $config)) { |
|
119 | + |
|
120 | + // $config is a valid string, make sure it is an existing file. |
|
121 | + if ( ! file_exists($config)) { |
|
122 | + throw new RuntimeException(sprintf( |
|
123 | + _('Non-existing configuration source: %1$s'), |
|
124 | + (string)$config |
|
125 | + )); |
|
126 | + } |
|
127 | + |
|
128 | + // Try to load the file through PHP's include(). |
|
129 | + $configString = $config; |
|
130 | + try { |
|
131 | + // Make sure we don't accidentally create output. |
|
132 | + ob_get_contents(); |
|
133 | + $config = include($configString); |
|
134 | + ob_clean(); |
|
135 | + |
|
136 | + // The included should return an array. |
|
137 | + if ( ! is_array($config)) { |
|
138 | + throw new RuntimeException(_('File inclusion did not return an array.')); |
|
139 | + } |
|
140 | + } catch (Exception $exception) { |
|
141 | + throw new RuntimeException(sprintf( |
|
142 | + _('Loading from configuration source %1$s failed. Reason: %2$s'), |
|
143 | + (string)$configString, |
|
144 | + (string)$exception->getMessage() |
|
145 | + )); |
|
146 | + } |
|
147 | + } |
|
148 | + |
|
149 | + return (array)$config; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Process the passed-in defaults and merge them with the new values, while |
|
154 | + * checking that all required options are set. |
|
155 | + * |
|
156 | + * @since 0.1.0 |
|
157 | + * |
|
158 | + * @param array $config Configuration settings to resolve. |
|
159 | + * @return array Resolved configuration settings. |
|
160 | + * @throws UnexpectedValueException If there are errors while resolving the |
|
161 | + * options. |
|
162 | + */ |
|
163 | + protected function resolveOptions($config) |
|
164 | + { |
|
165 | + if ( ! $this->schema) { |
|
166 | + return $config; |
|
167 | + } |
|
168 | + |
|
169 | + try { |
|
170 | + $resolver = new OptionsResolver(); |
|
171 | + if ($this->configureOptions($resolver)) { |
|
172 | + $config = $resolver->resolve($config); |
|
173 | + } |
|
174 | + } catch (Exception $exception) { |
|
175 | + throw new UnexpectedValueException(sprintf( |
|
176 | + _('Error while resolving config options: %1$s'), |
|
177 | + $exception->getMessage() |
|
178 | + )); |
|
179 | + } |
|
180 | + |
|
181 | + return $config; |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Configure the possible and required options for the Config. |
|
186 | + * |
|
187 | + * This should return a bool to let the resolve_options() know whether the |
|
188 | + * actual resolving needs to be done or not. |
|
189 | + * |
|
190 | + * @since 0.1.0 |
|
191 | + * |
|
192 | + * @param OptionsResolver $resolver Reference to the OptionsResolver |
|
193 | + * instance. |
|
194 | + * @return bool Whether to do the resolving. |
|
195 | + * @throws UnexpectedValueException If there are errors while processing. |
|
196 | + */ |
|
197 | + protected function configureOptions(OptionsResolver $resolver) |
|
198 | + { |
|
199 | + $defined = $this->schema->getDefinedOptions(); |
|
200 | + $defaults = $this->schema->getDefaultOptions(); |
|
201 | + $required = $this->schema->getRequiredOptions(); |
|
202 | + |
|
203 | + if ( ! $defined && ! $defaults && ! $required) { |
|
204 | + return false; |
|
205 | + } |
|
206 | + |
|
207 | + try { |
|
208 | + if ($defined) { |
|
209 | + $resolver->setDefined($defined); |
|
210 | + } |
|
211 | + if ($defaults) { |
|
212 | + $resolver->setDefaults($defaults); |
|
213 | + } |
|
214 | + if ($required) { |
|
215 | + $resolver->setRequired($required); |
|
216 | + } |
|
217 | + } catch (Exception $exception) { |
|
218 | + throw new UnexpectedValueException(sprintf( |
|
219 | + _('Error while processing config options: %1$s'), |
|
220 | + $exception->getMessage() |
|
221 | + )); |
|
222 | + } |
|
223 | + |
|
224 | + return true; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Validate the Config file. |
|
229 | + * |
|
230 | + * @since 0.1.0 |
|
231 | + * |
|
232 | + * @return boolean |
|
233 | + */ |
|
234 | + public function isValid() |
|
235 | + { |
|
236 | + if ($this->validator) { |
|
237 | + return $this->validator->isValid($this); |
|
238 | + } |
|
239 | + |
|
240 | + return true; |
|
241 | + } |
|
242 | 242 | } |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Config Schema Interface. |
|
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 Schema Interface. |
|
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 | |
@@ -22,30 +22,30 @@ discard block |
||
22 | 22 | interface ConfigSchemaInterface |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * Get the set of defined options. |
|
27 | - * |
|
28 | - * @since 0.1.0 |
|
29 | - * |
|
30 | - * @return array|null |
|
31 | - */ |
|
32 | - public function getDefinedOptions(); |
|
25 | + /** |
|
26 | + * Get the set of defined options. |
|
27 | + * |
|
28 | + * @since 0.1.0 |
|
29 | + * |
|
30 | + * @return array|null |
|
31 | + */ |
|
32 | + public function getDefinedOptions(); |
|
33 | 33 | |
34 | - /** |
|
35 | - * Get the set of default options. |
|
36 | - * |
|
37 | - * @since 0.1.0 |
|
38 | - * |
|
39 | - * @return array|null |
|
40 | - */ |
|
41 | - public function getDefaultOptions(); |
|
34 | + /** |
|
35 | + * Get the set of default options. |
|
36 | + * |
|
37 | + * @since 0.1.0 |
|
38 | + * |
|
39 | + * @return array|null |
|
40 | + */ |
|
41 | + public function getDefaultOptions(); |
|
42 | 42 | |
43 | - /** |
|
44 | - * Get the set of required options. |
|
45 | - * |
|
46 | - * @since 0.1.0 |
|
47 | - * |
|
48 | - * @return array|null |
|
49 | - */ |
|
50 | - public function getRequiredOptions(); |
|
43 | + /** |
|
44 | + * Get the set of required options. |
|
45 | + * |
|
46 | + * @since 0.1.0 |
|
47 | + * |
|
48 | + * @return array|null |
|
49 | + */ |
|
50 | + public function getRequiredOptions(); |
|
51 | 51 | } |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Config Validator Interface |
|
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 Validator Interface |
|
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 | |
@@ -24,14 +24,14 @@ discard block |
||
24 | 24 | interface ConfigValidatorInterface |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * Check whether the passed-in Config is valid. |
|
29 | - * |
|
30 | - * @since 0.1.0 |
|
31 | - * |
|
32 | - * @param ConfigInterface $config |
|
33 | - * |
|
34 | - * @return bool |
|
35 | - */ |
|
36 | - public function isValid(ConfigInterface $config); |
|
27 | + /** |
|
28 | + * Check whether the passed-in Config is valid. |
|
29 | + * |
|
30 | + * @since 0.1.0 |
|
31 | + * |
|
32 | + * @param ConfigInterface $config |
|
33 | + * |
|
34 | + * @return bool |
|
35 | + */ |
|
36 | + public function isValid(ConfigInterface $config); |
|
37 | 37 | } |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Generic Config Schema Class. |
|
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 | + * Generic Config Schema Class. |
|
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 | |
@@ -25,131 +25,131 @@ discard block |
||
25 | 25 | class ConfigSchema extends AbstractConfigSchema |
26 | 26 | { |
27 | 27 | |
28 | - /** |
|
29 | - * The key that is used in the schema to define a required value. |
|
30 | - */ |
|
31 | - const REQUIRED_KEY = 'required'; |
|
32 | - |
|
33 | - /** |
|
34 | - * The key that is used in the schema to define a default value. |
|
35 | - */ |
|
36 | - const DEFAULT_VALUE = 'default'; |
|
37 | - |
|
38 | - /** |
|
39 | - * The list of values that are recognized as true in the schema. |
|
40 | - */ |
|
41 | - const TRUTHY_VALUES = [ |
|
42 | - true, |
|
43 | - 1, |
|
44 | - 'true', |
|
45 | - 'True', |
|
46 | - 'TRUE', |
|
47 | - 'y', |
|
48 | - 'Y', |
|
49 | - 'yes', |
|
50 | - 'Yes', |
|
51 | - 'YES', |
|
52 | - '√', |
|
53 | - ]; |
|
54 | - |
|
55 | - /** |
|
56 | - * Instantiate a ConfigSchema object. |
|
57 | - * |
|
58 | - * @since 0.1.0 |
|
59 | - * |
|
60 | - * @param ConfigInterface|array $schema The schema to parse. |
|
61 | - * @throws InvalidArgumentException |
|
62 | - */ |
|
63 | - public function __construct($schema) |
|
64 | - { |
|
65 | - if ($schema instanceof ConfigInterface) { |
|
66 | - $schema = $schema->getArrayCopy(); |
|
67 | - } |
|
68 | - |
|
69 | - if ( ! is_array($schema)) { |
|
70 | - throw new InvalidArgumentException(sprintf( |
|
71 | - _('Invalid schema source: %1$s'), |
|
72 | - print_r($schema, true) |
|
73 | - )); |
|
74 | - } |
|
75 | - |
|
76 | - array_walk($schema, [$this, 'parseSchema']); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Parse a single provided schema entry. |
|
81 | - * |
|
82 | - * @since 0.1.0 |
|
83 | - * |
|
84 | - * @param mixed $data The data associated with the key. |
|
85 | - * @param string $key The key of the schema data. |
|
86 | - */ |
|
87 | - protected function parseSchema($data, $key) |
|
88 | - { |
|
89 | - $this->parseDefined($key); |
|
90 | - |
|
91 | - if (array_key_exists(self::REQUIRED_KEY, $data)) { |
|
92 | - $this->parseRequired($key, |
|
93 | - $data[self::REQUIRED_KEY]); |
|
94 | - } |
|
95 | - |
|
96 | - if (array_key_exists(self::DEFAULT_VALUE, $data)) { |
|
97 | - $this->parseDefault($key, |
|
98 | - $data[self::DEFAULT_VALUE]); |
|
99 | - } |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Parse the set of defined values. |
|
104 | - * |
|
105 | - * @since 0.1.0 |
|
106 | - * |
|
107 | - * @param string $key The key of the schema data. |
|
108 | - */ |
|
109 | - protected function parseDefined($key) |
|
110 | - { |
|
111 | - $this->defined[] = $key; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Parse the set of required values. |
|
116 | - * |
|
117 | - * @since 0.1.0 |
|
118 | - * |
|
119 | - * @param string $key The key of the schema data. |
|
120 | - * @param mixed $data The data associated with the key. |
|
121 | - */ |
|
122 | - protected function parseRequired($key, $data) |
|
123 | - { |
|
124 | - if ($this->isTruthy($data)) { |
|
125 | - $this->required[] = $key; |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Parse the set of default values. |
|
131 | - * |
|
132 | - * @since 0.1.0 |
|
133 | - * |
|
134 | - * @param string $key The key of the schema data. |
|
135 | - * @param mixed $data The data associated with the key. |
|
136 | - */ |
|
137 | - protected function parseDefault($key, $data) |
|
138 | - { |
|
139 | - $this->defaults[$key] = $data; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Return a boolean true or false for an arbitrary set of data. Recognizes |
|
144 | - * several different string values that should be valued as true. |
|
145 | - * |
|
146 | - * @since 0.1.0 |
|
147 | - * |
|
148 | - * @param mixed $data The data to evaluate. |
|
149 | - * @return bool |
|
150 | - */ |
|
151 | - protected function isTruthy($data) |
|
152 | - { |
|
153 | - return in_array($data, self::TRUTHY_VALUES, true); |
|
154 | - } |
|
28 | + /** |
|
29 | + * The key that is used in the schema to define a required value. |
|
30 | + */ |
|
31 | + const REQUIRED_KEY = 'required'; |
|
32 | + |
|
33 | + /** |
|
34 | + * The key that is used in the schema to define a default value. |
|
35 | + */ |
|
36 | + const DEFAULT_VALUE = 'default'; |
|
37 | + |
|
38 | + /** |
|
39 | + * The list of values that are recognized as true in the schema. |
|
40 | + */ |
|
41 | + const TRUTHY_VALUES = [ |
|
42 | + true, |
|
43 | + 1, |
|
44 | + 'true', |
|
45 | + 'True', |
|
46 | + 'TRUE', |
|
47 | + 'y', |
|
48 | + 'Y', |
|
49 | + 'yes', |
|
50 | + 'Yes', |
|
51 | + 'YES', |
|
52 | + '√', |
|
53 | + ]; |
|
54 | + |
|
55 | + /** |
|
56 | + * Instantiate a ConfigSchema object. |
|
57 | + * |
|
58 | + * @since 0.1.0 |
|
59 | + * |
|
60 | + * @param ConfigInterface|array $schema The schema to parse. |
|
61 | + * @throws InvalidArgumentException |
|
62 | + */ |
|
63 | + public function __construct($schema) |
|
64 | + { |
|
65 | + if ($schema instanceof ConfigInterface) { |
|
66 | + $schema = $schema->getArrayCopy(); |
|
67 | + } |
|
68 | + |
|
69 | + if ( ! is_array($schema)) { |
|
70 | + throw new InvalidArgumentException(sprintf( |
|
71 | + _('Invalid schema source: %1$s'), |
|
72 | + print_r($schema, true) |
|
73 | + )); |
|
74 | + } |
|
75 | + |
|
76 | + array_walk($schema, [$this, 'parseSchema']); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Parse a single provided schema entry. |
|
81 | + * |
|
82 | + * @since 0.1.0 |
|
83 | + * |
|
84 | + * @param mixed $data The data associated with the key. |
|
85 | + * @param string $key The key of the schema data. |
|
86 | + */ |
|
87 | + protected function parseSchema($data, $key) |
|
88 | + { |
|
89 | + $this->parseDefined($key); |
|
90 | + |
|
91 | + if (array_key_exists(self::REQUIRED_KEY, $data)) { |
|
92 | + $this->parseRequired($key, |
|
93 | + $data[self::REQUIRED_KEY]); |
|
94 | + } |
|
95 | + |
|
96 | + if (array_key_exists(self::DEFAULT_VALUE, $data)) { |
|
97 | + $this->parseDefault($key, |
|
98 | + $data[self::DEFAULT_VALUE]); |
|
99 | + } |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Parse the set of defined values. |
|
104 | + * |
|
105 | + * @since 0.1.0 |
|
106 | + * |
|
107 | + * @param string $key The key of the schema data. |
|
108 | + */ |
|
109 | + protected function parseDefined($key) |
|
110 | + { |
|
111 | + $this->defined[] = $key; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Parse the set of required values. |
|
116 | + * |
|
117 | + * @since 0.1.0 |
|
118 | + * |
|
119 | + * @param string $key The key of the schema data. |
|
120 | + * @param mixed $data The data associated with the key. |
|
121 | + */ |
|
122 | + protected function parseRequired($key, $data) |
|
123 | + { |
|
124 | + if ($this->isTruthy($data)) { |
|
125 | + $this->required[] = $key; |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Parse the set of default values. |
|
131 | + * |
|
132 | + * @since 0.1.0 |
|
133 | + * |
|
134 | + * @param string $key The key of the schema data. |
|
135 | + * @param mixed $data The data associated with the key. |
|
136 | + */ |
|
137 | + protected function parseDefault($key, $data) |
|
138 | + { |
|
139 | + $this->defaults[$key] = $data; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Return a boolean true or false for an arbitrary set of data. Recognizes |
|
144 | + * several different string values that should be valued as true. |
|
145 | + * |
|
146 | + * @since 0.1.0 |
|
147 | + * |
|
148 | + * @param mixed $data The data to evaluate. |
|
149 | + * @return bool |
|
150 | + */ |
|
151 | + protected function isTruthy($data) |
|
152 | + { |
|
153 | + return in_array($data, self::TRUTHY_VALUES, true); |
|
154 | + } |
|
155 | 155 | } |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Config Interface |
|
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 Interface |
|
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 | |
@@ -27,70 +27,70 @@ discard block |
||
27 | 27 | interface ConfigInterface extends IteratorAggregate, ArrayAccess, Serializable, Countable |
28 | 28 | { |
29 | 29 | |
30 | - /** |
|
31 | - * Creates a copy of the ArrayObject. |
|
32 | - * |
|
33 | - * Returns a copy of the array. When the ArrayObject refers to an object an |
|
34 | - * array of the public properties of that object will be returned. |
|
35 | - * This is implemented by \ArrayObject. |
|
36 | - * |
|
37 | - * @since 0.1.0 |
|
38 | - * |
|
39 | - * @return array Copy of the array. |
|
40 | - */ |
|
41 | - public function getArrayCopy(); |
|
30 | + /** |
|
31 | + * Creates a copy of the ArrayObject. |
|
32 | + * |
|
33 | + * Returns a copy of the array. When the ArrayObject refers to an object an |
|
34 | + * array of the public properties of that object will be returned. |
|
35 | + * This is implemented by \ArrayObject. |
|
36 | + * |
|
37 | + * @since 0.1.0 |
|
38 | + * |
|
39 | + * @return array Copy of the array. |
|
40 | + */ |
|
41 | + public function getArrayCopy(); |
|
42 | 42 | |
43 | - /** |
|
44 | - * Check whether the Config has a specific key. |
|
45 | - * |
|
46 | - * To check a value several levels deep, add the keys for each level as a comma-separated list. |
|
47 | - * |
|
48 | - * @since 0.1.0 |
|
49 | - * @since 0.1.4 Accepts list of keys. |
|
50 | - * |
|
51 | - * @param string ... List of keys. |
|
52 | - * @return bool |
|
53 | - */ |
|
54 | - public function hasKey(); |
|
43 | + /** |
|
44 | + * Check whether the Config has a specific key. |
|
45 | + * |
|
46 | + * To check a value several levels deep, add the keys for each level as a comma-separated list. |
|
47 | + * |
|
48 | + * @since 0.1.0 |
|
49 | + * @since 0.1.4 Accepts list of keys. |
|
50 | + * |
|
51 | + * @param string ... List of keys. |
|
52 | + * @return bool |
|
53 | + */ |
|
54 | + public function hasKey(); |
|
55 | 55 | |
56 | - /** |
|
57 | - * Get the value of a specific key. |
|
58 | - * |
|
59 | - * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
60 | - * |
|
61 | - * @since 0.1.0 |
|
62 | - * @since 0.1.4 Accepts list of keys. |
|
63 | - * |
|
64 | - * @param string ... List of keys. |
|
65 | - * |
|
66 | - * @return mixed |
|
67 | - */ |
|
68 | - public function getKey(); |
|
56 | + /** |
|
57 | + * Get the value of a specific key. |
|
58 | + * |
|
59 | + * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
60 | + * |
|
61 | + * @since 0.1.0 |
|
62 | + * @since 0.1.4 Accepts list of keys. |
|
63 | + * |
|
64 | + * @param string ... List of keys. |
|
65 | + * |
|
66 | + * @return mixed |
|
67 | + */ |
|
68 | + public function getKey(); |
|
69 | 69 | |
70 | - /** |
|
71 | - * Get a (multi-dimensional) array of all the configuration settings. |
|
72 | - * |
|
73 | - * @since 0.1.4 |
|
74 | - * |
|
75 | - * @return array |
|
76 | - */ |
|
77 | - public function getAll(); |
|
70 | + /** |
|
71 | + * Get a (multi-dimensional) array of all the configuration settings. |
|
72 | + * |
|
73 | + * @since 0.1.4 |
|
74 | + * |
|
75 | + * @return array |
|
76 | + */ |
|
77 | + public function getAll(); |
|
78 | 78 | |
79 | - /** |
|
80 | - * Get the an array with all the keys |
|
81 | - * |
|
82 | - * @since 0.1.0 |
|
83 | - * |
|
84 | - * @return mixed |
|
85 | - */ |
|
86 | - public function getKeys(); |
|
79 | + /** |
|
80 | + * Get the an array with all the keys |
|
81 | + * |
|
82 | + * @since 0.1.0 |
|
83 | + * |
|
84 | + * @return mixed |
|
85 | + */ |
|
86 | + public function getKeys(); |
|
87 | 87 | |
88 | - /** |
|
89 | - * Is the Config valid? |
|
90 | - * |
|
91 | - * @since 0.1.0 |
|
92 | - * |
|
93 | - * @return boolean |
|
94 | - */ |
|
95 | - public function isValid(); |
|
88 | + /** |
|
89 | + * Is the Config valid? |
|
90 | + * |
|
91 | + * @since 0.1.0 |
|
92 | + * |
|
93 | + * @return boolean |
|
94 | + */ |
|
95 | + public function isValid(); |
|
96 | 96 | } |
@@ -1,13 +1,13 @@ discard block |
||
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 | |
@@ -16,84 +16,84 @@ discard block |
||
16 | 16 | trait ConfigTrait |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * Reference to the Config object. |
|
21 | - * |
|
22 | - * @since 0.1.2 |
|
23 | - * |
|
24 | - * @var ConfigInterface |
|
25 | - */ |
|
26 | - protected $config; |
|
19 | + /** |
|
20 | + * Reference to the Config object. |
|
21 | + * |
|
22 | + * @since 0.1.2 |
|
23 | + * |
|
24 | + * @var ConfigInterface |
|
25 | + */ |
|
26 | + protected $config; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Process the passed-in configuration file. |
|
30 | - * |
|
31 | - * @since 0.1.2 |
|
32 | - * |
|
33 | - * @param ConfigInterface $config The Config to process. |
|
34 | - */ |
|
35 | - protected function processConfig(ConfigInterface $config) |
|
36 | - { |
|
37 | - $this->config = $config; |
|
38 | - } |
|
28 | + /** |
|
29 | + * Process the passed-in configuration file. |
|
30 | + * |
|
31 | + * @since 0.1.2 |
|
32 | + * |
|
33 | + * @param ConfigInterface $config The Config to process. |
|
34 | + */ |
|
35 | + protected function processConfig(ConfigInterface $config) |
|
36 | + { |
|
37 | + $this->config = $config; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * Check whether the Config has a specific key. |
|
42 | - * |
|
43 | - * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
44 | - * |
|
45 | - * @since 0.1.2 |
|
46 | - * @since 0.1.5 Accepts list of keys. |
|
47 | - * |
|
48 | - * @param string ... List of keys. |
|
49 | - * @return bool Whether the key is known. |
|
50 | - */ |
|
51 | - protected function hasConfigKey() |
|
52 | - { |
|
53 | - $keys = func_get_args(); |
|
40 | + /** |
|
41 | + * Check whether the Config has a specific key. |
|
42 | + * |
|
43 | + * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
44 | + * |
|
45 | + * @since 0.1.2 |
|
46 | + * @since 0.1.5 Accepts list of keys. |
|
47 | + * |
|
48 | + * @param string ... List of keys. |
|
49 | + * @return bool Whether the key is known. |
|
50 | + */ |
|
51 | + protected function hasConfigKey() |
|
52 | + { |
|
53 | + $keys = func_get_args(); |
|
54 | 54 | |
55 | - return call_user_func_array([$this->config, 'hasKey'], $keys); |
|
56 | - } |
|
55 | + return call_user_func_array([$this->config, 'hasKey'], $keys); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Get the Config value for 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 ... List of keys. |
|
67 | - * @return mixed Value of the key. |
|
68 | - */ |
|
69 | - protected function getConfigKey() |
|
70 | - { |
|
71 | - $keys = func_get_args(); |
|
58 | + /** |
|
59 | + * Get the Config value for 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 ... List of keys. |
|
67 | + * @return mixed Value of the key. |
|
68 | + */ |
|
69 | + protected function getConfigKey() |
|
70 | + { |
|
71 | + $keys = func_get_args(); |
|
72 | 72 | |
73 | - return call_user_func_array([$this->config, 'getKey'], $keys); |
|
74 | - } |
|
73 | + return call_user_func_array([$this->config, 'getKey'], $keys); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Get a (multi-dimensional) array of all the configuration settings. |
|
78 | - * |
|
79 | - * @since 0.1.4 |
|
80 | - * |
|
81 | - * @return array All the configuration settings. |
|
82 | - */ |
|
83 | - protected function getConfigArray() |
|
84 | - { |
|
85 | - return $this->config->getAll(); |
|
86 | - } |
|
76 | + /** |
|
77 | + * Get a (multi-dimensional) array of all the configuration settings. |
|
78 | + * |
|
79 | + * @since 0.1.4 |
|
80 | + * |
|
81 | + * @return array All the configuration settings. |
|
82 | + */ |
|
83 | + protected function getConfigArray() |
|
84 | + { |
|
85 | + return $this->config->getAll(); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Get an array of all the keys that are known by the Config. |
|
90 | - * |
|
91 | - * @since 0.1.2 |
|
92 | - * |
|
93 | - * @return array Array of strings containing all the keys. |
|
94 | - */ |
|
95 | - protected function getConfigKeys() |
|
96 | - { |
|
97 | - return $this->config->getKeys(); |
|
98 | - } |
|
88 | + /** |
|
89 | + * Get an array of all the keys that are known by the Config. |
|
90 | + * |
|
91 | + * @since 0.1.2 |
|
92 | + * |
|
93 | + * @return array Array of strings containing all the keys. |
|
94 | + */ |
|
95 | + protected function getConfigKeys() |
|
96 | + { |
|
97 | + return $this->config->getKeys(); |
|
98 | + } |
|
99 | 99 | } |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | parent::__construct($config, ArrayObject::ARRAY_AS_PROPS); |
53 | 53 | |
54 | 54 | if (null !== $delimiter) { |
55 | - $this->delimiter = (array)$delimiter; |
|
55 | + $this->delimiter = (array) $delimiter; |
|
56 | 56 | } |
57 | 57 | } |
58 | 58 | |
@@ -136,7 +136,7 @@ discard block |
||
136 | 136 | */ |
137 | 137 | public function getKeys() |
138 | 138 | { |
139 | - return array_keys((array)$this); |
|
139 | + return array_keys((array) $this); |
|
140 | 140 | } |
141 | 141 | |
142 | 142 | /** |
@@ -180,7 +180,7 @@ discard block |
||
180 | 180 | // Replace all of the configured delimiters by the first one, so that we can then use explode(). |
181 | 181 | $normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString); |
182 | 182 | |
183 | - return (array)explode($this->delimiter[0], $normalizedString); |
|
183 | + return (array) explode($this->delimiter[0], $normalizedString); |
|
184 | 184 | } |
185 | 185 | |
186 | 186 | /** |
@@ -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 | } |