public static function getRequestVar($varName, $varDefault = null, $varType = null, $requestArrayToUse = null)
{
if (is_null($requestArrayToUse)) {
$requestArrayToUse = $_GET + $_POST;
}
$varDefault = self::sanitizeInputValues($varDefault);
if ($varType === 'int') {
// settype accepts only integer
// 'int' is simply a shortcut for 'integer'
$varType = 'integer';
}
// there is no value $varName in the REQUEST so we try to use the default value
if (empty($varName) || !isset($requestArrayToUse[$varName]) || !is_array($requestArrayToUse[$varName]) && strlen($requestArrayToUse[$varName]) === 0) {
if (is_null($varDefault)) {
throw new Exception("The parameter '{$varName}' isn't set in the Request, and a default value wasn't provided.");
} else {
if (!is_null($varType) && in_array($varType, array('string', 'integer', 'array'))) {
settype($varDefault, $varType);
}
return $varDefault;
}
}
// Normal case, there is a value available in REQUEST for the requested varName:
// we deal w/ json differently
if ($varType == 'json') {
$value = $requestArrayToUse[$varName];
$value = json_decode($value, $assoc = true);
return self::sanitizeInputValues($value, $alreadyStripslashed = true);
}
$value = self::sanitizeInputValues($requestArrayToUse[$varName]);
if (isset($varType)) {
$ok = false;
if ($varType === 'string') {
if (is_string($value) || is_int($value)) {
$ok = true;
} elseif (is_float($value)) {
$value = Common::forceDotAsSeparatorForDecimalPoint($value);
$ok = true;
}
} elseif ($varType === 'integer') {
if ($value == (string) (int) $value) {
$ok = true;
}
} elseif ($varType === 'float') {
$valueToCompare = (string) (double) $value;
$valueToCompare = Common::forceDotAsSeparatorForDecimalPoint($valueToCompare);
if ($value == $valueToCompare) {
$ok = true;
}
} elseif ($varType === 'array') {
if (is_array($value)) {
$ok = true;
}
} else {
throw new Exception("\$varType specified is not known. It should be one of the following: array, int, integer, float, string");
}
// The type is not correct
if ($ok === false) {
if ($varDefault === null) {
throw new Exception("The parameter '{$varName}' doesn't have a correct type, and a default value wasn't provided.");
} else {
settype($varDefault, $varType);
return $varDefault;
}
}
settype($value, $varType);
}
return $value;
}