Skip to content

Commit

Permalink
lib: Add UrlParams::req()
Browse files Browse the repository at this point in the history
UrlParams::req() should be used for requiring a mandatory URL parameter.

refs #8886
  • Loading branch information
lippserd committed Apr 7, 2015
1 parent 0fc5801 commit 93b1a35
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions library/Icinga/Web/UrlParams.php
Expand Up @@ -3,6 +3,8 @@

namespace Icinga\Web;

use Icinga\Exception\MissingParameterException;

class UrlParams
{
protected $separator = '&';
Expand Down Expand Up @@ -42,6 +44,29 @@ public function get($param, $default = null)
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
*
Expand Down

0 comments on commit 93b1a35

Please sign in to comment.