@@ -13,7 +13,6 @@ |
||
13 | 13 | |
14 | 14 | use ArrayObject; |
15 | 15 | use OutOfRangeException; |
16 | -use BadMethodCallException; |
|
17 | 16 | use BrightNucleus\Config\ConfigInterface; |
18 | 17 | |
19 | 18 | /** |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | */ |
51 | 51 | public function hasKey($key) |
52 | 52 | { |
53 | - return array_key_exists($key, (array)$this); |
|
53 | + return array_key_exists($key, (array) $this); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | { |
67 | 67 | if ( ! $this->hasKey($key)) { |
68 | 68 | throw new OutOfRangeException(sprintf(_('The configuration key %1$s does not exist.'), |
69 | - (string)$key)); |
|
69 | + (string) $key)); |
|
70 | 70 | } |
71 | 71 | |
72 | 72 | return $this[$key]; |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | */ |
81 | 81 | public function getKeys() |
82 | 82 | { |
83 | - return array_keys((array)$this); |
|
83 | + return array_keys((array) $this); |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | /** |
@@ -27,67 +27,67 @@ |
||
27 | 27 | abstract class AbstractConfig extends ArrayObject implements ConfigInterface |
28 | 28 | { |
29 | 29 | |
30 | - /** |
|
31 | - * Instantiate the AbstractConfig object. |
|
32 | - * |
|
33 | - * @since 0.1.0 |
|
34 | - * |
|
35 | - * @param array $config Array with settings. |
|
36 | - */ |
|
37 | - public function __construct(array $config) |
|
38 | - { |
|
39 | - // Make sure the config entries can be accessed as properties. |
|
40 | - parent::__construct($config, ArrayObject::ARRAY_AS_PROPS); |
|
41 | - } |
|
30 | + /** |
|
31 | + * Instantiate the AbstractConfig object. |
|
32 | + * |
|
33 | + * @since 0.1.0 |
|
34 | + * |
|
35 | + * @param array $config Array with settings. |
|
36 | + */ |
|
37 | + public function __construct(array $config) |
|
38 | + { |
|
39 | + // Make sure the config entries can be accessed as properties. |
|
40 | + parent::__construct($config, ArrayObject::ARRAY_AS_PROPS); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Check whether the Config has a specific key. |
|
45 | - * |
|
46 | - * @since 0.1.0 |
|
47 | - * |
|
48 | - * @param string $key The key to check the existence for. |
|
49 | - * @return bool |
|
50 | - */ |
|
51 | - public function hasKey($key) |
|
52 | - { |
|
53 | - return array_key_exists($key, (array)$this); |
|
54 | - } |
|
43 | + /** |
|
44 | + * Check whether the Config has a specific key. |
|
45 | + * |
|
46 | + * @since 0.1.0 |
|
47 | + * |
|
48 | + * @param string $key The key to check the existence for. |
|
49 | + * @return bool |
|
50 | + */ |
|
51 | + public function hasKey($key) |
|
52 | + { |
|
53 | + return array_key_exists($key, (array)$this); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Get the value of a specific key. |
|
58 | - * |
|
59 | - * @since 0.1.0 |
|
60 | - * |
|
61 | - * @param string $key The key to get the value for. |
|
62 | - * @return mixed |
|
63 | - * @throws OutOfRangeException If an unknown key is requested. |
|
64 | - */ |
|
65 | - public function getKey($key) |
|
66 | - { |
|
67 | - if ( ! $this->hasKey($key)) { |
|
68 | - throw new OutOfRangeException(sprintf(_('The configuration key %1$s does not exist.'), |
|
69 | - (string)$key)); |
|
70 | - } |
|
56 | + /** |
|
57 | + * Get the value of a specific key. |
|
58 | + * |
|
59 | + * @since 0.1.0 |
|
60 | + * |
|
61 | + * @param string $key The key to get the value for. |
|
62 | + * @return mixed |
|
63 | + * @throws OutOfRangeException If an unknown key is requested. |
|
64 | + */ |
|
65 | + public function getKey($key) |
|
66 | + { |
|
67 | + if ( ! $this->hasKey($key)) { |
|
68 | + throw new OutOfRangeException(sprintf(_('The configuration key %1$s does not exist.'), |
|
69 | + (string)$key)); |
|
70 | + } |
|
71 | 71 | |
72 | - return $this[$key]; |
|
73 | - } |
|
72 | + return $this[$key]; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Get the an array with all the keys |
|
77 | - * |
|
78 | - * @since 0.1.0 |
|
79 | - * @return array |
|
80 | - */ |
|
81 | - public function getKeys() |
|
82 | - { |
|
83 | - return array_keys((array)$this); |
|
84 | - } |
|
75 | + /** |
|
76 | + * Get the an array with all the keys |
|
77 | + * |
|
78 | + * @since 0.1.0 |
|
79 | + * @return array |
|
80 | + */ |
|
81 | + public function getKeys() |
|
82 | + { |
|
83 | + return array_keys((array)$this); |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Validate the Config file. |
|
88 | - * |
|
89 | - * @since 0.1.0 |
|
90 | - * @return boolean |
|
91 | - */ |
|
92 | - abstract public function isValid(); |
|
86 | + /** |
|
87 | + * Validate the Config file. |
|
88 | + * |
|
89 | + * @since 0.1.0 |
|
90 | + * @return boolean |
|
91 | + */ |
|
92 | + abstract public function isValid(); |
|
93 | 93 | } |
@@ -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 | } |
@@ -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,210 +30,210 @@ 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 | - * |
|
57 | - * @param array|string $config Array with settings or filename for the |
|
58 | - * settings file. |
|
59 | - * @param Schema|null $schema Optional. Config that contains default |
|
60 | - * values that can get overwritten. |
|
61 | - * @param Validator|null $validator Optional. Validator class that does the |
|
62 | - * actual validation. |
|
63 | - * |
|
64 | - * @throws InvalidArgumentException If the config source is not a string or |
|
65 | - * array. |
|
66 | - * @throws RuntimeException If loading of the config source failed. |
|
67 | - * @throws UnexpectedValueException If the config file is not valid. |
|
68 | - */ |
|
69 | - public function __construct( |
|
70 | - $config, |
|
71 | - Schema $schema = null, |
|
72 | - Validator $validator = null |
|
73 | - ) { |
|
74 | - $this->schema = $schema; |
|
75 | - $this->validator = $validator; |
|
76 | - |
|
77 | - // Make sure $config is either a string or array. |
|
78 | - if ( ! (is_string($config) || is_array($config))) { |
|
79 | - throw new InvalidArgumentException(sprintf( |
|
80 | - _('Invalid configuration source: %1$s'), |
|
81 | - print_r($config, true) |
|
82 | - )); |
|
83 | - } |
|
84 | - |
|
85 | - if (is_string($config)) { |
|
86 | - $config = $this->fetchArrayData($config); |
|
87 | - } |
|
88 | - |
|
89 | - $config = $this->resolveOptions($config); |
|
90 | - |
|
91 | - parent::__construct($config); |
|
92 | - |
|
93 | - // Finally, validate the resulting config. |
|
94 | - if ( ! $this->isValid()) { |
|
95 | - throw new UnexpectedValueException(sprintf( |
|
96 | - _('ConfigInterface file is not valid: %1$s'), |
|
97 | - print_r($config, true) |
|
98 | - )); |
|
99 | - } |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Fetch array data from a string pointing to a file. |
|
104 | - * |
|
105 | - * @since 0.1.0 |
|
106 | - * |
|
107 | - * @param string $config Filename for the settings file. |
|
108 | - * @return array Array with configuration settings. |
|
109 | - * @throws RuntimeException If the config source is a non-existing |
|
110 | - * file. |
|
111 | - * @throws RuntimeException If loading of the config source failed. |
|
112 | - */ |
|
113 | - protected function fetchArrayData($config) |
|
114 | - { |
|
115 | - if (is_string($config) && ! ('' === $config)) { |
|
116 | - |
|
117 | - // $config is a valid string, make sure it is an existing file. |
|
118 | - if ( ! file_exists($config)) { |
|
119 | - throw new RuntimeException(sprintf( |
|
120 | - _('Non-existing configuration source: %1$s'), |
|
121 | - (string)$config |
|
122 | - )); |
|
123 | - } |
|
124 | - |
|
125 | - // Try to load the file through PHP's include(). |
|
126 | - $configString = $config; |
|
127 | - try { |
|
128 | - // Make sure we don't accidentally create output. |
|
129 | - ob_get_contents(); |
|
130 | - $config = include($configString); |
|
131 | - ob_clean(); |
|
132 | - |
|
133 | - // The included should return an array. |
|
134 | - if ( ! is_array($config)) { |
|
135 | - throw new RuntimeException(_('File inclusion did not return an array.')); |
|
136 | - } |
|
137 | - } catch (Exception $exception) { |
|
138 | - throw new RuntimeException(sprintf( |
|
139 | - _('Loading from configuration source %1$s failed. Reason: %2$s'), |
|
140 | - (string)$configString, |
|
141 | - (string)$exception->getMessage() |
|
142 | - )); |
|
143 | - } |
|
144 | - } |
|
145 | - |
|
146 | - return (array)$config; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Process the passed-in defaults and merge them with the new values, while |
|
151 | - * checking that all required options are set. |
|
152 | - * |
|
153 | - * @since 0.1.0 |
|
154 | - * |
|
155 | - * @param array $config Configuration settings to resolve. |
|
156 | - * @return array Resolved configuration settings. |
|
157 | - * @throws UnexpectedValueException If there are errors while resolving the |
|
158 | - * options. |
|
159 | - */ |
|
160 | - protected function resolveOptions($config) |
|
161 | - { |
|
162 | - if ( ! $this->schema) { |
|
163 | - return $config; |
|
164 | - } |
|
165 | - |
|
166 | - try { |
|
167 | - $resolver = new OptionsResolver(); |
|
168 | - if ($this->configureOptions($resolver)) { |
|
169 | - $config = $resolver->resolve($config); |
|
170 | - } |
|
171 | - } catch (Exception $exception) { |
|
172 | - throw new UnexpectedValueException(sprintf( |
|
173 | - _('Error while resolving config options: %1$s'), |
|
174 | - $exception->getMessage() |
|
175 | - )); |
|
176 | - } |
|
177 | - |
|
178 | - return $config; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Configure the possible and required options for the Config. |
|
183 | - * |
|
184 | - * This should return a bool to let the resolve_options() know whether the |
|
185 | - * actual resolving needs to be done or not. |
|
186 | - * |
|
187 | - * @since 0.1.0 |
|
188 | - * |
|
189 | - * @param OptionsResolver $resolver Reference to the OptionsResolver |
|
190 | - * instance. |
|
191 | - * @return bool Whether to do the resolving. |
|
192 | - * @throws UnexpectedValueException If there are errors while processing. |
|
193 | - */ |
|
194 | - protected function configureOptions(OptionsResolver $resolver) |
|
195 | - { |
|
196 | - $defined = $this->schema->getDefinedOptions(); |
|
197 | - $defaults = $this->schema->getDefaultOptions(); |
|
198 | - $required = $this->schema->getRequiredOptions(); |
|
199 | - |
|
200 | - if ( ! $defined && ! $defaults && ! $required) { |
|
201 | - return false; |
|
202 | - } |
|
203 | - |
|
204 | - try { |
|
205 | - if ($defined) { |
|
206 | - $resolver->setDefined($defined); |
|
207 | - } |
|
208 | - if ($defaults) { |
|
209 | - $resolver->setDefaults($defaults); |
|
210 | - } |
|
211 | - if ($required) { |
|
212 | - $resolver->setRequired($required); |
|
213 | - } |
|
214 | - } catch (Exception $exception) { |
|
215 | - throw new UnexpectedValueException(sprintf( |
|
216 | - _('Error while processing config options: %1$s'), |
|
217 | - $exception->getMessage() |
|
218 | - )); |
|
219 | - } |
|
220 | - |
|
221 | - return true; |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Validate the Config file. |
|
226 | - * |
|
227 | - * @since 0.1.0 |
|
228 | - * |
|
229 | - * @return boolean |
|
230 | - */ |
|
231 | - public function isValid() |
|
232 | - { |
|
233 | - if ($this->validator) { |
|
234 | - return $this->validator->isValid($this); |
|
235 | - } |
|
236 | - |
|
237 | - return true; |
|
238 | - } |
|
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 | + * |
|
57 | + * @param array|string $config Array with settings or filename for the |
|
58 | + * settings file. |
|
59 | + * @param Schema|null $schema Optional. Config that contains default |
|
60 | + * values that can get overwritten. |
|
61 | + * @param Validator|null $validator Optional. Validator class that does the |
|
62 | + * actual validation. |
|
63 | + * |
|
64 | + * @throws InvalidArgumentException If the config source is not a string or |
|
65 | + * array. |
|
66 | + * @throws RuntimeException If loading of the config source failed. |
|
67 | + * @throws UnexpectedValueException If the config file is not valid. |
|
68 | + */ |
|
69 | + public function __construct( |
|
70 | + $config, |
|
71 | + Schema $schema = null, |
|
72 | + Validator $validator = null |
|
73 | + ) { |
|
74 | + $this->schema = $schema; |
|
75 | + $this->validator = $validator; |
|
76 | + |
|
77 | + // Make sure $config is either a string or array. |
|
78 | + if ( ! (is_string($config) || is_array($config))) { |
|
79 | + throw new InvalidArgumentException(sprintf( |
|
80 | + _('Invalid configuration source: %1$s'), |
|
81 | + print_r($config, true) |
|
82 | + )); |
|
83 | + } |
|
84 | + |
|
85 | + if (is_string($config)) { |
|
86 | + $config = $this->fetchArrayData($config); |
|
87 | + } |
|
88 | + |
|
89 | + $config = $this->resolveOptions($config); |
|
90 | + |
|
91 | + parent::__construct($config); |
|
92 | + |
|
93 | + // Finally, validate the resulting config. |
|
94 | + if ( ! $this->isValid()) { |
|
95 | + throw new UnexpectedValueException(sprintf( |
|
96 | + _('ConfigInterface file is not valid: %1$s'), |
|
97 | + print_r($config, true) |
|
98 | + )); |
|
99 | + } |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Fetch array data from a string pointing to a file. |
|
104 | + * |
|
105 | + * @since 0.1.0 |
|
106 | + * |
|
107 | + * @param string $config Filename for the settings file. |
|
108 | + * @return array Array with configuration settings. |
|
109 | + * @throws RuntimeException If the config source is a non-existing |
|
110 | + * file. |
|
111 | + * @throws RuntimeException If loading of the config source failed. |
|
112 | + */ |
|
113 | + protected function fetchArrayData($config) |
|
114 | + { |
|
115 | + if (is_string($config) && ! ('' === $config)) { |
|
116 | + |
|
117 | + // $config is a valid string, make sure it is an existing file. |
|
118 | + if ( ! file_exists($config)) { |
|
119 | + throw new RuntimeException(sprintf( |
|
120 | + _('Non-existing configuration source: %1$s'), |
|
121 | + (string)$config |
|
122 | + )); |
|
123 | + } |
|
124 | + |
|
125 | + // Try to load the file through PHP's include(). |
|
126 | + $configString = $config; |
|
127 | + try { |
|
128 | + // Make sure we don't accidentally create output. |
|
129 | + ob_get_contents(); |
|
130 | + $config = include($configString); |
|
131 | + ob_clean(); |
|
132 | + |
|
133 | + // The included should return an array. |
|
134 | + if ( ! is_array($config)) { |
|
135 | + throw new RuntimeException(_('File inclusion did not return an array.')); |
|
136 | + } |
|
137 | + } catch (Exception $exception) { |
|
138 | + throw new RuntimeException(sprintf( |
|
139 | + _('Loading from configuration source %1$s failed. Reason: %2$s'), |
|
140 | + (string)$configString, |
|
141 | + (string)$exception->getMessage() |
|
142 | + )); |
|
143 | + } |
|
144 | + } |
|
145 | + |
|
146 | + return (array)$config; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Process the passed-in defaults and merge them with the new values, while |
|
151 | + * checking that all required options are set. |
|
152 | + * |
|
153 | + * @since 0.1.0 |
|
154 | + * |
|
155 | + * @param array $config Configuration settings to resolve. |
|
156 | + * @return array Resolved configuration settings. |
|
157 | + * @throws UnexpectedValueException If there are errors while resolving the |
|
158 | + * options. |
|
159 | + */ |
|
160 | + protected function resolveOptions($config) |
|
161 | + { |
|
162 | + if ( ! $this->schema) { |
|
163 | + return $config; |
|
164 | + } |
|
165 | + |
|
166 | + try { |
|
167 | + $resolver = new OptionsResolver(); |
|
168 | + if ($this->configureOptions($resolver)) { |
|
169 | + $config = $resolver->resolve($config); |
|
170 | + } |
|
171 | + } catch (Exception $exception) { |
|
172 | + throw new UnexpectedValueException(sprintf( |
|
173 | + _('Error while resolving config options: %1$s'), |
|
174 | + $exception->getMessage() |
|
175 | + )); |
|
176 | + } |
|
177 | + |
|
178 | + return $config; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Configure the possible and required options for the Config. |
|
183 | + * |
|
184 | + * This should return a bool to let the resolve_options() know whether the |
|
185 | + * actual resolving needs to be done or not. |
|
186 | + * |
|
187 | + * @since 0.1.0 |
|
188 | + * |
|
189 | + * @param OptionsResolver $resolver Reference to the OptionsResolver |
|
190 | + * instance. |
|
191 | + * @return bool Whether to do the resolving. |
|
192 | + * @throws UnexpectedValueException If there are errors while processing. |
|
193 | + */ |
|
194 | + protected function configureOptions(OptionsResolver $resolver) |
|
195 | + { |
|
196 | + $defined = $this->schema->getDefinedOptions(); |
|
197 | + $defaults = $this->schema->getDefaultOptions(); |
|
198 | + $required = $this->schema->getRequiredOptions(); |
|
199 | + |
|
200 | + if ( ! $defined && ! $defaults && ! $required) { |
|
201 | + return false; |
|
202 | + } |
|
203 | + |
|
204 | + try { |
|
205 | + if ($defined) { |
|
206 | + $resolver->setDefined($defined); |
|
207 | + } |
|
208 | + if ($defaults) { |
|
209 | + $resolver->setDefaults($defaults); |
|
210 | + } |
|
211 | + if ($required) { |
|
212 | + $resolver->setRequired($required); |
|
213 | + } |
|
214 | + } catch (Exception $exception) { |
|
215 | + throw new UnexpectedValueException(sprintf( |
|
216 | + _('Error while processing config options: %1$s'), |
|
217 | + $exception->getMessage() |
|
218 | + )); |
|
219 | + } |
|
220 | + |
|
221 | + return true; |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Validate the Config file. |
|
226 | + * |
|
227 | + * @since 0.1.0 |
|
228 | + * |
|
229 | + * @return boolean |
|
230 | + */ |
|
231 | + public function isValid() |
|
232 | + { |
|
233 | + if ($this->validator) { |
|
234 | + return $this->validator->isValid($this); |
|
235 | + } |
|
236 | + |
|
237 | + return true; |
|
238 | + } |
|
239 | 239 | } |
@@ -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 | - * 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,56 +27,56 @@ 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 | - * @since 0.1.0 |
|
47 | - * |
|
48 | - * @param string $key The key to check the existence for. |
|
49 | - * |
|
50 | - * @return bool |
|
51 | - */ |
|
52 | - public function hasKey($key); |
|
43 | + /** |
|
44 | + * Check whether the Config has a specific key. |
|
45 | + * |
|
46 | + * @since 0.1.0 |
|
47 | + * |
|
48 | + * @param string $key The key to check the existence for. |
|
49 | + * |
|
50 | + * @return bool |
|
51 | + */ |
|
52 | + public function hasKey($key); |
|
53 | 53 | |
54 | - /** |
|
55 | - * Get the value of a specific key. |
|
56 | - * |
|
57 | - * @since 0.1.0 |
|
58 | - * |
|
59 | - * @param string $key The key to get the value for. |
|
60 | - * |
|
61 | - * @return mixed |
|
62 | - */ |
|
63 | - public function getKey($key); |
|
54 | + /** |
|
55 | + * Get the value of a specific key. |
|
56 | + * |
|
57 | + * @since 0.1.0 |
|
58 | + * |
|
59 | + * @param string $key The key to get the value for. |
|
60 | + * |
|
61 | + * @return mixed |
|
62 | + */ |
|
63 | + public function getKey($key); |
|
64 | 64 | |
65 | - /** |
|
66 | - * Get the an array with all the keys |
|
67 | - * |
|
68 | - * @since 0.1.0 |
|
69 | - * |
|
70 | - * @return mixed |
|
71 | - */ |
|
72 | - public function getKeys(); |
|
65 | + /** |
|
66 | + * Get the an array with all the keys |
|
67 | + * |
|
68 | + * @since 0.1.0 |
|
69 | + * |
|
70 | + * @return mixed |
|
71 | + */ |
|
72 | + public function getKeys(); |
|
73 | 73 | |
74 | - /** |
|
75 | - * Is the Config valid? |
|
76 | - * |
|
77 | - * @since 0.1.0 |
|
78 | - * |
|
79 | - * @return boolean |
|
80 | - */ |
|
81 | - public function isValid(); |
|
74 | + /** |
|
75 | + * Is the Config valid? |
|
76 | + * |
|
77 | + * @since 0.1.0 |
|
78 | + * |
|
79 | + * @return boolean |
|
80 | + */ |
|
81 | + public function isValid(); |
|
82 | 82 | } |
@@ -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 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,62 +16,62 @@ 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 | - * @since 0.1.2 |
|
44 | - * |
|
45 | - * @param string $key The key to check. |
|
46 | - * @return bool Whether the key is known. |
|
47 | - */ |
|
48 | - protected function hasConfigKey($key) |
|
49 | - { |
|
50 | - return $this->config->hasKey($key); |
|
51 | - } |
|
40 | + /** |
|
41 | + * Check whether the Config has a specific key. |
|
42 | + * |
|
43 | + * @since 0.1.2 |
|
44 | + * |
|
45 | + * @param string $key The key to check. |
|
46 | + * @return bool Whether the key is known. |
|
47 | + */ |
|
48 | + protected function hasConfigKey($key) |
|
49 | + { |
|
50 | + return $this->config->hasKey($key); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Get the Config value for a specific key. |
|
55 | - * |
|
56 | - * @since 0.1.2 |
|
57 | - * |
|
58 | - * @param string $key The key for which to get the value. |
|
59 | - * @return mixed Value of the key. |
|
60 | - */ |
|
61 | - protected function getConfigKey($key) |
|
62 | - { |
|
63 | - return $this->config->getKey($key); |
|
64 | - } |
|
53 | + /** |
|
54 | + * Get the Config value for a specific key. |
|
55 | + * |
|
56 | + * @since 0.1.2 |
|
57 | + * |
|
58 | + * @param string $key The key for which to get the value. |
|
59 | + * @return mixed Value of the key. |
|
60 | + */ |
|
61 | + protected function getConfigKey($key) |
|
62 | + { |
|
63 | + return $this->config->getKey($key); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Get an array o all the keys that are known by the Config. |
|
68 | - * |
|
69 | - * @since 0.1.2 |
|
70 | - * |
|
71 | - * @return array Array of strings containing all the keys. |
|
72 | - */ |
|
73 | - protected function getConfigKeys() |
|
74 | - { |
|
75 | - return $this->config->getKeys(); |
|
76 | - } |
|
66 | + /** |
|
67 | + * Get an array o all the keys that are known by the Config. |
|
68 | + * |
|
69 | + * @since 0.1.2 |
|
70 | + * |
|
71 | + * @return array Array of strings containing all the keys. |
|
72 | + */ |
|
73 | + protected function getConfigKeys() |
|
74 | + { |
|
75 | + return $this->config->getKeys(); |
|
76 | + } |
|
77 | 77 | } |
@@ -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 | } |