@@ -299,9 +299,16 @@ static HashTable* php_phongo_binary_get_debug_info(zend_object* object, int* is_
299299
300300 zend_hash_str_update (props , "vector" , sizeof ("vector" ) - 1 , & vector );
301301
302- zval vector_type ;
302+ zval vector_type ;
303+ zend_object * vector_type_case = phongo_bson_vector_type_to_case (phongo_binary_get_vector_type (intern ));
303304
304- ZVAL_LONG (& vector_type , phongo_binary_get_vector_type (intern ));
305+ // The vector should always be valid by this point, but check for an error
306+ if (!vector_type_case ) {
307+ phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE , "Binary vector data is invalid" );
308+ return props ;
309+ }
310+
311+ ZVAL_OBJ_COPY (& vector_type , vector_type_case );
305312 zend_hash_str_update (props , "vectorType" , sizeof ("vectorType" ) - 1 , & vector_type );
306313 }
307314
@@ -496,7 +503,7 @@ static PHP_METHOD(MongoDB_BSON_Binary, fromVector)
496503 Z_PARAM_OBJ_OF_CLASS (type , php_phongo_vectortype_ce )
497504 PHONGO_PARSE_PARAMETERS_END ();
498505
499- switch (phongo_bson_vector_type_from_name ( Z_STRVAL_P ( zend_enum_fetch_case_name ( type )) )) {
506+ switch (phongo_bson_vector_type_from_case ( type )) {
500507 case PHONGO_BSON_VECTOR_TYPE_FLOAT32 :
501508 phongo_binary_init_vector_from_float32_array (intern , vector );
502509 return ;
@@ -545,7 +552,7 @@ static PHP_METHOD(MongoDB_BSON_Binary, getVectorType)
545552 RETURN_THROWS ();
546553 }
547554
548- const char * type_case = phongo_bson_vector_type_to_name (phongo_binary_get_vector_type (Z_BINARY_OBJ_P ( getThis ()) ));
555+ const char * type_case = phongo_bson_vector_type_to_name (phongo_binary_get_vector_type (intern ));
549556
550557 // The vector should always be valid by this point, but check for an error
551558 if (!type_case ) {
@@ -558,43 +565,48 @@ static PHP_METHOD(MongoDB_BSON_Binary, getVectorType)
558565
559566static void phongo_binary_get_vector_as_array (const php_phongo_binary_t * intern , zval * return_value )
560567{
561- phongo_bson_vector_type_t type = phongo_binary_get_vector_type (intern );
562-
563- // The vector should always be valid by this point, but check for an error
564- if (type == PHONGO_BSON_VECTOR_TYPE_UNKNOWN ) {
565- phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE , "Binary vector data is invalid" );
566- RETURN_THROWS ();
567- }
568-
569568 bson_t tmp_doc = BSON_INITIALIZER ;
570569
571- if (type == PHONGO_BSON_VECTOR_TYPE_INT8 ) {
572- bson_vector_int8_const_view_t view ;
570+ switch (phongo_binary_get_vector_type (intern )) {
571+ case PHONGO_BSON_VECTOR_TYPE_INT8 : {
572+ bson_vector_int8_const_view_t view ;
573573
574- if (!bson_vector_int8_const_view_init (& view , (const uint8_t * ) intern -> data , intern -> data_len ) ||
575- !BSON_APPEND_ARRAY_FROM_VECTOR_INT8 (& tmp_doc , "vector" , view )) {
576- phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE , "Failed to convert binary vector data to an array" );
577- bson_destroy (& tmp_doc );
578- RETURN_THROWS ();
574+ if (!bson_vector_int8_const_view_init (& view , (const uint8_t * ) intern -> data , intern -> data_len ) ||
575+ !BSON_APPEND_ARRAY_FROM_VECTOR_INT8 (& tmp_doc , "vector" , view )) {
576+ phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE , "Failed to convert binary vector data to an array" );
577+ bson_destroy (& tmp_doc );
578+ RETURN_THROWS ();
579+ }
580+
581+ break ;
579582 }
580- } else if ( type == PHONGO_BSON_VECTOR_TYPE_FLOAT32 ) {
581- bson_vector_float32_const_view_t view ;
583+ case PHONGO_BSON_VECTOR_TYPE_FLOAT32 : {
584+ bson_vector_float32_const_view_t view ;
582585
583- if (!bson_vector_float32_const_view_init (& view , (const uint8_t * ) intern -> data , intern -> data_len ) ||
584- !BSON_APPEND_ARRAY_FROM_VECTOR_FLOAT32 (& tmp_doc , "vector" , view )) {
585- phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE , "Failed to convert binary vector data to an array" );
586- bson_destroy (& tmp_doc );
587- RETURN_THROWS ();
586+ if (!bson_vector_float32_const_view_init (& view , (const uint8_t * ) intern -> data , intern -> data_len ) ||
587+ !BSON_APPEND_ARRAY_FROM_VECTOR_FLOAT32 (& tmp_doc , "vector" , view )) {
588+ phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE , "Failed to convert binary vector data to an array" );
589+ bson_destroy (& tmp_doc );
590+ RETURN_THROWS ();
591+ }
592+
593+ break ;
588594 }
589- } else if ( type == PHONGO_BSON_VECTOR_TYPE_PACKED_BIT ) {
590- bson_vector_packed_bit_const_view_t view ;
595+ case PHONGO_BSON_VECTOR_TYPE_PACKED_BIT : {
596+ bson_vector_packed_bit_const_view_t view ;
591597
592- if (!bson_vector_packed_bit_const_view_init (& view , (const uint8_t * ) intern -> data , intern -> data_len ) ||
593- !BSON_APPEND_ARRAY_FROM_VECTOR_PACKED_BIT (& tmp_doc , "vector" , view )) {
594- phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE , "Failed to convert binary vector data to an array" );
595- bson_destroy (& tmp_doc );
596- RETURN_THROWS ();
598+ if (!bson_vector_packed_bit_const_view_init (& view , (const uint8_t * ) intern -> data , intern -> data_len ) ||
599+ !BSON_APPEND_ARRAY_FROM_VECTOR_PACKED_BIT (& tmp_doc , "vector" , view )) {
600+ phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE , "Failed to convert binary vector data to an array" );
601+ bson_destroy (& tmp_doc );
602+ RETURN_THROWS ();
603+ }
604+
605+ break ;
597606 }
607+ default :
608+ phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE , "Binary vector data is invalid" );
609+ RETURN_THROWS ();
598610 }
599611
600612 bson_iter_t iter ;
@@ -647,5 +659,5 @@ static PHP_METHOD(MongoDB_BSON_Binary, toArray)
647659 RETURN_THROWS ();
648660 }
649661
650- phongo_binary_get_vector_as_array (Z_BINARY_OBJ_P ( getThis ()) , return_value );
662+ phongo_binary_get_vector_as_array (intern , return_value );
651663}
0 commit comments