@@ -90,17 +90,70 @@ public function getCacheTimeout()
90
90
return static ::CACHE_TIMEOUT ;
91
91
}
92
92
93
- /**
94
- * Sets the input values for a given context.
95
- *
96
- * @param array $inputs Associative array of inputs
97
- * @param string $queriedContext The context name
98
- * @return void
99
- */
100
- protected function setInputs (array $ inputs , $ queriedContext )
93
+ public function loadConfiguration ()
94
+ {
95
+ foreach (static ::CONFIGURATION as $ optionName => $ optionValue ) {
96
+ $ section = $ this ->getShortName ();
97
+ $ configurationOption = Configuration::getConfig ($ section , $ optionName );
98
+
99
+ if ($ configurationOption !== null ) {
100
+ $ this ->configuration [$ optionName ] = $ configurationOption ;
101
+ continue ;
102
+ }
103
+
104
+ if (isset ($ optionValue ['required ' ]) && $ optionValue ['required ' ] === true ) {
105
+ throw new \Exception (sprintf ('Missing configuration option: %s ' , $ optionName ));
106
+ } elseif (isset ($ optionValue ['defaultValue ' ])) {
107
+ $ this ->configuration [$ optionName ] = $ optionValue ['defaultValue ' ];
108
+ }
109
+ }
110
+ }
111
+
112
+ public function setInput (array $ input )
113
+ {
114
+ $ context = $ input ['context ' ] ?? null ;
115
+ if ($ context ) {
116
+ // Context hinting (optional)
117
+ $ this ->queriedContext = $ context ;
118
+ unset($ input ['context ' ]);
119
+ }
120
+
121
+ $ parameters = $ this ->getParameters ();
122
+
123
+ if (!$ parameters ) {
124
+ if ($ input ) {
125
+ throw new \Exception ('Invalid parameters value(s) ' );
126
+ }
127
+ return ;
128
+ }
129
+
130
+ $ validator = new ParameterValidator ();
131
+
132
+ // $input is passed by reference!
133
+ if (!$ validator ->validateInput ($ input , $ parameters )) {
134
+ $ invalidParameterKeys = array_column ($ validator ->getInvalidParameters (), 'name ' );
135
+ throw new \Exception (sprintf ('Invalid parameters value(s): %s ' , implode (', ' , $ invalidParameterKeys )));
136
+ }
137
+
138
+ // Guess the context from input data
139
+ if (empty ($ this ->queriedContext )) {
140
+ $ queriedContext = $ validator ->getQueriedContext ($ input , $ parameters );
141
+ $ this ->queriedContext = $ queriedContext ;
142
+ }
143
+
144
+ if (is_null ($ this ->queriedContext )) {
145
+ throw new \Exception ('Required parameter(s) missing ' );
146
+ } elseif ($ this ->queriedContext === false ) {
147
+ throw new \Exception ('Mixed context parameters ' );
148
+ }
149
+
150
+ $ this ->setInputWithContext ($ input , $ this ->queriedContext );
151
+ }
152
+
153
+ private function setInputWithContext (array $ input , $ queriedContext )
101
154
{
102
155
// Import and assign all inputs to their context
103
- foreach ($ inputs as $ name => $ value ) {
156
+ foreach ($ input as $ name => $ value ) {
104
157
foreach (static ::PARAMETERS as $ context => $ set ) {
105
158
if (array_key_exists ($ name , static ::PARAMETERS [$ context ])) {
106
159
$ this ->inputs [$ context ][$ name ]['value ' ] = $ value ;
@@ -128,7 +181,7 @@ protected function setInputs(array $inputs, $queriedContext)
128
181
129
182
switch ($ type ) {
130
183
case 'checkbox ' :
131
- $ this ->inputs [$ context ][$ name ]['value ' ] = $ inputs [$ context ][$ name ]['value ' ] ?? false ;
184
+ $ this ->inputs [$ context ][$ name ]['value ' ] = $ input [$ context ][$ name ]['value ' ] ?? false ;
132
185
break ;
133
186
case 'list ' :
134
187
if (!isset ($ properties ['defaultValue ' ])) {
@@ -153,8 +206,8 @@ protected function setInputs(array $inputs, $queriedContext)
153
206
// Copy global parameter values to the guessed context
154
207
if (array_key_exists ('global ' , static ::PARAMETERS )) {
155
208
foreach (static ::PARAMETERS ['global ' ] as $ name => $ properties ) {
156
- if (isset ($ inputs [$ name ])) {
157
- $ value = $ inputs [$ name ];
209
+ if (isset ($ input [$ name ])) {
210
+ $ value = $ input [$ name ];
158
211
} else {
159
212
if ($ properties ['type ' ] ?? null === 'checkbox ' ) {
160
213
$ value = false ;
@@ -176,91 +229,6 @@ protected function setInputs(array $inputs, $queriedContext)
176
229
}
177
230
}
178
231
179
- /**
180
- * Set inputs for the bridge
181
- *
182
- * Returns errors and aborts execution if the provided input parameters are
183
- * invalid.
184
- *
185
- * @param array List of input parameters. Each element in this list must
186
- * relate to an item in {@see BridgeAbstract::PARAMETERS}
187
- * @return void
188
- */
189
- public function setDatas (array $ inputs )
190
- {
191
- if (isset ($ inputs ['context ' ])) { // Context hinting (optional)
192
- $ this ->queriedContext = $ inputs ['context ' ];
193
- unset($ inputs ['context ' ]);
194
- }
195
-
196
- if (empty (static ::PARAMETERS )) {
197
- if (!empty ($ inputs )) {
198
- throw new \Exception ('Invalid parameters value(s) ' );
199
- }
200
-
201
- return ;
202
- }
203
-
204
- $ validator = new ParameterValidator ();
205
-
206
- if (!$ validator ->validateData ($ inputs , static ::PARAMETERS )) {
207
- $ parameters = array_map (
208
- function ($ i ) {
209
- return $ i ['name ' ];
210
- }, // Just display parameter names
211
- $ validator ->getInvalidParameters ()
212
- );
213
-
214
- throw new \Exception (sprintf ('Invalid parameters value(s): %s ' , implode (', ' , $ parameters )));
215
- }
216
-
217
- // Guess the context from input data
218
- if (empty ($ this ->queriedContext )) {
219
- $ this ->queriedContext = $ validator ->getQueriedContext ($ inputs , static ::PARAMETERS );
220
- }
221
-
222
- if (is_null ($ this ->queriedContext )) {
223
- throw new \Exception ('Required parameter(s) missing ' );
224
- } elseif ($ this ->queriedContext === false ) {
225
- throw new \Exception ('Mixed context parameters ' );
226
- }
227
-
228
- $ this ->setInputs ($ inputs , $ this ->queriedContext );
229
- }
230
-
231
- /**
232
- * Loads configuration for the bridge
233
- *
234
- * Returns errors and aborts execution if the provided configuration is
235
- * invalid.
236
- *
237
- * @return void
238
- */
239
- public function loadConfiguration ()
240
- {
241
- foreach (static ::CONFIGURATION as $ optionName => $ optionValue ) {
242
- $ section = $ this ->getShortName ();
243
- $ configurationOption = Configuration::getConfig ($ section , $ optionName );
244
-
245
- if ($ configurationOption !== null ) {
246
- $ this ->configuration [$ optionName ] = $ configurationOption ;
247
- continue ;
248
- }
249
-
250
- if (isset ($ optionValue ['required ' ]) && $ optionValue ['required ' ] === true ) {
251
- throw new \Exception (sprintf ('Missing configuration option: %s ' , $ optionName ));
252
- } elseif (isset ($ optionValue ['defaultValue ' ])) {
253
- $ this ->configuration [$ optionName ] = $ optionValue ['defaultValue ' ];
254
- }
255
- }
256
- }
257
-
258
- /**
259
- * Returns the value for the provided input
260
- *
261
- * @param string $input The input name
262
- * @return mixed|null The input value or null if the input is not defined
263
- */
264
232
protected function getInput ($ input )
265
233
{
266
234
return $ this ->inputs [$ this ->queriedContext ][$ input ]['value ' ] ?? null ;
0 commit comments