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