IcingaObject: dedicated boolean munging method

This commit is contained in:
Thomas Gelf 2016-02-16 11:46:01 +01:00
parent 22918f99b1
commit b22f12cf55
1 changed files with 17 additions and 12 deletions

View File

@ -184,18 +184,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
}
if ($this->propertyIsBoolean($key) && $value !== null) {
if ($value === 'y' || $value === '1' || $value === true || $value === 1) {
return parent::set($key, 'y');
} elseif ($value === 'n' || $value === '0' || $value === false || $value === 0) {
return parent::set($key, 'n');
} elseif ($value === '') {
return parent::set($key, null);
} else {
throw new ProgrammingError(
'Got invalid boolean: %s',
var_export($value, 1)
);
}
return parent::set($key, $this->normalizeBoolean($value));
}
if ($this->hasRelation($key)) {
@ -214,6 +203,22 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return parent::set($key, $value);
}
protected function normalizeBoolean($value)
{
if ($value === 'y' || $value === '1' || $value === true || $value === 1) {
return 'y';
} elseif ($value === 'n' || $value === '0' || $value === false || $value === 0) {
return 'n';
} elseif ($value === '' || $value === null) {
return null;
} else {
throw new ProgrammingError(
'Got invalid boolean: %s',
var_export($value, 1)
);
}
}
public function markForRemoval($remove = true)
{
$this->shouldBeRemoved = $remove;