2424#include "phongo_error.h"
2525#include "Binary.h"
2626#include "Binary_arginfo.h"
27+ #include "VectorType.h"
2728
2829zend_class_entry * php_phongo_binary_ce ;
2930
@@ -45,7 +46,7 @@ static bool php_phongo_binary_init(php_phongo_binary_t* intern, const char* data
4546 return false;
4647 }
4748
48- if ((type == BSON_SUBTYPE_VECTOR ) && phongo_binary_get_vector_type_from_data ((const uint8_t * ) data , data_len ) == PHONGO_BSON_VECTOR_TYPE_INVALID ) {
49+ if ((type == BSON_SUBTYPE_VECTOR ) && phongo_binary_get_vector_type_from_data ((const uint8_t * ) data , data_len ) == PHONGO_BSON_VECTOR_TYPE_UNKNOWN ) {
4950 phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Binary vector data is invalid" );
5051 return false;
5152 }
@@ -459,11 +460,11 @@ static void phongo_binary_init_vector_from_packed_bit_array(php_phongo_binary_t*
459460 ZEND_HASH_FOREACH_VAL_IND (vector , val )
460461 {
461462 if (Z_TYPE_P (val ) != IS_LONG && Z_TYPE_P (val ) != IS_TRUE && Z_TYPE_P (val ) != IS_FALSE ) {
462- phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Expected vector[%zu] to be an integer or boolean, %s given" , i , zend_zval_type_name (val ));
463+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Expected vector[%zu] to be 0, 1, or a boolean, %s given" , i , zend_zval_type_name (val ));
463464 return ;
464465 }
465466
466- if (Z_TYPE_P (val ) == IS_LONG && ( Z_LVAL_P (val ) < 0 || Z_LVAL_P (val ) > 1 ) ) {
467+ if (Z_TYPE_P (val ) == IS_LONG && Z_LVAL_P (val ) != 0 && Z_LVAL_P (val ) != 1 ) {
467468 phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Expected vector[%zu] to be 0 or 1, %" PHONGO_LONG_FORMAT " given" , i , Z_LVAL_P (val ));
468469 return ;
469470 }
@@ -484,8 +485,8 @@ static void phongo_binary_init_vector_from_packed_bit_array(php_phongo_binary_t*
484485
485486static PHP_METHOD (MongoDB_BSON_Binary , fromVector )
486487{
487- HashTable * vector ;
488- zend_object * type ;
488+ HashTable * vector ;
489+ zend_object * type ;
489490
490491 object_init_ex (return_value , php_phongo_binary_ce );
491492 php_phongo_binary_t * intern = Z_BINARY_OBJ_P (return_value );
@@ -495,25 +496,20 @@ static PHP_METHOD(MongoDB_BSON_Binary, fromVector)
495496 Z_PARAM_OBJ_OF_CLASS (type , php_phongo_vectortype_ce )
496497 PHONGO_PARSE_PARAMETERS_END ();
497498
498- zval * type_name = zend_enum_fetch_case_name (type );
499-
500- if (zend_string_equals_literal (Z_STR_P (type_name ), "Float32" )) {
501- phongo_binary_init_vector_from_float32_array (intern , vector );
502- return ;
503- }
504-
505- if (zend_string_equals_literal (Z_STR_P (type_name ), "Int8" )) {
506- phongo_binary_init_vector_from_int8_array (intern , vector );
507- return ;
508- }
509-
510- if (zend_string_equals_literal (Z_STR_P (type_name ), "PackedBit" )) {
511- phongo_binary_init_vector_from_packed_bit_array (intern , vector );
512- return ;
499+ switch (phongo_bson_vector_type_from_name (Z_STRVAL_P (zend_enum_fetch_case_name (type )))) {
500+ case PHONGO_BSON_VECTOR_TYPE_FLOAT32 :
501+ phongo_binary_init_vector_from_float32_array (intern , vector );
502+ return ;
503+ case PHONGO_BSON_VECTOR_TYPE_INT8 :
504+ phongo_binary_init_vector_from_int8_array (intern , vector );
505+ return ;
506+ case PHONGO_BSON_VECTOR_TYPE_PACKED_BIT :
507+ phongo_binary_init_vector_from_packed_bit_array (intern , vector );
508+ return ;
509+ default :
510+ phongo_throw_exception (PHONGO_ERROR_LOGIC , "Unsupported binary vector type: %s" , Z_STRVAL_P (zend_enum_fetch_case_name (type )));
511+ RETURN_THROWS ();
513512 }
514-
515- phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Unsupported binary vector type: %s" , Z_STR_P (type_name ));
516- RETURN_THROWS ();
517513}
518514
519515static phongo_bson_vector_type_t phongo_binary_get_vector_type_from_data (const uint8_t * data , uint32_t data_len )
@@ -530,7 +526,7 @@ static phongo_bson_vector_type_t phongo_binary_get_vector_type_from_data(const u
530526 return PHONGO_BSON_VECTOR_TYPE_PACKED_BIT ;
531527 }
532528
533- return PHONGO_BSON_VECTOR_TYPE_INVALID ;
529+ return PHONGO_BSON_VECTOR_TYPE_UNKNOWN ;
534530}
535531
536532static phongo_bson_vector_type_t phongo_binary_get_vector_type (const php_phongo_binary_t * intern )
@@ -549,23 +545,12 @@ static PHP_METHOD(MongoDB_BSON_Binary, getVectorType)
549545 RETURN_THROWS ();
550546 }
551547
552- phongo_bson_vector_type_t type = phongo_binary_get_vector_type (Z_BINARY_OBJ_P (getThis ()));
553- const char * type_case ;
548+ const char * type_case = phongo_bson_vector_type_to_name (phongo_binary_get_vector_type (Z_BINARY_OBJ_P (getThis ())));
554549
555- switch (type ) {
556- case PHONGO_BSON_VECTOR_TYPE_FLOAT32 :
557- type_case = "Float32" ;
558- break ;
559- case PHONGO_BSON_VECTOR_TYPE_INT8 :
560- type_case = "Int8" ;
561- break ;
562- case PHONGO_BSON_VECTOR_TYPE_PACKED_BIT :
563- type_case = "PackedBit" ;
564- break ;
565- default :
566- // The vector should always be valid by this point, but check for an error
567- phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE , "Binary vector data is invalid" );
568- RETURN_THROWS ();
550+ // The vector should always be valid by this point, but check for an error
551+ if (!type_case ) {
552+ phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE , "Binary vector data is invalid" );
553+ RETURN_THROWS ();
569554 }
570555
571556 RETVAL_OBJ_COPY (zend_enum_get_case_cstr (php_phongo_vectortype_ce , type_case ));
@@ -576,7 +561,7 @@ static void phongo_binary_get_vector_as_array(const php_phongo_binary_t* intern,
576561 phongo_bson_vector_type_t type = phongo_binary_get_vector_type (intern );
577562
578563 // The vector should always be valid by this point, but check for an error
579- if (type == PHONGO_BSON_VECTOR_TYPE_INVALID ) {
564+ if (type == PHONGO_BSON_VECTOR_TYPE_UNKNOWN ) {
580565 phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE , "Binary vector data is invalid" );
581566 RETURN_THROWS ();
582567 }
0 commit comments