From 3bb0e31416994b0a36b784c8278b1c5f33999b1d Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Thu, 13 Mar 2014 10:25:21 +0100 Subject: finalized implementation of serialize/unserialize methods --- include/classbase.h | 4 ++-- include/serializable.h | 4 ++-- src/classbase.cpp | 23 +++++++++++++++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/include/classbase.h b/include/classbase.h index cc69a91..b4312dd 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -442,8 +442,8 @@ private: * @param data Structure describing the serialize/unserialize data * @return int */ - static int serialize(struct _zval_struct *object, unsigned char **buffer, zend_uint *buf_len, struct _zend_serialize_data *data); - static int unserialize(struct _zval_struct **object, struct _zend_class_entry *entry, const unsigned char *buffer, zend_uint buf_len, struct _zend_unserialize_data *data); + static int serialize(struct _zval_struct *object, unsigned char **buffer, unsigned int *buf_len, struct _zend_serialize_data *data); + static int unserialize(struct _zval_struct **object, struct _zend_class_entry *entry, const unsigned char *buffer, unsigned int buf_len, struct _zend_unserialize_data *data); /** * Name of the class diff --git a/include/serializable.h b/include/serializable.h index 67934a9..ce60d91 100644 --- a/include/serializable.h +++ b/include/serializable.h @@ -25,9 +25,9 @@ public: * This method should return a string representation of the object that * can be passed to the serialize() method and that will revive the object * - * @return Php::Value + * @return std::string */ - virtual Php::Value serialize() = 0; + virtual std::string serialize() = 0; /** * Unserialize the object diff --git a/src/classbase.cpp b/src/classbase.cpp index 0e39ae1..e922855 100644 --- a/src/classbase.cpp +++ b/src/classbase.cpp @@ -1013,8 +1013,19 @@ zend_object_iterator *ClassBase::getIterator(zend_class_entry *entry, zval *obje */ int ClassBase::serialize(zval *object, unsigned char **buffer, zend_uint *buf_len, zend_serialize_data *data) { - std::cout << "serialize is called" << std::endl; + // get the serializable object + Serializable *serializable = dynamic_cast(cpp_object(object)); + // call the serialize method on the object + auto value = serializable->serialize(); + + // allocate the buffer, and copy the data into it (the zend engine will + // (hopefully) clean up the data for us - the default serialize method does + // it like this too) + *buffer = (unsigned char*)estrndup(value.c_str(), value.size()); + *buf_len = value.size(); + + // done return SUCCESS; } @@ -1028,8 +1039,16 @@ int ClassBase::serialize(zval *object, unsigned char **buffer, zend_uint *buf_le */ int ClassBase::unserialize(zval **object, zend_class_entry *entry, const unsigned char *buffer, zend_uint buf_len, zend_unserialize_data *data) { - std::cout << "unserialize is called" << std::endl; + // create the PHP object + object_init_ex(*object, entry); + // turn this into a serializale + Serializable *serializable = dynamic_cast(cpp_object(*object)); + + // call the unserialize method on it + serializable->unserialize(buffer, buf_len); + + // done return SUCCESS; } -- cgit v1.2.3