[icinga-checkins] icinga.org: icingaweb2-module-director/feature-ddo-playground: DdoObject: handle boolean and timestamp values

git at icinga.org git at icinga.org
Tue Jul 19 10:31:52 CEST 2016


Module: icingaweb2-module-director
Branch: feature-ddo-playground
Commit: 423feb97c8bbf54a4d10054f3d5bf32be3b5d728
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=423feb97c8bbf54a4d10054f3d5bf32be3b5d728

Author: Thomas Gelf <thomas at gelf.net>
Date:   Tue Jun 21 14:44:28 2016 +0200

DdoObject: handle boolean and timestamp values

---

 library/Director/Ddo/DdoObject.php |   48 ++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/library/Director/Ddo/DdoObject.php b/library/Director/Ddo/DdoObject.php
index 067159a..38197fa 100644
--- a/library/Director/Ddo/DdoObject.php
+++ b/library/Director/Ddo/DdoObject.php
@@ -3,7 +3,55 @@
 namespace Icinga\Module\Director\Ddo;
 
 use Icinga\Module\Director\Data\Db\DbObject;
+use Icinga\Exception\ProgrammingError;
 
 abstract class DdoObject extends DbObject
 {
+    protected $booleans = array();
+
+    protected $timestamps = array();
+
+    public function set($key, $value)
+    {
+        if ($this->propertyIsBoolean($key)) {
+            return parent::set($key, $this->normalizeBoolean($value));
+        }
+
+        if ($this->propertyIsTimestamp($key)) {
+            return parent::set($key, $this->normalizeTimestamp($value));
+        }
+
+        return parent::set($key, $value);
+    }
+
+    public function propertyIsBoolean($property)
+    {
+        return in_array($property, $this->booleans);
+    }
+
+    public function propertyIsTimestamp($property)
+    {
+        return in_array($property, $this->timestamps);
+    }
+
+    protected function normalizeTimestamp($value)
+    {
+        return (int) round($value * 1000000);
+    }
+
+    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)
+            );
+        }
+    }
 }



More information about the icinga-checkins mailing list