This commit is contained in:
logmanoriginal 2016-09-01 21:09:18 +02:00
commit c642fca0d0

View file

@ -143,68 +143,104 @@ abstract class BridgeAbstract implements BridgeInterface {
return $this->items; return $this->items;
} }
protected function validateData(&$data){ protected function isValidTextValue($value, $pattern = null){
$validated=true; if(!is_null($pattern)){
foreach($data as $name=>$value){ $filteredValue = filter_var($value, FILTER_VALIDATE_REGEXP,
$registered=false; array('options' => array(
foreach($this->parameters as $context=>$set){ 'regexp' => '/^' . $pattern . '$/'
if(array_key_exists($name,$set)){ ))
$registered=true; );
if(!isset($set[$name]['type'])){ } else {
$set[$name]['type']='text'; $filteredValue = filter_var($value);
} }
if($filteredValue === false)
return null;
return $filteredValue;
}
protected function isValidNumberValue($value){
$filteredValue = filter_var($value, FILTER_VALIDATE_INT);
if($filteredValue === false && !empty($value))
return null;
return $filteredValue;
}
protected function isValidCheckboxValue($value){
$filteredValue = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
if(is_null($filteredValue))
return null;
return $filteredValue;
}
protected function isValidListValue($value, $expectedValues){
$filteredValue = filter_var($value);
if($filteredValue === false)
return null;
if(!in_array($filteredValue, $expectedValues)){ // Check sub-values?
foreach($expectedValues as $subName => $subValue){
if(is_array($subValue) && in_array($filteredValue, $subValue))
return $filteredValue;
}
return null;
}
return $filteredValue;
}
protected function validateData(&$data){
if(!is_array($data))
return false;
foreach($data as $name => $value){
$registered = false;
foreach($this->parameters as $context => $set){
if(array_key_exists($name, $set)){
$registered = true;
if(!isset($set[$name]['type'])){ // Default to 'text'
$set[$name]['type'] = 'text';
}
switch($set[$name]['type']){ switch($set[$name]['type']){
case 'number': case 'number':
$data[$name]=filter_var($value,FILTER_VALIDATE_INT); $data[$name] = $this->isValidNumberValue($value);
if($data[$name]===false && !empty($value)){
$validated=false;
}
break; break;
case 'checkbox': case 'checkbox':
$data[$name]=filter_var($value,FILTER_VALIDATE_BOOLEAN, $data[$name] = $this->isValidCheckboxValue($value);
FILTER_NULL_ON_FAILURE);
if(is_null($data[$name])){
$validated=false;
}
break; break;
case 'list': case 'list':
$data[$name]=filter_var($value); $data[$name] = $this->isValidListValue($value, $set[$name]['values']);
if(!in_array($value,$set[$name]['values'])){
foreach($set[$name]['values'] as $subName=>$subValue){
if(is_array($subValue) &&
in_array($value,$subValue)){
$data[$name]=filter_var($value);
break 2;
}
}
$validated=false;
$data[$name]=null;
}
break; break;
default: default:
case 'text': case 'text':
if(isset($set[$name]['pattern'])){ if(isset($set[$name]['pattern'])){
$data[$name]=filter_var($value,FILTER_VALIDATE_REGEXP, $data[$name] = $this->isValidTextValue($value, $set[$name]['pattern']);
array('options'=>array( } else {
'regexp'=>'/^'.$set[$name]['pattern'].'$/' $data[$name] = $this->isValidTextValue($value);
))
);
}else{
$data[$name]=filter_var($value);
}
if($data[$name]===false && !empty($value)){
$validated=false;
} }
break; break;
} }
if(is_null($data[$name])){
echo 'Parameter \'' . $name . '\' is invalid!' . PHP_EOL;
return false;
} }
} }
if(!$registered){
$validated=false;
}
} }
return $validated; if(!$registered)
return false;
}
return true;
} }
protected function getQueriedContext(){ protected function getQueriedContext(){
@ -251,14 +287,11 @@ abstract class BridgeAbstract implements BridgeInterface {
if(!is_null($this->cache)){ if(!is_null($this->cache)){
$this->cache->prepare($inputs); $this->cache->prepare($inputs);
$time = $this->cache->getTime(); $time = $this->cache->getTime();
} else {
$time = false;
}
if($time !== false && (time() - $this->getCacheDuration() < $time)){ if($time !== false && (time() - $this->getCacheDuration() < $time)){
$this->items = $this->cache->loadData(); $this->items = $this->cache->loadData();
return; return;
} }
}
if(empty($this->parameters)){ if(empty($this->parameters)){
if(!empty($inputs)){ if(!empty($inputs)){