@@ -46,6 +46,41 @@ PHONGO_API zend_class_entry *php_phongo_binary_ce;
4646
4747zend_object_handlers php_phongo_handler_binary ;
4848
49+ /* Initialize the object from a string and return whether it was successful. */
50+ static bool php_phongo_binary_init (php_phongo_binary_t * intern , const char * data , phongo_zpp_char_len data_len , phongo_long type )
51+ {
52+ if (type < 0 || type > UINT8_MAX ) {
53+ return false;
54+ }
55+
56+ intern -> data = estrndup (data , data_len );
57+ intern -> data_len = data_len ;
58+ intern -> type = (uint8_t ) type ;
59+
60+ return true;
61+ }
62+
63+ /* Initialize the object from a HashTable and return whether it was successful. */
64+ static bool php_phongo_binary_init_from_hash (php_phongo_binary_t * intern , HashTable * props )
65+ {
66+ #if PHP_VERSION_ID >= 70000
67+ zval * data , * type ;
68+
69+ if ((data = zend_hash_str_find (props , "data" , sizeof ("data" )- 1 )) && Z_TYPE_P (data ) == IS_STRING &&
70+ (type = zend_hash_str_find (props , "type" , sizeof ("type" )- 1 )) && Z_TYPE_P (type ) == IS_LONG ) {
71+ return php_phongo_binary_init (intern , Z_STRVAL_P (data ), Z_STRLEN_P (data ), Z_LVAL_P (type ));
72+ }
73+ #else
74+ zval * * data , * * type ;
75+
76+ if (zend_hash_find (props , "data" , sizeof ("data" ), (void * * ) & data ) == SUCCESS && Z_TYPE_PP (data ) == IS_STRING &&
77+ zend_hash_find (props , "type" , sizeof ("type" ), (void * * ) & type ) == SUCCESS && Z_TYPE_PP (type ) == IS_LONG ) {
78+ return php_phongo_binary_init (intern , Z_STRVAL_PP (data ), Z_STRLEN_PP (data ), Z_LVAL_PP (type ));
79+ }
80+ #endif
81+ return false;
82+ }
83+
4984/* {{{ proto BSON\Binary Binary::__construct(string $data, int $type)
5085 Construct a new BSON Binary type */
5186PHP_METHOD (Binary , __construct )
@@ -66,16 +101,55 @@ PHP_METHOD(Binary, __construct)
66101 }
67102 zend_restore_error_handling (& error_handling TSRMLS_CC );
68103
69- if (type < 0 || type > UINT8_MAX ) {
104+ if (! php_phongo_binary_init ( intern , data , data_len , type ) ) {
70105 phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Expected type to be an unsigned 8-bit integer, %" PHONGO_LONG_FORMAT " given" , type );
106+ }
107+ }
108+ /* }}} */
109+
110+ /* {{{ proto Binary::__set_state(array $properties)
111+ */
112+ PHP_METHOD (Binary , __set_state )
113+ {
114+ php_phongo_binary_t * intern ;
115+ HashTable * props ;
116+ zval * array ;
117+
118+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "a" , & array ) == FAILURE ) {
119+ RETURN_FALSE ;
120+ }
121+
122+ object_init_ex (return_value , php_phongo_binary_ce );
123+
124+ intern = Z_BINARY_OBJ_P (return_value );
125+ props = Z_ARRVAL_P (array );
126+
127+ if (!php_phongo_binary_init_from_hash (intern , props )) {
128+ php_error (E_ERROR , "Invalid serialization data for Binary object" );
129+ }
130+ }
131+ /* }}} */
132+
133+ /* {{{ proto Binary::__wakeup()
134+ */
135+ PHP_METHOD (Binary , __wakeup )
136+ {
137+ php_phongo_binary_t * intern ;
138+ HashTable * props ;
139+
140+ if (zend_parse_parameters_none () == FAILURE ) {
71141 return ;
72142 }
73143
74- intern -> data = estrndup (data , data_len );
75- intern -> data_len = data_len ;
76- intern -> type = (uint8_t ) type ;
144+ intern = Z_BINARY_OBJ_P (getThis ());
145+ props = zend_std_get_properties (getThis () TSRMLS_CC );
146+
147+ if (!php_phongo_binary_init_from_hash (intern , props )) {
148+ php_error (E_ERROR , "Invalid serialization data for Binary object" );
149+ }
77150}
78151/* }}} */
152+
79153/* {{{ proto string Binary::getData()
80154 */
81155PHP_METHOD (Binary , getData )
@@ -117,18 +191,19 @@ ZEND_BEGIN_ARG_INFO_EX(ai_Binary___construct, 0, 0, 2)
117191 ZEND_ARG_INFO (0 , type )
118192ZEND_END_ARG_INFO ();
119193
120- ZEND_BEGIN_ARG_INFO_EX (ai_Binary_getData , 0 , 0 , 0 )
194+ ZEND_BEGIN_ARG_INFO_EX (ai_Binary___set_state , 0 , 0 , 1 )
195+ ZEND_ARG_ARRAY_INFO (0 , properties , 0 )
121196ZEND_END_ARG_INFO ();
122197
123- ZEND_BEGIN_ARG_INFO_EX (ai_Binary_getType , 0 , 0 , 0 )
198+ ZEND_BEGIN_ARG_INFO_EX (ai_Binary_void , 0 , 0 , 0 )
124199ZEND_END_ARG_INFO ();
125200
126-
127201static zend_function_entry php_phongo_binary_me [] = {
128202 PHP_ME (Binary , __construct , ai_Binary___construct , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
129- PHP_ME (Binary , getData , ai_Binary_getData , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
130- PHP_ME (Binary , getType , ai_Binary_getType , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
131- PHP_ME (Manager , __wakeUp , NULL , ZEND_ACC_PUBLIC )
203+ PHP_ME (Binary , __set_state , ai_Binary___set_state , ZEND_ACC_PUBLIC |ZEND_ACC_STATIC )
204+ PHP_ME (Binary , getData , ai_Binary_void , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
205+ PHP_ME (Binary , getType , ai_Binary_void , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
206+ PHP_ME (Binary , __wakeup , ai_Binary_void , ZEND_ACC_PUBLIC )
132207 PHP_FE_END
133208};
134209
@@ -174,23 +249,43 @@ phongo_create_object_retval php_phongo_binary_create_object(zend_class_entry *cl
174249#endif
175250} /* }}} */
176251
177- HashTable * php_phongo_binary_get_debug_info (zval * object , int * is_temp TSRMLS_DC ) /* {{{ */
252+ HashTable * php_phongo_binary_get_properties (zval * object TSRMLS_DC ) /* {{{ */
178253{
179254 php_phongo_binary_t * intern ;
255+ HashTable * props ;
256+
257+ intern = Z_BINARY_OBJ_P (object );
258+ props = zend_std_get_properties (object TSRMLS_CC );
259+
260+ if (!intern -> data ) {
261+ return props ;
262+ }
263+
180264#if PHP_VERSION_ID >= 70000
181- zval retval ;
265+ {
266+ zval data , type ;
267+
268+ ZVAL_STRINGL (& data , intern -> data , intern -> data_len );
269+ zend_hash_str_update (props , "data" , sizeof ("data" )- 1 , & data );
270+
271+ ZVAL_LONG (& type , intern -> type );
272+ zend_hash_str_update (props , "type" , sizeof ("type" )- 1 , & type );
273+ }
182274#else
183- zval retval = zval_used_for_init ;
184- #endif
275+ {
276+ zval * data , * type ;
185277
186- intern = Z_BINARY_OBJ_P ( object );
187- * is_temp = 1 ;
188- array_init_size ( & retval , 2 );
278+ MAKE_STD_ZVAL ( data );
279+ ZVAL_STRINGL ( data , intern -> data , intern -> data_len , 1 ) ;
280+ zend_hash_update ( props , "data" , sizeof ( "data" ), & data , sizeof ( data ), NULL );
189281
190- ADD_ASSOC_STRINGL (& retval , "data" , intern -> data , intern -> data_len );
191- ADD_ASSOC_LONG_EX (& retval , "type" , intern -> type );
282+ MAKE_STD_ZVAL (type );
283+ ZVAL_LONG (type , intern -> type );
284+ zend_hash_update (props , "type" , sizeof ("type" ), & type , sizeof (type ), NULL );
285+ }
286+ #endif
192287
193- return Z_ARRVAL ( retval ) ;
288+ return props ;
194289} /* }}} */
195290/* }}} */
196291
@@ -203,12 +298,12 @@ PHP_MINIT_FUNCTION(Binary)
203298 INIT_NS_CLASS_ENTRY (ce , BSON_NAMESPACE , "Binary" , php_phongo_binary_me );
204299 php_phongo_binary_ce = zend_register_internal_class (& ce TSRMLS_CC );
205300 php_phongo_binary_ce -> create_object = php_phongo_binary_create_object ;
206- PHONGO_CE_INIT (php_phongo_binary_ce );
301+ PHONGO_CE_FINAL (php_phongo_binary_ce );
207302
208303 zend_class_implements (php_phongo_binary_ce TSRMLS_CC , 1 , php_phongo_type_ce );
209304
210305 memcpy (& php_phongo_handler_binary , phongo_get_std_object_handlers (), sizeof (zend_object_handlers ));
211- php_phongo_handler_binary .get_debug_info = php_phongo_binary_get_debug_info ;
306+ php_phongo_handler_binary .get_properties = php_phongo_binary_get_properties ;
212307#if PHP_VERSION_ID >= 70000
213308 php_phongo_handler_binary .free_obj = php_phongo_binary_free_object ;
214309 php_phongo_handler_binary .offset = XtOffsetOf (php_phongo_binary_t , std );
0 commit comments