lib: Add UrlParams::req()

UrlParams::req() should be used for requiring a mandatory URL parameter.

refs #8886
This commit is contained in:
Eric Lippmann 2015-04-07 12:17:59 +02:00
parent 0fc5801e5e
commit 93b1a35de0
1 changed files with 25 additions and 0 deletions

View File

@ -3,6 +3,8 @@
namespace Icinga\Web;
use Icinga\Exception\MissingParameterException;
class UrlParams
{
protected $separator = '&';
@ -42,6 +44,29 @@ class UrlParams
return rawurldecode($this->params[ end($this->index[$param]) ][ 1 ]);
}
/**
* Require a parameter
*
* @param string $name Name of the parameter
* @param bool $strict Whether the parameter's value must not be the empty string
*
* @return mixed
*
* @throws MissingParameterException If the parameter was not given
*/
public function req($name, $strict = true)
{
if ($this->has($name)) {
$value = $this->get($name);
if (! $strict || strlen($value) > 0) {
return $value;
}
}
$e = new MissingParameterException(t('Required parameter \'%s\' missing'), $name);
$e->setParameter($name);
throw $e;
}
/**
* Get all instances of the given parameter
*