[ParameterValidator] Move 'getQueriedContext' from BridgeAbstract

This commit is contained in:
logmanoriginal 2018-09-22 17:04:55 +02:00
parent f853ffc07c
commit 1d451610d6
2 changed files with 49 additions and 48 deletions

View file

@ -116,53 +116,6 @@ abstract class BridgeAbstract implements BridgeInterface {
}
}
/**
* Returns the name of the context matching the provided inputs
*
* @param array $inputs Associative array of inputs
* @return mixed Returns the context name or null if no match was found
*/
protected function getQueriedContext(array $inputs){
$queriedContexts = array();
// Detect matching context
foreach(static::PARAMETERS as $context => $set) {
$queriedContexts[$context] = null;
// Check if all parameters of the context are satisfied
foreach($set as $id => $properties) {
if(isset($inputs[$id]) && !empty($inputs[$id])) {
$queriedContexts[$context] = true;
} elseif(isset($properties['required'])
&& $properties['required'] === true) {
$queriedContexts[$context] = false;
break;
}
}
}
// Abort if one of the globally required parameters is not satisfied
if(array_key_exists('global', static::PARAMETERS)
&& $queriedContexts['global'] === false) {
return null;
}
unset($queriedContexts['global']);
switch(array_sum($queriedContexts)) {
case 0: // Found no match, is there a context without parameters?
foreach($queriedContexts as $context => $queried) {
if(is_null($queried)) {
return $context;
}
}
return null;
case 1: // Found unique match
return array_search(true, $queriedContexts);
default: return false;
}
}
/**
* Defined datas with parameters depending choose bridge
* Note : you can define a cache with "setCache"
@ -210,7 +163,7 @@ abstract class BridgeAbstract implements BridgeInterface {
}
// Guess the paramter context from input data
$this->queriedContext = $this->getQueriedContext($inputs);
$this->queriedContext = $validator->getQueriedContext($inputs, static::PARAMETERS);
if(is_null($this->queriedContext)) {
returnClientError('Required parameter(s) missing');
} elseif($this->queriedContext === false) {

View file

@ -120,4 +120,52 @@ class ParameterValidator {
return empty($this->invalid);
}
/**
* Returns the name of the context matching the provided inputs
*
* @param array $data Associative array of user data
* @param array $parameters Array of bridge parameters
* @return mixed Returns the context name or null if no match was found
*/
public function getQueriedContext($data, $parameters){
$queriedContexts = array();
// Detect matching context
foreach($parameters as $context => $set) {
$queriedContexts[$context] = null;
// Check if all parameters of the context are satisfied
foreach($set as $id => $properties) {
if(isset($data[$id]) && !empty($data[$id])) {
$queriedContexts[$context] = true;
} elseif(isset($properties['required'])
&& $properties['required'] === true) {
$queriedContexts[$context] = false;
break;
}
}
}
// Abort if one of the globally required parameters is not satisfied
if(array_key_exists('global', $parameters)
&& $queriedContexts['global'] === false) {
return null;
}
unset($queriedContexts['global']);
switch(array_sum($queriedContexts)) {
case 0: // Found no match, is there a context without parameters?
foreach($queriedContexts as $context => $queried) {
if(is_null($queried)) {
return $context;
}
}
return null;
case 1: // Found unique match
return array_search(true, $queriedContexts);
default: return false;
}
}
}