[icinga-checkins] icinga.org: icingaweb2-module-director/bugfix/loop-templateresolver-11803: PlainObjectRenderer: Render assoc arrays as Hashes

git at icinga.org git at icinga.org
Mon Oct 31 13:31:44 CET 2016


Module: icingaweb2-module-director
Branch: bugfix/loop-templateresolver-11803
Commit: a6ad95848ba2626ac508d6cd07394dc303b1bc9d
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=a6ad95848ba2626ac508d6cd07394dc303b1bc9d

Author: Markus Frosch <markus.frosch at icinga.com>
Date:   Thu Oct 27 15:54:13 2016 +0200

PlainObjectRenderer: Render assoc arrays as Hashes

Previous handling completely ignored any keys.

---

 library/Director/PlainObjectRenderer.php |   18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/library/Director/PlainObjectRenderer.php b/library/Director/PlainObjectRenderer.php
index 8267bd4..11d0eea 100644
--- a/library/Director/PlainObjectRenderer.php
+++ b/library/Director/PlainObjectRenderer.php
@@ -98,14 +98,28 @@ class PlainObjectRenderer
             return self::renderInteger($object);
         } elseif (is_float($object)) {
             return self::renderFloat($object);
+        } elseif (is_object($object) || static::isAssocArray($object)) {
+            return self::renderHash($object, $prefix);
         } elseif (is_array($object)) {
             return self::renderArray($object, $prefix);
-        } elseif (is_object($object)) {
-            return self::renderHash($object, $prefix);
         } elseif (is_string($object)) {
             return self::renderString($object);
         } else {
             return '(UNKNOWN TYPE) ' . var_export($object, 1);
         }
     }
+
+    /**
+     * Check if an array contains assoc keys
+     *
+     * @from https://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
+     * @param $arr
+     * @return bool
+     */
+    protected static function isAssocArray($arr)
+    {
+        if (! is_array($arr)) return false;
+        if (array() === $arr) return false;
+        return array_keys($arr) !== range(0, count($arr) - 1);
+    }
 }



More information about the icinga-checkins mailing list