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

git at icinga.org git at icinga.org
Thu Jul 28 16:58:55 CEST 2016


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

Author: Thomas Gelf <thomas at gelf.net>
Date:   Thu Jul 28 14:56:16 2016 +0000

JsonDecode: provide a new property modifier

fixes #12251

---

 .../PropertyModifierJsonDecode.php                 |   56 ++++++++++++++++++++
 run.php                                            |    1 +
 2 files changed, 57 insertions(+)

diff --git a/library/Director/PropertyModifier/PropertyModifierJsonDecode.php b/library/Director/PropertyModifier/PropertyModifierJsonDecode.php
new file mode 100644
index 0000000..c495b41
--- /dev/null
+++ b/library/Director/PropertyModifier/PropertyModifierJsonDecode.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace Icinga\Module\Director\PropertyModifier;
+
+use Icinga\Exception\InvalidPropertyException;
+use Icinga\Module\Director\Hook\PropertyModifierHook;
+use Icinga\Module\Director\Web\Form\QuickForm;
+
+class PropertyModifierJsonDecode extends PropertyModifierHook
+{
+    public static function addSettingsFormFields(QuickForm $form)
+    {
+        $form->addElement('select', 'on_failure', array(
+            'label'        => 'On failure',
+            'description'  => $form->translate(
+                'What should we do in case we are unable to decode the given string?'
+            ),
+            'multiOptions' => $form->optionalEnum(array(
+                'null' => $form->translate('Set no value (null)'),
+                'keep' => $form->translate('Keep the JSON string as is'),
+                'fail' => $form->translate('Let the whole import run fail'),
+            )),
+            'required'    => true,
+        ));
+    }
+
+    public function getName()
+    {
+        return 'Decode a JSON string';
+    }
+
+    public function transform($value)
+    {
+        if (null === $value) {
+            return $value;
+        }
+
+        $decoded = @json_decode($value);
+        if ($decoded === null && JSON_ERROR_NONE === json_last_error()) {
+            switch ($this->getSetting('on_failure')) {
+                case 'null':
+                    return null;
+                case 'keep':
+                    return $value;
+                case 'fail':
+                default:
+                    throw new InvalidPropertyException(
+                        'JSON decoding failed for %s',
+                        $value
+                    );
+            }
+        }
+
+        return $decoded;
+    }
+}
diff --git a/run.php b/run.php
index 6d64db7..240c1a1 100644
--- a/run.php
+++ b/run.php
@@ -33,6 +33,7 @@ $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\Pro
 $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierFromLatin1');
 $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierBitmask');
 $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierMakeBoolean');
+$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierJsonDecode');
 
 $this->provideHook('director/Job', $prefix . 'Job\\HousekeepingJob');
 $this->provideHook('director/Job', $prefix . 'Job\\ConfigJob');



More information about the icinga-checkins mailing list