@@ -46,6 +46,37 @@ PHONGO_API zend_class_entry *php_phongo_timestamp_ce;
4646
4747zend_object_handlers php_phongo_handler_timestamp ;
4848
49+ /* Initialize the object from a string and return whether it was successful. */
50+ static bool php_phongo_timestamp_init (php_phongo_timestamp_t * intern , phongo_long increment , phongo_long timestamp )
51+ {
52+ intern -> increment = increment ;
53+ intern -> timestamp = timestamp ;
54+ intern -> initialized = true;
55+
56+ return true;
57+ }
58+
59+ /* Initialize the object from a HashTable and return whether it was successful. */
60+ static bool php_phongo_timestamp_init_from_hash (php_phongo_timestamp_t * intern , HashTable * props )
61+ {
62+ #if PHP_VERSION_ID >= 70000
63+ zval * increment , * timestamp ;
64+
65+ if ((increment = zend_hash_str_find (props , "increment" , sizeof ("increment" )- 1 )) && Z_TYPE_P (increment ) == IS_LONG &&
66+ (timestamp = zend_hash_str_find (props , "timestamp" , sizeof ("timestamp" )- 1 )) && Z_TYPE_P (timestamp ) == IS_LONG ) {
67+ return php_phongo_timestamp_init (intern , Z_LVAL_P (increment ), Z_LVAL_P (timestamp ));
68+ }
69+ #else
70+ zval * * increment , * * timestamp ;
71+
72+ if (zend_hash_find (props , "increment" , sizeof ("increment" ), (void * * ) & increment ) == SUCCESS && Z_TYPE_PP (increment ) == IS_LONG &&
73+ zend_hash_find (props , "timestamp" , sizeof ("timestamp" ), (void * * ) & timestamp ) == SUCCESS && Z_TYPE_PP (timestamp ) == IS_LONG ) {
74+ return php_phongo_timestamp_init (intern , Z_LVAL_PP (increment ), Z_LVAL_PP (timestamp ));
75+ }
76+ #endif
77+ return false;
78+ }
79+
4980/* {{{ proto BSON\Timestamp Timestamp::__construct(integer $increment, int $timestamp)
5081 Construct a new BSON Timestamp (4bytes increment, 4bytes timestamp) */
5182PHP_METHOD (Timestamp , __construct )
@@ -75,10 +106,33 @@ PHP_METHOD(Timestamp, __construct)
75106 return ;
76107 }
77108
78- intern -> increment = increment ;
79- intern -> timestamp = timestamp ;
109+ php_phongo_timestamp_init (intern , increment , timestamp );
110+ }
111+ /* }}} */
112+
113+ /* {{{ proto Timestamp::__set_state(array $properties)
114+ */
115+ PHP_METHOD (Timestamp , __set_state )
116+ {
117+ php_phongo_timestamp_t * intern ;
118+ HashTable * props ;
119+ zval * array ;
120+
121+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "a" , & array ) == FAILURE ) {
122+ RETURN_FALSE ;
123+ }
124+
125+ object_init_ex (return_value , php_phongo_timestamp_ce );
126+
127+ intern = Z_TIMESTAMP_OBJ_P (return_value );
128+ props = Z_ARRVAL_P (array );
129+
130+ if (!php_phongo_timestamp_init_from_hash (intern , props )) {
131+ php_error (E_ERROR , "Invalid serialization data for Timestamp object" );
132+ }
80133}
81134/* }}} */
135+
82136/* {{{ proto string Timestamp::__toString()
83137 Returns [increment:timestamp] */
84138PHP_METHOD (Timestamp , __toString )
@@ -100,21 +154,45 @@ PHP_METHOD(Timestamp, __toString)
100154}
101155/* }}} */
102156
157+ /* {{{ proto Timestamp::__wakeup()
158+ */
159+ PHP_METHOD (Timestamp , __wakeup )
160+ {
161+ php_phongo_timestamp_t * intern ;
162+ HashTable * props ;
163+
164+ if (zend_parse_parameters_none () == FAILURE ) {
165+ return ;
166+ }
167+
168+ intern = Z_TIMESTAMP_OBJ_P (getThis ());
169+ props = zend_std_get_properties (getThis () TSRMLS_CC );
170+
171+ if (!php_phongo_timestamp_init_from_hash (intern , props )) {
172+ php_error (E_ERROR , "Invalid serialization data for Timestamp object" );
173+ }
174+ }
175+ /* }}} */
176+
103177/* {{{ BSON\Timestamp */
104178
105179ZEND_BEGIN_ARG_INFO_EX (ai_Timestamp___construct , 0 , 0 , 2 )
106180 ZEND_ARG_INFO (0 , increment )
107181 ZEND_ARG_INFO (0 , timestamp )
108182ZEND_END_ARG_INFO ();
109183
110- ZEND_BEGIN_ARG_INFO_EX (ai_Timestamp___toString , 0 , 0 , 0 )
111- ZEND_END_ARG_INFO ();
184+ ZEND_BEGIN_ARG_INFO_EX (ai_Timestamp___set_state , 0 , 0 , 1 )
185+ ZEND_ARG_ARRAY_INFO (0 , properties , 0 )
186+ ZEND_END_ARG_INFO ()
112187
188+ ZEND_BEGIN_ARG_INFO_EX (ai_Timestamp_void , 0 , 0 , 0 )
189+ ZEND_END_ARG_INFO ()
113190
114191static zend_function_entry php_phongo_timestamp_me [] = {
115192 PHP_ME (Timestamp , __construct , ai_Timestamp___construct , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
116- PHP_ME (Timestamp , __toString , ai_Timestamp___toString , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
117- PHP_ME (Manager , __wakeUp , NULL , ZEND_ACC_PUBLIC )
193+ PHP_ME (Timestamp , __set_state , ai_Timestamp___set_state , ZEND_ACC_PUBLIC |ZEND_ACC_STATIC )
194+ PHP_ME (Timestamp , __toString , ai_Timestamp_void , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
195+ PHP_ME (Timestamp , __wakeup , ai_Timestamp_void , ZEND_ACC_PUBLIC )
118196 PHP_FE_END
119197};
120198
@@ -177,6 +255,45 @@ HashTable *php_phongo_timestamp_get_debug_info(zval *object, int *is_temp TSRMLS
177255
178256 return Z_ARRVAL (retval );
179257} /* }}} */
258+
259+ HashTable * php_phongo_timestamp_get_properties (zval * object TSRMLS_DC ) /* {{{ */
260+ {
261+ php_phongo_timestamp_t * intern ;
262+ HashTable * props ;
263+
264+ intern = Z_TIMESTAMP_OBJ_P (object );
265+ props = zend_std_get_properties (object TSRMLS_CC );
266+
267+ if (!intern -> initialized ) {
268+ return props ;
269+ }
270+
271+ #if PHP_VERSION_ID >= 70000
272+ {
273+ zval increment , timestamp ;
274+
275+ ZVAL_LONG (& increment , intern -> increment );
276+ zend_hash_str_update (props , "increment" , sizeof ("increment" )- 1 , & increment );
277+
278+ ZVAL_LONG (& timestamp , intern -> timestamp );
279+ zend_hash_str_update (props , "timestamp" , sizeof ("timestamp" )- 1 , & timestamp );
280+ }
281+ #else
282+ {
283+ zval * increment , * timestamp ;
284+
285+ MAKE_STD_ZVAL (increment );
286+ ZVAL_LONG (increment , intern -> increment );
287+ zend_hash_update (props , "increment" , sizeof ("increment" ), & increment , sizeof (increment ), NULL );
288+
289+ MAKE_STD_ZVAL (timestamp );
290+ ZVAL_LONG (timestamp , intern -> timestamp );
291+ zend_hash_update (props , "timestamp" , sizeof ("timestamp" ), & timestamp , sizeof (timestamp ), NULL );
292+ }
293+ #endif
294+
295+ return props ;
296+ } /* }}} */
180297/* }}} */
181298
182299/* {{{ PHP_MINIT_FUNCTION */
@@ -189,11 +306,10 @@ PHP_MINIT_FUNCTION(Timestamp)
189306 php_phongo_timestamp_ce = zend_register_internal_class (& ce TSRMLS_CC );
190307 php_phongo_timestamp_ce -> create_object = php_phongo_timestamp_create_object ;
191308 PHONGO_CE_FINAL (php_phongo_timestamp_ce );
192- PHONGO_CE_DISABLE_SERIALIZATION (php_phongo_timestamp_ce );
193309
194310 zend_class_implements (php_phongo_timestamp_ce TSRMLS_CC , 1 , php_phongo_type_ce );
195311 memcpy (& php_phongo_handler_timestamp , phongo_get_std_object_handlers (), sizeof (zend_object_handlers ));
196- php_phongo_handler_timestamp .get_debug_info = php_phongo_timestamp_get_debug_info ;
312+ php_phongo_handler_timestamp .get_properties = php_phongo_timestamp_get_properties ;
197313#if PHP_VERSION_ID >= 70000
198314 php_phongo_handler_timestamp .free_obj = php_phongo_timestamp_free_object ;
199315 php_phongo_handler_timestamp .offset = XtOffsetOf (php_phongo_timestamp_t , std );
0 commit comments