24
24
#include "phongo_error.h"
25
25
#include "Binary.h"
26
26
#include "Binary_arginfo.h"
27
+ #include "VectorType.h"
27
28
28
29
zend_class_entry * php_phongo_binary_ce ;
29
30
@@ -45,7 +46,7 @@ static bool php_phongo_binary_init(php_phongo_binary_t* intern, const char* data
45
46
return false;
46
47
}
47
48
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 ) {
49
50
phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Binary vector data is invalid" );
50
51
return false;
51
52
}
@@ -459,11 +460,11 @@ static void phongo_binary_init_vector_from_packed_bit_array(php_phongo_binary_t*
459
460
ZEND_HASH_FOREACH_VAL_IND (vector , val )
460
461
{
461
462
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 ));
463
464
return ;
464
465
}
465
466
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 ) {
467
468
phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Expected vector[%zu] to be 0 or 1, %" PHONGO_LONG_FORMAT " given" , i , Z_LVAL_P (val ));
468
469
return ;
469
470
}
@@ -484,8 +485,8 @@ static void phongo_binary_init_vector_from_packed_bit_array(php_phongo_binary_t*
484
485
485
486
static PHP_METHOD (MongoDB_BSON_Binary , fromVector )
486
487
{
487
- HashTable * vector ;
488
- zend_object * type ;
488
+ HashTable * vector ;
489
+ zend_object * type ;
489
490
490
491
object_init_ex (return_value , php_phongo_binary_ce );
491
492
php_phongo_binary_t * intern = Z_BINARY_OBJ_P (return_value );
@@ -495,25 +496,20 @@ static PHP_METHOD(MongoDB_BSON_Binary, fromVector)
495
496
Z_PARAM_OBJ_OF_CLASS (type , php_phongo_vectortype_ce )
496
497
PHONGO_PARSE_PARAMETERS_END ();
497
498
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 ();
513
512
}
514
-
515
- phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Unsupported binary vector type: %s" , Z_STR_P (type_name ));
516
- RETURN_THROWS ();
517
513
}
518
514
519
515
static 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
530
526
return PHONGO_BSON_VECTOR_TYPE_PACKED_BIT ;
531
527
}
532
528
533
- return PHONGO_BSON_VECTOR_TYPE_INVALID ;
529
+ return PHONGO_BSON_VECTOR_TYPE_UNKNOWN ;
534
530
}
535
531
536
532
static 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)
549
545
RETURN_THROWS ();
550
546
}
551
547
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 ())));
554
549
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 ();
569
554
}
570
555
571
556
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,
576
561
phongo_bson_vector_type_t type = phongo_binary_get_vector_type (intern );
577
562
578
563
// 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 ) {
580
565
phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE , "Binary vector data is invalid" );
581
566
RETURN_THROWS ();
582
567
}
0 commit comments