IcingaObject: improve renderPropertyAsSeconds()

This commit is contained in:
Thomas Gelf 2015-12-10 12:58:47 +01:00
parent f4ca547ac2
commit a0f0870566
1 changed files with 29 additions and 1 deletions

View File

@ -733,7 +733,35 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
protected function renderPropertyAsSeconds($key)
{
$value = (int) $this->$key;
// TODO: db field should be int
if (ctype_digit($this->$key)) {
$value = (int) $this->$key;
} else {
// TODO: do this at munge time
$parts = preg_split('/\s+/', $this->$key, -1, PREG_SPLIT_NO_EMPTY);
$value = 0;
foreach ($parts as $part) {
if (! preg_match('/^(\d+)([dhms]?)$/', $part, $m)) {
throw new ConfigurationError(
'"%s" is not a valid time (duration) definition',
$this->$key
);
}
switch ($m[2]) {
case 'd':
$value += $m[1] * 86400;
break;
case 'h':
$value += $m[1] * 3600;
break;
case 'm':
$value += $m[1] * 60;
break;
default:
$value += (int) $m[1];
}
}
}
if ($value > 0 && $value % 60 === 0) {
$value = (int) ($value / 60) . 'm';