2016-09-26 01:09:29 +03:00
|
|
|
<?php
|
2017-02-14 19:28:07 +03:00
|
|
|
function validateData(&$data, $parameters){
|
2016-09-26 01:09:29 +03:00
|
|
|
$validateTextValue = function($value, $pattern = null){
|
2017-07-29 20:28:00 +03:00
|
|
|
if(!is_null($pattern)) {
|
2017-02-14 19:28:07 +03:00
|
|
|
$filteredValue = filter_var($value,
|
|
|
|
FILTER_VALIDATE_REGEXP,
|
|
|
|
array('options' => array(
|
2016-09-26 01:09:29 +03:00
|
|
|
'regexp' => '/^' . $pattern . '$/'
|
2017-02-14 19:28:07 +03:00
|
|
|
)
|
|
|
|
));
|
2016-09-26 01:09:29 +03:00
|
|
|
} else {
|
|
|
|
$filteredValue = filter_var($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($filteredValue === false)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return $filteredValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
$validateNumberValue = function($value){
|
|
|
|
$filteredValue = filter_var($value, FILTER_VALIDATE_INT);
|
|
|
|
|
2017-08-06 14:43:23 +03:00
|
|
|
if($filteredValue === false)
|
2016-09-26 01:09:29 +03:00
|
|
|
return null;
|
|
|
|
|
|
|
|
return $filteredValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
$validateCheckboxValue = function($value){
|
2017-08-06 14:45:24 +03:00
|
|
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
2016-09-26 01:09:29 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
$validateListValue = function($value, $expectedValues){
|
|
|
|
$filteredValue = filter_var($value);
|
|
|
|
|
|
|
|
if($filteredValue === false)
|
|
|
|
return null;
|
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
if(!in_array($filteredValue, $expectedValues)) { // Check sub-values?
|
|
|
|
foreach($expectedValues as $subName => $subValue) {
|
2016-09-26 01:09:29 +03:00
|
|
|
if(is_array($subValue) && in_array($filteredValue, $subValue))
|
|
|
|
return $filteredValue;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $filteredValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!is_array($data))
|
|
|
|
return false;
|
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
foreach($data as $name => $value) {
|
2016-09-26 01:09:29 +03:00
|
|
|
$registered = false;
|
2017-07-29 20:28:00 +03:00
|
|
|
foreach($parameters as $context => $set) {
|
|
|
|
if(array_key_exists($name, $set)) {
|
2016-09-26 01:09:29 +03:00
|
|
|
$registered = true;
|
2017-07-29 20:28:00 +03:00
|
|
|
if(!isset($set[$name]['type'])) {
|
2016-09-26 01:09:29 +03:00
|
|
|
$set[$name]['type'] = 'text';
|
|
|
|
}
|
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
switch($set[$name]['type']) {
|
2016-09-26 01:09:29 +03:00
|
|
|
case 'number':
|
|
|
|
$data[$name] = $validateNumberValue($value);
|
|
|
|
break;
|
|
|
|
case 'checkbox':
|
|
|
|
$data[$name] = $validateCheckboxValue($value);
|
|
|
|
break;
|
|
|
|
case 'list':
|
|
|
|
$data[$name] = $validateListValue($value, $set[$name]['values']);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case 'text':
|
2017-07-29 20:28:00 +03:00
|
|
|
if(isset($set[$name]['pattern'])) {
|
2016-09-26 01:09:29 +03:00
|
|
|
$data[$name] = $validateTextValue($value, $set[$name]['pattern']);
|
|
|
|
} else {
|
|
|
|
$data[$name] = $validateTextValue($value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-08-17 19:52:17 +03:00
|
|
|
if(is_null($data[$name]) && isset($set[$name]['required']) && $set[$name]['required']) {
|
2016-09-26 01:09:29 +03:00
|
|
|
echo 'Parameter \'' . $name . '\' is invalid!' . PHP_EOL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$registered)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|