3434#include <ext/standard/info.h>
3535#include <Zend/zend_interfaces.h>
3636#include <ext/spl/spl_iterators.h>
37+ #include <ext/standard/php_var.h>
38+ #if PHP_VERSION_ID >= 70000
39+ # include <zend_smart_str.h>
40+ #else
41+ # include <ext/standard/php_smart_str.h>
42+ #endif
3743/* Our Compatability header */
3844#include "phongo_compat.h"
3945
@@ -199,6 +205,95 @@ PHP_METHOD(Binary, getType)
199205}
200206/* }}} */
201207
208+ /* {{{ proto string Binary::serialize()
209+ */
210+ PHP_METHOD (Binary , serialize )
211+ {
212+ php_phongo_binary_t * intern ;
213+ #if PHP_VERSION_ID >= 70000
214+ zval retval ;
215+ #else
216+ zval * retval ;
217+ #endif
218+ php_serialize_data_t var_hash ;
219+ smart_str buf = { 0 };
220+
221+ intern = Z_BINARY_OBJ_P (getThis ());
222+
223+ if (zend_parse_parameters_none () == FAILURE ) {
224+ return ;
225+ }
226+
227+ #if PHP_VERSION_ID >= 70000
228+ array_init_size (& retval , 2 );
229+ ADD_ASSOC_STRINGL (& retval , "data" , intern -> data , intern -> data_len );
230+ ADD_ASSOC_LONG_EX (& retval , "type" , intern -> type );
231+ #else
232+ ALLOC_INIT_ZVAL (retval );
233+ array_init_size (retval , 2 );
234+ ADD_ASSOC_STRINGL (retval , "data" , intern -> data , intern -> data_len );
235+ ADD_ASSOC_LONG_EX (retval , "type" , intern -> type );
236+ #endif
237+
238+ PHP_VAR_SERIALIZE_INIT (var_hash );
239+ php_var_serialize (& buf , & retval , & var_hash TSRMLS_CC );
240+ smart_str_0 (& buf );
241+ PHP_VAR_SERIALIZE_DESTROY (var_hash );
242+
243+ PHONGO_RETVAL_SMART_STR (buf );
244+
245+ smart_str_free (& buf );
246+ zval_ptr_dtor (& retval );
247+ }
248+ /* }}} */
249+
250+ /* {{{ proto string Binary::unserialize(string $serialized)
251+ */
252+ PHP_METHOD (Binary , unserialize )
253+ {
254+ php_phongo_binary_t * intern ;
255+ zend_error_handling error_handling ;
256+ char * serialized ;
257+ phongo_zpp_char_len serialized_len ;
258+ #if PHP_VERSION_ID >= 70000
259+ zval props ;
260+ #else
261+ zval * props ;
262+ #endif
263+ php_unserialize_data_t var_hash ;
264+
265+ intern = Z_BINARY_OBJ_P (getThis ());
266+
267+ zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
268+
269+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "s" , & serialized , & serialized_len ) == FAILURE ) {
270+ zend_restore_error_handling (& error_handling TSRMLS_CC );
271+ return ;
272+ }
273+ zend_restore_error_handling (& error_handling TSRMLS_CC );
274+
275+ #if PHP_VERSION_ID < 70000
276+ ALLOC_INIT_ZVAL (props );
277+ #endif
278+ PHP_VAR_UNSERIALIZE_INIT (var_hash );
279+ if (!php_var_unserialize (& props , (const unsigned char * * ) & serialized , (unsigned char * ) serialized + serialized_len , & var_hash TSRMLS_CC )) {
280+ zval_ptr_dtor (& props );
281+ phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC , "%s unserialization failed" , ZSTR_VAL (php_phongo_binary_ce -> name ));
282+
283+ PHP_VAR_UNSERIALIZE_DESTROY (var_hash );
284+ return ;
285+ }
286+ PHP_VAR_UNSERIALIZE_DESTROY (var_hash );
287+
288+ #if PHP_VERSION_ID >= 70000
289+ php_phongo_binary_init_from_hash (intern , HASH_OF (& props ) TSRMLS_CC );
290+ #else
291+ php_phongo_binary_init_from_hash (intern , HASH_OF (props ) TSRMLS_CC );
292+ #endif
293+ zval_ptr_dtor (& props );
294+ }
295+ /* }}} */
296+
202297
203298/* {{{ BSON\Binary */
204299
@@ -211,6 +306,10 @@ ZEND_BEGIN_ARG_INFO_EX(ai_Binary___set_state, 0, 0, 1)
211306 ZEND_ARG_ARRAY_INFO (0 , properties , 0 )
212307ZEND_END_ARG_INFO ()
213308
309+ ZEND_BEGIN_ARG_INFO_EX (ai_Binary_unserialize , 0 , 0 , 1 )
310+ ZEND_ARG_INFO (0 , serialized )
311+ ZEND_END_ARG_INFO ()
312+
214313ZEND_BEGIN_ARG_INFO_EX (ai_Binary_void , 0 , 0 , 0 )
215314ZEND_END_ARG_INFO ()
216315
@@ -219,6 +318,8 @@ static zend_function_entry php_phongo_binary_me[] = {
219318 PHP_ME (Binary , __set_state , ai_Binary___set_state , ZEND_ACC_PUBLIC |ZEND_ACC_STATIC )
220319 PHP_ME (Binary , __toString , ai_Binary_void , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
221320 PHP_ME (Binary , __wakeup , ai_Binary_void , ZEND_ACC_PUBLIC )
321+ PHP_ME (Binary , serialize , ai_Binary_void , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
322+ PHP_ME (Binary , unserialize , ai_Binary_unserialize , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
222323 PHP_ME (Binary , getData , ai_Binary_void , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
223324 PHP_ME (Binary , getType , ai_Binary_void , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
224325 PHP_FE_END
@@ -318,6 +419,7 @@ PHP_MINIT_FUNCTION(Binary)
318419 PHONGO_CE_FINAL (php_phongo_binary_ce );
319420
320421 zend_class_implements (php_phongo_binary_ce TSRMLS_CC , 1 , php_phongo_type_ce );
422+ zend_class_implements (php_phongo_binary_ce TSRMLS_CC , 1 , zend_ce_serializable );
321423
322424 memcpy (& php_phongo_handler_binary , phongo_get_std_object_handlers (), sizeof (zend_object_handlers ));
323425 php_phongo_handler_binary .get_properties = php_phongo_binary_get_properties ;
0 commit comments