@@ -450,32 +450,25 @@ bool php_phongo_bson_visit_document(const bson_iter_t *iter __attribute__((unuse
450
450
{
451
451
zval * retval = ((php_phongo_bson_state * )data )-> zchild ;
452
452
bson_iter_t child ;
453
- zend_class_entry * ce = zend_standard_class_def ;
454
453
TSRMLS_FETCH ();
455
454
456
455
if (bson_iter_init (& child , v_document )) {
457
- php_phongo_bson_state state = {NULL , {{ NULL , 0 }, { NULL , 0 }} };
456
+ php_phongo_bson_state state = {NULL , {NULL , NULL } };
458
457
459
458
state .map = ((php_phongo_bson_state * )data )-> map ;
460
459
461
460
MAKE_STD_ZVAL (state .zchild );
462
461
array_init (state .zchild );
463
462
464
463
if (!bson_iter_visit_all (& child , & php_bson_visitors , & state )) {
465
- if (state .map .document .classname_len ) {
466
- ce = zend_fetch_class (state .map .document .classname , state .map .document .classname_len , ZEND_FETCH_CLASS_AUTO TSRMLS_CC );
467
- if (ce -> type == ZEND_INTERNAL_CLASS && ce -> constructor ) {
468
- zval tmp_val = * state .zchild ;
464
+ if (state .map .document ) {
465
+ zval tmp_val = * state .zchild ;
469
466
470
- object_and_properties_init (state .zchild , ce , NULL );
471
- zend_call_method_with_1_params (& state .zchild , ce , & ce -> constructor , "__construct" , NULL , & tmp_val );
472
-
473
- zval_dtor (& tmp_val );
474
- } else {
475
- object_and_properties_init (state .zchild , ce , Z_ARRVAL_P (state .zchild ));
476
- }
467
+ object_and_properties_init (state .zchild , state .map .document , NULL );
468
+ zend_call_method_with_1_params (& state .zchild , NULL , NULL , BSON_UNSERIALIZE_FUNC_NAME , NULL , & tmp_val );
469
+ zval_dtor (& tmp_val );
477
470
} else {
478
- object_and_properties_init (state .zchild , ce , Z_ARRVAL_P (state .zchild ));
471
+ object_and_properties_init (state .zchild , zend_standard_class_def , Z_ARRVAL_P (state .zchild ));
479
472
}
480
473
481
474
add_assoc_zval (retval , key , state .zchild );
@@ -491,12 +484,11 @@ bool php_phongo_bson_visit_document(const bson_iter_t *iter __attribute__((unuse
491
484
bool php_phongo_bson_visit_array (const bson_iter_t * iter __attribute__((unused )), const char * key , const bson_t * v_array , void * data )
492
485
{
493
486
zval * retval = ((php_phongo_bson_state * )data )-> zchild ;
494
- zend_class_entry * ce = zend_standard_class_def ;
495
487
bson_iter_t child ;
496
488
TSRMLS_FETCH ();
497
489
498
490
if (bson_iter_init (& child , v_array )) {
499
- php_phongo_bson_state state = {NULL , {{ NULL , 0 }, { NULL , 0 }} };
491
+ php_phongo_bson_state state = {NULL , {NULL , NULL } };
500
492
501
493
state .map = ((php_phongo_bson_state * )data )-> map ;
502
494
@@ -505,20 +497,12 @@ bool php_phongo_bson_visit_array(const bson_iter_t *iter __attribute__((unused))
505
497
506
498
if (!bson_iter_visit_all (& child , & php_bson_visitors , & state )) {
507
499
508
- if (state .map .array .classname_len ) {
509
- ce = zend_fetch_class (state .map .array .classname , state .map .array .classname_len , ZEND_FETCH_CLASS_AUTO TSRMLS_CC );
510
- if (ce -> type == ZEND_INTERNAL_CLASS && ce -> constructor ) {
511
- zval tmp_val = * state .zchild ;
512
-
513
- object_and_properties_init (state .zchild , ce , NULL );
514
- zend_call_method_with_1_params (& state .zchild , ce , & ce -> constructor , "__construct" , NULL , & tmp_val );
500
+ if (state .map .array ) {
501
+ zval tmp_val = * state .zchild ;
515
502
516
- zval_dtor (& tmp_val );
517
- } else {
518
- object_and_properties_init (state .zchild , ce , Z_ARRVAL_P (state .zchild ));
519
- }
520
- } else {
521
- object_and_properties_init (state .zchild , ce , Z_ARRVAL_P (state .zchild ));
503
+ object_and_properties_init (state .zchild , state .map .array , NULL );
504
+ zend_call_method_with_1_params (& state .zchild , NULL , NULL , BSON_UNSERIALIZE_FUNC_NAME , NULL , & tmp_val );
505
+ zval_dtor (& tmp_val );
522
506
}
523
507
524
508
add_assoc_zval (retval , key , state .zchild );
@@ -842,11 +826,12 @@ PHP_FUNCTION(fromArray)
842
826
Returns the PHP representation of a BSON value, optionally converting them into custom types/classes */
843
827
PHP_FUNCTION (toArray )
844
828
{
845
- char * data , * document_classname , * array_classname ;
846
- int data_len , document_classname_len , array_classname_len ;
829
+ char * data , * classname ;
830
+ int data_len , classname_len ;
847
831
zval * typemap = NULL ;
848
- zend_bool document_classname_free = 0 , array_classname_free = 0 ;
849
- php_phongo_bson_state state = {NULL , {{NULL , 0 }, {NULL , 0 }}};
832
+ zend_bool classname_free = 0 ;
833
+ zend_class_entry * array_ce = NULL , * document_ce = NULL ;
834
+ php_phongo_bson_state state = {NULL , {NULL , NULL } };
850
835
851
836
(void )return_value_ptr ; (void )this_ptr ; (void )return_value_used ; /* We don't use these */
852
837
@@ -855,26 +840,30 @@ PHP_FUNCTION(toArray)
855
840
}
856
841
857
842
if (typemap ) {
858
- array_classname = php_array_fetchl_string (typemap , "array" , sizeof ("array" )- 1 , & array_classname_len , & array_classname_free );
859
- if (array_classname_len ) {
860
- state .map .array .classname_len = array_classname_len ;
861
- state .map .array .classname = array_classname ;
843
+ classname = php_array_fetchl_string (typemap , "array" , sizeof ("array" )- 1 , & classname_len , & classname_free );
844
+ array_ce = zend_fetch_class (classname , classname_len , ZEND_FETCH_CLASS_AUTO TSRMLS_CC );
845
+
846
+ if (instanceof_function (array_ce , php_phongo_unserializable_ce TSRMLS_CC )) {
847
+ state .map .array = array_ce ;
848
+ }
849
+ if (classname_free ) {
850
+ efree (classname );
862
851
}
863
852
864
- document_classname = php_array_fetchl_string (typemap , "document" , sizeof ("document" )- 1 , & document_classname_len , & document_classname_free );
865
- if (document_classname_len ) {
866
- state .map .document .classname_len = document_classname_len ;
867
- state .map .document .classname = document_classname ;
853
+ classname = php_array_fetchl_string (typemap , "document" , sizeof ("document" )- 1 , & classname_len , & classname_free );
854
+ document_ce = zend_fetch_class (classname , classname_len , ZEND_FETCH_CLASS_AUTO TSRMLS_CC );
855
+ if (instanceof_function (document_ce , php_phongo_unserializable_ce TSRMLS_CC )) {
856
+ state .map .document = document_ce ;
857
+ }
858
+ if (classname_free ) {
859
+ efree (classname );
868
860
}
869
861
}
862
+
870
863
state .zchild = return_value ;
871
864
if (!bson_to_zval ((const unsigned char * )data , data_len , & state )) {
872
865
RETURN_NULL ();
873
866
}
874
-
875
- if (document_classname_free ) {
876
- efree (document_classname );
877
- }
878
867
}
879
868
/* }}} */
880
869
0 commit comments