From 2cff396fe410b023561fcdc9c34343650b46addb Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 20 May 2021 16:21:46 +0200 Subject: [PATCH] DbDataFormatter: new helper class Trying to reduce logic in IcingaObject --- library/Director/Data/Db/DbDataFormatter.php | 26 ++++++++++++++++++++ library/Director/Objects/IcingaObject.php | 24 +++--------------- 2 files changed, 29 insertions(+), 21 deletions(-) create mode 100644 library/Director/Data/Db/DbDataFormatter.php diff --git a/library/Director/Data/Db/DbDataFormatter.php b/library/Director/Data/Db/DbDataFormatter.php new file mode 100644 index 00000000..d6e4eeb5 --- /dev/null +++ b/library/Director/Data/Db/DbDataFormatter.php @@ -0,0 +1,26 @@ +propertyIsBoolean($key)) { - return parent::set($key, $this->normalizeBoolean($value)); + return parent::set($key, DbDataFormatter::normalizeBoolean($value)); } // e.g. zone_id @@ -793,27 +793,9 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer return $this->ranges()->getValues(); } - protected function normalizeBoolean($value) - { - if ($value === 'y' || $value === '1' || $value === true || $value === 1) { - return 'y'; - } - if ($value === 'n' || $value === '0' || $value === false || $value === 0) { - return 'n'; - } - if ($value === '' || $value === null) { - return null; - } - - throw new InvalidArgumentException(sprintf( - 'Got invalid boolean: %s', - var_export($value, 1) - )); - } - protected function setDisabled($disabled) { - return parent::reallySet('disabled', $this->normalizeBoolean($disabled)); + return $this->reallySet('disabled', DbDataFormatter::normalizeBoolean($disabled)); } public function isDisabled()