@@ -471,26 +471,28 @@ bool php_phongo_bson_visit_document(const bson_iter_t *iter ARG_UNUSED, const ch
471
471
array_init (state .zchild );
472
472
473
473
if (!bson_iter_visit_all (& child , & php_bson_visitors , & state )) {
474
- if (state .odm ) {
474
+ /* If php_phongo_bson_visit_binary() finds an ODM class, it should
475
+ * supersede a default type map and named document class. */
476
+ if (state .odm && state .map .document_type == PHONGO_TYPEMAP_NONE ) {
475
477
state .map .document_type = PHONGO_TYPEMAP_CLASS ;
476
478
}
479
+
477
480
switch (state .map .document_type ) {
478
481
case PHONGO_TYPEMAP_NATIVE_ARRAY :
479
482
add_assoc_zval (retval , key , state .zchild );
480
483
Z_SET_REFCOUNT_P (state .zchild , 1 );
481
484
break ;
482
485
483
- case PHONGO_TYPEMAP_CLASS :
484
- if (instanceof_function (state .odm ? state .odm : state .map .document , php_phongo_unserializable_ce TSRMLS_CC )) {
485
- zval * obj = NULL ;
486
+ case PHONGO_TYPEMAP_CLASS : {
487
+ zval * obj = NULL ;
486
488
487
- MAKE_STD_ZVAL (obj );
488
- object_init_ex (obj , state .odm ? state .odm : state .map .document );
489
- zend_call_method_with_1_params (& obj , NULL , NULL , BSON_UNSERIALIZE_FUNC_NAME , NULL , state .zchild );
490
- add_assoc_zval (retval , key , obj );
491
- zval_ptr_dtor (& state .zchild );
492
- break ;
493
- }
489
+ MAKE_STD_ZVAL (obj );
490
+ object_init_ex (obj , state .odm ? state .odm : state .map .document );
491
+ zend_call_method_with_1_params (& obj , NULL , NULL , BSON_UNSERIALIZE_FUNC_NAME , NULL , state .zchild );
492
+ add_assoc_zval (retval , key , obj );
493
+ zval_ptr_dtor (& state .zchild );
494
+ break ;
495
+ }
494
496
495
497
case PHONGO_TYPEMAP_NATIVE_OBJECT :
496
498
default :
@@ -522,23 +524,17 @@ bool php_phongo_bson_visit_array(const bson_iter_t *iter ARG_UNUSED, const char
522
524
if (!bson_iter_visit_all (& child , & php_bson_visitors , & state )) {
523
525
524
526
switch (state .map .array_type ) {
525
- case PHONGO_TYPEMAP_CLASS :
526
- if (instanceof_function (state .map .array , php_phongo_unserializable_ce TSRMLS_CC )) {
527
- zval * obj = NULL ;
528
-
529
- MAKE_STD_ZVAL (obj );
530
- object_init_ex (obj , state .map .array );
531
- zend_call_method_with_1_params (& obj , NULL , NULL , BSON_UNSERIALIZE_FUNC_NAME , NULL , state .zchild );
532
- add_assoc_zval (retval , key , obj );
533
- zval_ptr_dtor (& state .zchild );
534
- break ;
535
- }
536
- /* If the object someehow doesn't implement php_phongo_unserializable_ce then use stdclass.
537
- * This is needed as we need to know how to pass the state.zchild to the class to populate it.
538
- * Not all classes have ctor that accepts first parameter array of values.
539
- */
527
+ case PHONGO_TYPEMAP_CLASS : {
528
+ zval * obj = NULL ;
529
+
530
+ MAKE_STD_ZVAL (obj );
531
+ object_init_ex (obj , state .map .array );
532
+ zend_call_method_with_1_params (& obj , NULL , NULL , BSON_UNSERIALIZE_FUNC_NAME , NULL , state .zchild );
533
+ add_assoc_zval (retval , key , obj );
534
+ zval_ptr_dtor (& state .zchild );
535
+ break ;
536
+ }
540
537
541
- /* break intentionally omitted */
542
538
case PHONGO_TYPEMAP_NATIVE_OBJECT :
543
539
object_and_properties_init (state .zchild , zend_standard_class_def , Z_ARRVAL_P (state .zchild ));
544
540
add_assoc_zval (retval , key , state .zchild );
@@ -910,9 +906,9 @@ int bson_to_zval(const unsigned char *data, int data_len, php_phongo_bson_state
910
906
array_init (state -> zchild );
911
907
bson_iter_visit_all (& iter , & php_bson_visitors , state );
912
908
913
- /* If php_phongo_bson_visit_binary() finds an ODM class, it supersedes our
914
- * document type. */
915
- if (state -> odm ) {
909
+ /* If php_phongo_bson_visit_binary() finds an ODM class, it should supersede
910
+ * a default type map and named root class . */
911
+ if (state -> odm && state -> map . root_type == PHONGO_TYPEMAP_NONE ) {
916
912
state -> map .root_type = PHONGO_TYPEMAP_CLASS ;
917
913
}
918
914
@@ -921,19 +917,16 @@ int bson_to_zval(const unsigned char *data, int data_len, php_phongo_bson_state
921
917
/* Nothing to do here */
922
918
break ;
923
919
924
- case PHONGO_TYPEMAP_CLASS :
925
- /* If the class implements Unserializable, initialize the object
926
- * from our array data; otherwise, fall through to native object. */
927
- if (instanceof_function (state -> odm ? state -> odm : state -> map .root , php_phongo_unserializable_ce TSRMLS_CC )) {
928
- zval * obj = NULL ;
929
-
930
- MAKE_STD_ZVAL (obj );
931
- object_init_ex (obj , state -> odm ? state -> odm : state -> map .root );
932
- zend_call_method_with_1_params (& obj , NULL , NULL , BSON_UNSERIALIZE_FUNC_NAME , NULL , state -> zchild );
933
- zval_ptr_dtor (& state -> zchild );
934
- state -> zchild = obj ;
935
- break ;
936
- }
920
+ case PHONGO_TYPEMAP_CLASS : {
921
+ zval * obj = NULL ;
922
+
923
+ MAKE_STD_ZVAL (obj );
924
+ object_init_ex (obj , state -> odm ? state -> odm : state -> map .root );
925
+ zend_call_method_with_1_params (& obj , NULL , NULL , BSON_UNSERIALIZE_FUNC_NAME , NULL , state -> zchild );
926
+ zval_ptr_dtor (& state -> zchild );
927
+ state -> zchild = obj ;
928
+ break ;
929
+ }
937
930
938
931
case PHONGO_TYPEMAP_NATIVE_OBJECT :
939
932
default :
0 commit comments