@@ -88,51 +88,18 @@ static int phongo_is_array_or_document(zval* val)
8888 return IS_ARRAY ;
8989}
9090
91- /* Checks the return type of a bsonSerialize() method. Returns true on
92- * success; otherwise, throws an exception and returns false.
93- *
94- * TODO: obsolete once the tentative return type in Serializable::bsonSerialize is enforced.
95- */
96- static inline bool phongo_check_bson_serialize_return_type (zval * retval , zend_class_entry * ce )
91+ static bool phongo_bson_encode_serializable (zval * object , zval * out_data )
9792{
98- if (instanceof_function (ce , phongo_persistable_ce )) {
99- // Instances of Persistable must return an array, stdClass, or MongoDB\BSON\Document
100- if (
101- Z_TYPE_P (retval ) != IS_ARRAY && !(Z_TYPE_P (retval ) == IS_OBJECT && (instanceof_function (Z_OBJCE_P (retval ), zend_standard_class_def ) || instanceof_function (Z_OBJCE_P (retval ), phongo_document_ce )))) {
102- phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE ,
103- "Expected %s::%s() to return an array, stdClass, or %s, %s given" ,
104- ZSTR_VAL (ce -> name ),
105- BSON_SERIALIZE_FUNC_NAME ,
106- ZSTR_VAL (phongo_document_ce -> name ),
107- zend_zval_type_name (retval ));
108- return false;
109- }
93+ ZVAL_UNDEF (out_data );
94+ zend_call_method_with_0_params (Z_OBJ_P (object ), NULL , NULL , BSON_SERIALIZE_FUNC_NAME , out_data );
11095
111- return true;
96+ if (Z_ISUNDEF_P (out_data )) {
97+ /* zend_call_method() failed or bsonSerialize() threw an
98+ * exception. Either way, there is nothing else to do. */
99+ return false;
112100 }
113101
114- if (instanceof_function (ce , phongo_serializable_ce )) {
115- // Instances of Serializable must return an array, stdClass, MongoDB\BSON\Document, or MongoDB\BSON\PackedArray
116- if (
117- Z_TYPE_P (retval ) != IS_ARRAY && !(Z_TYPE_P (retval ) == IS_OBJECT && (instanceof_function (Z_OBJCE_P (retval ), zend_standard_class_def ) || instanceof_function (Z_OBJCE_P (retval ), phongo_document_ce ) || instanceof_function (Z_OBJCE_P (retval ), phongo_packedarray_ce )))) {
118- phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE ,
119- "Expected %s::%s() to return an array, stdClass, %s, or %s, %s given" ,
120- ZSTR_VAL (ce -> name ),
121- BSON_SERIALIZE_FUNC_NAME ,
122- ZSTR_VAL (phongo_document_ce -> name ),
123- ZSTR_VAL (phongo_packedarray_ce -> name ),
124- zend_zval_type_name (retval ));
125- return false;
126- }
127-
128- return true;
129- }
130-
131- phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE ,
132- "Expected to receive instance of %s, %s given" ,
133- ZSTR_VAL (phongo_serializable_ce -> name ),
134- ZSTR_VAL (ce -> name ));
135- return false;
102+ return true;
136103}
137104
138105/* Appends the array or object argument to the BSON document.
@@ -166,17 +133,8 @@ static void phongo_bson_append_object(bson_t* bson, phongo_field_path* field_pat
166133 zval obj_data ;
167134 bson_t child ;
168135
169- zend_call_method_with_0_params (Z_OBJ_P (object ), NULL , NULL , BSON_SERIALIZE_FUNC_NAME , & obj_data );
170-
171- if (Z_ISUNDEF (obj_data )) {
172- /* zend_call_method() failed or bsonSerialize() threw an
173- * exception. Either way, there is nothing else to do. */
174- return ;
175- }
176-
177- if (!phongo_check_bson_serialize_return_type (& obj_data , Z_OBJCE_P (object ))) {
136+ if (!phongo_bson_encode_serializable (object , & obj_data )) {
178137 // Exception already thrown
179- zval_ptr_dtor (& obj_data );
180138 return ;
181139 }
182140
@@ -488,17 +446,9 @@ static void phongo_zval_to_bson_internal(zval* data, phongo_field_path* field_pa
488446 /* For any MongoDB\BSON\Serializable, invoke the bsonSerialize method
489447 * and work with the result. */
490448 if (instanceof_function (Z_OBJCE_P (data ), phongo_serializable_ce )) {
491- zend_call_method_with_0_params (Z_OBJ_P (data ), NULL , NULL , BSON_SERIALIZE_FUNC_NAME , & obj_data );
492-
493- if (Z_ISUNDEF (obj_data )) {
494- /* zend_call_method() failed or bsonSerialize() threw an
495- * exception. Either way, there is nothing else to do. */
496- return ;
497- }
498-
499- if (!phongo_check_bson_serialize_return_type (& obj_data , Z_OBJCE_P (data ))) {
449+ if (!phongo_bson_encode_serializable (data , & obj_data )) {
500450 // Exception already thrown
501- goto cleanup ;
451+ return ;
502452 }
503453
504454 if (instanceof_function (Z_OBJCE_P (data ), phongo_persistable_ce )) {
0 commit comments