[icinga-checkins] icinga.org: icingaweb2-module-director/master: PropertyModifierMakeBoolean : new property modifier

git at icinga.org git at icinga.org
Wed Jul 20 12:29:07 CEST 2016


Module: icingaweb2-module-director
Branch: master
Commit: 2ff722e4d22eba2e952dbf18b3b1c7407e38e81e
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=2ff722e4d22eba2e952dbf18b3b1c7407e38e81e

Author: Thomas Gelf <thomas at gelf.net>
Date:   Wed Jul 20 12:28:40 2016 +0200

PropertyModifierMakeBoolean: new property modifier

fixes #12184

---

 .../PropertyModifierMakeBoolean.php                |   90 ++++++++++++++++++++
 run.php                                            |    1 +
 2 files changed, 91 insertions(+)

diff --git a/library/Director/PropertyModifier/PropertyModifierMakeBoolean.php b/library/Director/PropertyModifier/PropertyModifierMakeBoolean.php
new file mode 100644
index 0000000..2a6a197
--- /dev/null
+++ b/library/Director/PropertyModifier/PropertyModifierMakeBoolean.php
@@ -0,0 +1,90 @@
+<?php
+
+namespace Icinga\Module\Director\PropertyModifier;
+
+use Icinga\Exception\InvalidPropertyException;
+use Icinga\Module\Director\Hook\PropertyModifierHook;
+use Icinga\Module\Director\Web\Form\QuickForm;
+
+class PropertyModifierMakeBoolean extends PropertyModifierHook
+{
+    protected static $validStrings = array(
+        '0'     => false,
+        'false' => false,
+        'n'     => false,
+        'no'    => false,
+        '1'     => true,
+        'true'  => true,
+        'y'     => true,
+        'yes'   => true,
+    );
+
+    public function getName()
+    {
+        return 'Convert to a boolean value';
+    }
+
+    public static function addSettingsFormFields(QuickForm $form)
+    {
+        $form->addElement('select', 'on_invalid', array(
+            'label'       => 'Invalid properties',
+            'required'    => true,
+            'description' => $form->translate(
+                'This modifier transforms 0/"0"/false/"false"/"n"/"no" to false'
+                . ' and 1, "1", true, "true", "y" and "yes" to true, both in a'
+                . ' case insensitive way. What should happen if the given value'
+                . ' does not match any of those?'
+                . ' You could return a null value, or default to false or true.'
+                . ' You might also consider interrupting the whole import process'
+                . ' as of invalid source data'
+            ),
+            'multiOptions' => $form->optionalEnum(array(
+                'null'  => $form->translate('Set null'),
+                'true'  => $form->translate('Set true'),
+                'false' => $form->translate('Set false'),
+                'fail'  => $form->translate('Let the import fail'),
+            )),
+        ));
+    }
+
+    public function transform($value)
+    {
+        if ($value === false || $value === true || $value === null) {
+            return $value;
+        }
+
+        if ($value === 0) {
+            return false;
+        }
+
+        if ($value === 1) {
+            return true;
+        }
+
+        if (is_string($value)) {
+            $value = strtolower($value);
+
+            if (array_key_exists($value, self::$validStrings)) {
+                return self::$validStrings[$value];
+            }
+        }
+
+        switch ($this->getSetting('on_missing')) {
+            case 'null':
+                return null;
+
+            case 'false':
+                return false;
+
+            case 'true':
+                return true;
+
+            case 'fail':
+            default:
+                throw new InvalidPropertyException(
+                    '"%s" cannot be converted to a boolean value',
+                    $value
+                );
+        }
+    }
+}
diff --git a/run.php b/run.php
index d540855..6d64db7 100644
--- a/run.php
+++ b/run.php
@@ -32,6 +32,7 @@ $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\Pro
 $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierFromAdSid');
 $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierFromLatin1');
 $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierBitmask');
+$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierMakeBoolean');
 
 $this->provideHook('director/Job', $prefix . 'Job\\HousekeepingJob');
 $this->provideHook('director/Job', $prefix . 'Job\\ConfigJob');



More information about the icinga-checkins mailing list