Implement Serializable.
authorJan Schneider <jan@horde.org>
Tue, 21 Dec 2010 23:33:48 +0000 (00:33 +0100)
committerJan Schneider <jan@horde.org>
Tue, 21 Dec 2010 23:33:48 +0000 (00:33 +0100)
framework/Share/lib/Horde/Share/Object/Datatree.php

index 69671b5..b6bb852 100644 (file)
 class Horde_Share_Object_Datatree extends Horde_Share_Object
 {
     /**
+     * Serializable version.
+     */
+    const VERSION = 1;
+
+    /**
      * The actual storage object that holds the data.
      *
      * @var mixed
@@ -29,6 +34,41 @@ class Horde_Share_Object_Datatree extends Horde_Share_Object
     }
 
     /**
+     * Serialize this object.
+     *
+     * @return string  The serialized data.
+     */
+    public function serialize()
+    {
+        return serialize(array(
+            self::VERSION,
+            $this->datatreeObject,
+            $this->_shareCallback,
+        ));
+    }
+
+    /**
+     * Reconstruct the object from serialized data.
+     *
+     * @param string $data  The serialized data.
+     */
+    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->datatreeObject = $data[1];
+        if (empty($data[2])) {
+            throw new Exception('Missing callback for Horde_Share_Object unserializing');
+        }
+        $this->_shareCallback = $data[2];
+    }
+
+    /**
      * Sets an attribute value in this object.
      *
      * @param string $attribute  The attribute to set.