@@ -46,6 +46,37 @@ PHONGO_API zend_class_entry *php_phongo_decimal128_ce;
46
46
47
47
zend_object_handlers php_phongo_handler_decimal128 ;
48
48
49
+ /* Initialize the object from a string and return whether it was successful. */
50
+ static bool php_phongo_decimal128_init (php_phongo_decimal128_t * intern , const char * value )
51
+ {
52
+ if (bson_decimal128_from_string (value , & intern -> decimal )) {
53
+ intern -> initialized = true;
54
+
55
+ return true;
56
+ }
57
+
58
+ return false;
59
+ }
60
+
61
+ /* Initialize the object from a HashTable and return whether it was successful. */
62
+ static bool php_phongo_decimal128_init_from_hash (php_phongo_decimal128_t * intern , HashTable * props )
63
+ {
64
+ #if PHP_VERSION_ID >= 70000
65
+ zval * dec ;
66
+
67
+ if ((dec = zend_hash_str_find (props , "dec" , sizeof ("dec" )- 1 )) && Z_TYPE_P (dec ) == IS_STRING ) {
68
+ return php_phongo_decimal128_init (intern , Z_STRVAL_P (dec ));
69
+ }
70
+ #else
71
+ zval * * dec ;
72
+
73
+ if (zend_hash_find (props , "dec" , sizeof ("dec" ), (void * * ) & dec ) == SUCCESS && Z_TYPE_PP (dec ) == IS_STRING ) {
74
+ return php_phongo_decimal128_init (intern , Z_STRVAL_PP (dec ));
75
+ }
76
+ #endif
77
+ return false;
78
+ }
79
+
49
80
/* {{{ proto BSON\Decimal128 Decimal128::__construct(string $value)
50
81
Construct a new BSON Decimal128 type */
51
82
PHP_METHOD (Decimal128 , __construct )
@@ -65,9 +96,31 @@ PHP_METHOD(Decimal128, __construct)
65
96
}
66
97
zend_restore_error_handling (& error_handling TSRMLS_CC );
67
98
68
- if ( ! bson_decimal128_from_string ( value , & intern -> decimal )) {
99
+ if (! php_phongo_decimal128_init ( intern , value )) {
69
100
phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Error parsing decimal string: %s" , value );
70
- return ;
101
+ }
102
+ }
103
+ /* }}} */
104
+
105
+ /* {{{ proto Decimal128::__set_state(array $properties)
106
+ */
107
+ PHP_METHOD (Decimal128 , __set_state )
108
+ {
109
+ php_phongo_decimal128_t * intern ;
110
+ HashTable * props ;
111
+ zval * array ;
112
+
113
+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "a" , & array ) == FAILURE ) {
114
+ RETURN_FALSE ;
115
+ }
116
+
117
+ object_init_ex (return_value , php_phongo_decimal128_ce );
118
+
119
+ intern = Z_DECIMAL128_OBJ_P (return_value );
120
+ props = Z_ARRVAL_P (array );
121
+
122
+ if (!php_phongo_decimal128_init_from_hash (intern , props )) {
123
+ php_error (E_ERROR , "Invalid serialization data for Decimal128 object" );
71
124
}
72
125
}
73
126
/* }}} */
@@ -91,21 +144,44 @@ PHP_METHOD(Decimal128, __toString)
91
144
}
92
145
/* }}} */
93
146
147
+ /* {{{ proto Decimal128::__wakeup()
148
+ */
149
+ PHP_METHOD (Decimal128 , __wakeup )
150
+ {
151
+ php_phongo_decimal128_t * intern ;
152
+ HashTable * props ;
153
+
154
+ if (zend_parse_parameters_none () == FAILURE ) {
155
+ return ;
156
+ }
157
+
158
+ intern = Z_DECIMAL128_OBJ_P (getThis ());
159
+ props = zend_std_get_properties (getThis () TSRMLS_CC );
160
+
161
+ if (!php_phongo_decimal128_init_from_hash (intern , props )) {
162
+ php_error (E_ERROR , "Invalid serialization data for Decimal128 object" );
163
+ }
164
+ }
165
+ /* }}} */
94
166
95
167
/* {{{ BSON\Decimal128 */
96
168
97
169
ZEND_BEGIN_ARG_INFO_EX (ai_Decimal128___construct , 0 , 0 , 2 )
98
170
ZEND_ARG_INFO (0 , value )
99
171
ZEND_END_ARG_INFO ();
100
172
101
- ZEND_BEGIN_ARG_INFO_EX (ai_Decimal128___toString , 0 , 0 , 0 )
173
+ ZEND_BEGIN_ARG_INFO_EX (ai_Decimal128___set_state , 0 , 0 , 1 )
174
+ ZEND_ARG_ARRAY_INFO (0 , properties , 0 )
102
175
ZEND_END_ARG_INFO ();
103
176
177
+ ZEND_BEGIN_ARG_INFO_EX (ai_Decimal128_void , 0 , 0 , 0 )
178
+ ZEND_END_ARG_INFO ();
104
179
105
180
static zend_function_entry php_phongo_decimal128_me [] = {
106
181
PHP_ME (Decimal128 , __construct , ai_Decimal128___construct , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
107
- PHP_ME (Decimal128 , __toString , ai_Decimal128___toString , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
108
- PHP_ME (Manager , __wakeUp , NULL , ZEND_ACC_PUBLIC )
182
+ PHP_ME (Decimal128 , __set_state , ai_Decimal128___set_state , ZEND_ACC_PUBLIC |ZEND_ACC_STATIC )
183
+ PHP_ME (Decimal128 , __toString , ai_Decimal128_void , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
184
+ PHP_ME (Decimal128 , __wakeup , ai_Decimal128_void , ZEND_ACC_PUBLIC )
109
185
PHP_FE_END
110
186
};
111
187
@@ -148,24 +224,39 @@ phongo_create_object_retval php_phongo_decimal128_create_object(zend_class_entry
148
224
#endif
149
225
} /* }}} */
150
226
151
- HashTable * php_phongo_decimal128_get_debug_info (zval * object , int * is_temp TSRMLS_DC ) /* {{{ */
227
+ HashTable * php_phongo_decimal128_get_properties (zval * object TSRMLS_DC ) /* {{{ */
152
228
{
153
229
php_phongo_decimal128_t * intern ;
154
- char outbuf [BSON_DECIMAL128_STRING ] = "" ;
155
- #if PHP_VERSION_ID >= 70000
156
- zval retval ;
157
- #else
158
- zval retval = zval_used_for_init ;
159
- #endif
230
+ HashTable * props ;
231
+ char outbuf [BSON_DECIMAL128_STRING ] = "" ;
160
232
161
233
intern = Z_DECIMAL128_OBJ_P (object );
162
- * is_temp = 1 ;
163
- array_init_size (& retval , 1 );
234
+ props = zend_std_get_properties (object TSRMLS_CC );
235
+
236
+ if (!intern -> initialized ) {
237
+ return props ;
238
+ }
164
239
165
240
bson_decimal128_to_string (& intern -> decimal , outbuf );
166
- ADD_ASSOC_STRING (& retval , "dec" , outbuf );
167
241
168
- return Z_ARRVAL (retval );
242
+ #if PHP_VERSION_ID >= 70000
243
+ {
244
+ zval dec ;
245
+
246
+ ZVAL_STRING (& dec , outbuf );
247
+ zend_hash_str_update (props , "dec" , sizeof ("dec" )- 1 , & dec );
248
+ }
249
+ #else
250
+ {
251
+ zval * dec ;
252
+
253
+ MAKE_STD_ZVAL (dec );
254
+ ZVAL_STRING (dec , outbuf , 1 );
255
+ zend_hash_update (props , "dec" , sizeof ("dec" ), & dec , sizeof (dec ), NULL );
256
+ }
257
+ #endif
258
+
259
+ return props ;
169
260
} /* }}} */
170
261
/* }}} */
171
262
@@ -179,12 +270,11 @@ PHP_MINIT_FUNCTION(Decimal128)
179
270
php_phongo_decimal128_ce = zend_register_internal_class (& ce TSRMLS_CC );
180
271
php_phongo_decimal128_ce -> create_object = php_phongo_decimal128_create_object ;
181
272
PHONGO_CE_FINAL (php_phongo_decimal128_ce );
182
- PHONGO_CE_DISABLE_SERIALIZATION (php_phongo_decimal128_ce );
183
273
184
274
zend_class_implements (php_phongo_decimal128_ce TSRMLS_CC , 1 , php_phongo_type_ce );
185
275
186
276
memcpy (& php_phongo_handler_decimal128 , phongo_get_std_object_handlers (), sizeof (zend_object_handlers ));
187
- php_phongo_handler_decimal128 .get_debug_info = php_phongo_decimal128_get_debug_info ;
277
+ php_phongo_handler_decimal128 .get_properties = php_phongo_decimal128_get_properties ;
188
278
#if PHP_VERSION_ID >= 70000
189
279
php_phongo_handler_decimal128 .free_obj = php_phongo_decimal128_free_object ;
190
280
php_phongo_handler_decimal128 .offset = XtOffsetOf (php_phongo_decimal128_t , std );
0 commit comments