Skip to content
Snippets Groups Projects
Commit 29aa425f authored by Olav Morken's avatar Olav Morken
Browse files

Configuration: added getValueValidate function.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@715 44740490-163a-0410-bde0-09ae8108e29a
parent b90d8970
No related branches found
No related tags found
No related merge requests found
...@@ -269,6 +269,52 @@ class SimpleSAML_Configuration { ...@@ -269,6 +269,52 @@ class SimpleSAML_Configuration {
return $ret; return $ret;
} }
/**
* Retrieve a configuration option with one of the given values.
*
* This will check that the configuration option matches one of the given values. The match will use
* strict comparison. An exception will be thrown if it does not match.
*
* The option can be mandatory or optional. If no default value is given, it will be considered to be
* mandatory, and an exception will be thrown if it isn't provided. If a default value is given, it
* is considered to be optional, and the default value is returned. The default value is automatically
* included in the list of allowed values.
*
* @param $name The name of the option.
* @param $allowedValues The values the option is allowed to take, as an array.
* @param $default The default value which will be returned if the option isn't found. If this parameter
* isn't given, the option will be considered to be mandatory. The default value can be
* any value, including NULL.
* @return The option with the given name, or $default if the option isn't found adn $default is given.
*/
public function getValueValidate($name, $allowedValues, $default = self::REQUIRED_OPTION) {
assert('is_string($name)');
assert('is_array($allowedValues)');
$ret = $this->getValue($name, $default);
if($ret === $default) {
/* The option wasn't found, or it matches the default value. In any case, return
* this value.
*/
return $ret;
}
if(!in_array($ret, $allowedValues, TRUE)) {
$strValues = array();
foreach($allowedValues as $av) {
$strValues[] = var_export($av, TRUE);
}
$strValues = implode(', ', $strValues);
throw new Exception('Invalid value given for the option \'' . $name . '\' in \'' .
$this->configfilename . '\'. It should have one of the following values: ' .
$strValues . '; but it had the following value: ' . var_export($ret, TRUE));
}
return $ret;
}
} }
?> ?>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment