Improve the detection of changes for multi-dimensional arrays.
authorGunnar Wrobel <p@rdus.de>
Fri, 17 Apr 2009 20:13:59 +0000 (22:13 +0200)
committerGunnar Wrobel <p@rdus.de>
Sat, 18 Apr 2009 04:53:45 +0000 (06:53 +0200)
Fixed detection of PEAR_Errors.

framework/Kolab_Server/lib/Horde/Kolab/Server/Object.php

index f9939a7..d3bce4e 100644 (file)
@@ -705,8 +705,7 @@ class Horde_Kolab_Server_Object
                         if (!is_array($old)) {
                             $old = array($old);
                         }
-                        $changes = array_merge(array_diff($old, $value),
-                                               array_diff($value, $old));
+                        $changes = $this->getArrayChanges($old, $value);
                         if (empty($changes)) {
                             // Unchanged value
                             unset($info[$key]);
@@ -753,6 +752,34 @@ class Horde_Kolab_Server_Object
     }
 
     /**
+     * Identify changes between two arrays.
+     *
+     * @param array $a1 The first array.
+     * @param array $a2 The second array.
+     *
+     * @return array The differences between both arrays.
+     */
+    protected function getArrayChanges($a1, $a2)
+    {
+        if (empty($a1) || empty($a2)) {
+            return !empty($a1) ? $a1 : $a2;
+        }
+        $ar = array();
+        foreach ($a2 as $k => $v) {
+            if (!is_array($v) || !is_array($a1[$k])) {
+                if ($v !== $a1[$k]) {
+                    $ar[$k] = $v;
+                }
+            } else {
+                if ($arr = $this->getArrayChanges($a1[$k], $a2[$k])) {
+                    $ar[$k] = $arr;
+                }
+            }
+        }
+        return $ar;
+    }
+
+    /**
      * Identify the UID(s) of the result entry(s).
      *
      * @param array $result   The LDAP search result.
@@ -851,7 +878,7 @@ class Horde_Kolab_Server_Object
         $filter = $server->searchQuery($criteria);
         $result = $server->search($filter, $params, $server->getBaseUid());
         $data   = $result->as_struct();
-        if (is_a($data, 'PEAR_Error')) {
+        if ($data instanceOf PEAR_Error) {
             throw new Horde_Kolab_Server_Exception($data->getMessage());
         }
         return self::uidFromResult($data, $restrict);