Have Horde_Imap_Client_Search_Query implement Serializable
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 10 Sep 2010 20:50:05 +0000 (14:50 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 10 Sep 2010 22:51:43 +0000 (16:51 -0600)
framework/Imap_Client/lib/Horde/Imap/Client/Search/Query.php
framework/Mime/lib/Horde/Mime/Headers.php

index 31ac8dc..699e9c8 100644 (file)
  * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
  * @package  Imap_Client
  */
-class Horde_Imap_Client_Search_Query
+class Horde_Imap_Client_Search_Query implements Serializable
 {
+    /* Serialized version. */
+    const VERSION = 1;
+
     /* Constants for dateSearch() */
     const DATE_BEFORE = 'BEFORE';
     const DATE_ON = 'ON';
@@ -581,4 +584,48 @@ class Horde_Imap_Client_Search_Query
         $this->_search['prevsearch'] = $not;
     }
 
+    /* Serializable methods. */
+
+    /**
+     * Serialization.
+     *
+     * @return string  Serialized data.
+     */
+    public function serialize()
+    {
+        $data = array(
+            // Serialized data ID.
+            self::VERSION,
+            $this->_search
+        );
+
+        if (!is_null($this->_charset)) {
+            $data[] = $this->_charset;
+        }
+
+        return serialize($data);
+    }
+
+    /**
+     * Unserialization.
+     *
+     * @param string $data  Serialized data.
+     *
+     * @throws Exception
+     */
+    public function unserialize($data)
+    {
+        $data = @unserialize($data);
+        if (!is_array($data) ||
+            !isset($data[0]) ||
+            ($data[0] != self::VERSION)) {
+            throw new Exception('Cache version change');
+        }
+
+        $this->_search = $data[1];
+        if (isset($data[2])) {
+            $this->_charset = $data[2];
+        }
+    }
+
 }
index c202df9..d538395 100644 (file)
@@ -636,13 +636,18 @@ class Horde_Mime_Headers implements Serializable
      */
     public function serialize()
     {
-        return serialize(array(
+        $data = array(
             // Serialized data ID.
             self::VERSION,
             $this->_headers,
-            $this->_eol,
-            $this->_agent
-        ));
+            $this->_eol
+        );
+
+        if (!is_null($this->_agent)) {
+            $data[] = $this->_agent;
+        }
+
+        return serialize($data);
     }
 
     /**
@@ -663,7 +668,9 @@ class Horde_Mime_Headers implements Serializable
 
         $this->_headers = $data[1];
         $this->_eol = $data[2];
-        $this->_agent = $data[3];
+        if (isset($data[3])) {
+            $this->_agent = $data[3];
+        }
     }
 
 }