16
16
17
17
#include <php.h>
18
18
#include <ext/standard/base64.h>
19
+ #include <Zend/zend_enum.h>
19
20
#include <Zend/zend_interfaces.h>
20
21
21
22
#include "php_phongo.h"
22
23
#include "phongo_bson_encode.h"
23
24
#include "phongo_error.h"
24
25
#include "Binary.h"
25
26
#include "Binary_arginfo.h"
26
- #include "BinaryVector_arginfo.h"
27
27
28
28
zend_class_entry * php_phongo_binary_ce ;
29
29
30
- static phongo_bson_vector_type_t phongo_binaryvector_get_vector_type_from_data (const uint8_t * data , uint32_t data_len );
31
- static phongo_bson_vector_type_t phongo_binaryvector_get_vector_type (const php_phongo_binary_t * intern );
32
- static void phongo_binaryvector_get_vector_as_array (const php_phongo_binary_t * intern , zval * return_value );
30
+ static phongo_bson_vector_type_t phongo_binary_get_vector_type_from_data (const uint8_t * data , uint32_t data_len );
31
+ static phongo_bson_vector_type_t phongo_binary_get_vector_type (const php_phongo_binary_t * intern );
32
+ static void phongo_binary_get_vector_as_array (const php_phongo_binary_t * intern , zval * return_value );
33
33
34
34
/* Initialize the object and return whether it was successful. An exception will
35
35
* be thrown on error. */
@@ -45,7 +45,7 @@ static bool php_phongo_binary_init(php_phongo_binary_t* intern, const char* data
45
45
return false;
46
46
}
47
47
48
- if ((type == BSON_SUBTYPE_VECTOR ) && phongo_binaryvector_get_vector_type_from_data ((const uint8_t * ) data , data_len ) == PHONGO_BSON_VECTOR_TYPE_INVALID ) {
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
49
phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Binary vector data is invalid" );
50
50
return false;
51
51
}
@@ -290,7 +290,7 @@ static HashTable* php_phongo_binary_get_debug_info(zend_object* object, int* is_
290
290
if (intern -> type == BSON_SUBTYPE_VECTOR ) {
291
291
zval vector ;
292
292
293
- phongo_binaryvector_get_vector_as_array (intern , & vector );
293
+ phongo_binary_get_vector_as_array (intern , & vector );
294
294
295
295
if (EG (exception )) {
296
296
return props ;
@@ -300,7 +300,7 @@ static HashTable* php_phongo_binary_get_debug_info(zend_object* object, int* is_
300
300
301
301
zval vector_type ;
302
302
303
- ZVAL_LONG (& vector_type , phongo_binaryvector_get_vector_type (intern ));
303
+ ZVAL_LONG (& vector_type , phongo_binary_get_vector_type (intern ));
304
304
zend_hash_str_update (props , "vectorType" , sizeof ("vectorType" ) - 1 , & vector_type );
305
305
}
306
306
@@ -328,22 +328,12 @@ void php_phongo_binary_init_ce(INIT_FUNC_ARGS)
328
328
329
329
bool phongo_binary_new (zval * object , const char * data , size_t data_len , bson_subtype_t type )
330
330
{
331
- switch (type ) {
332
- case BSON_SUBTYPE_VECTOR :
333
- object_init_ex (object , php_phongo_binaryvector_ce );
334
- break ;
335
-
336
- default :
337
- object_init_ex (object , php_phongo_binary_ce );
338
- }
331
+ object_init_ex (object , php_phongo_binary_ce );
339
332
340
333
return php_phongo_binary_init (Z_BINARY_OBJ_P (object ), data , data_len , type );
341
334
}
342
335
343
- /* MongoDB\BSON\BinaryVector implementation */
344
- zend_class_entry * php_phongo_binaryvector_ce ;
345
-
346
- static inline void phongo_binaryvector_init_from_bson_key (php_phongo_binary_t * intern , const bson_t * doc , const char * key )
336
+ static inline void phongo_binary_init_vector_from_bson_key (php_phongo_binary_t * intern , const bson_t * doc , const char * key )
347
337
{
348
338
bson_iter_t iter ;
349
339
@@ -359,7 +349,7 @@ static inline void phongo_binaryvector_init_from_bson_key(php_phongo_binary_t* i
359
349
php_phongo_binary_init (intern , (const char * ) data , data_len , BSON_SUBTYPE_VECTOR );
360
350
}
361
351
362
- static void phongo_binaryvector_init_from_float32_array (php_phongo_binary_t * intern , HashTable * vector )
352
+ static void phongo_binary_init_vector_from_float32_array (php_phongo_binary_t * intern , HashTable * vector )
363
353
{
364
354
if (!zend_array_is_list (vector )) {
365
355
phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Expected vector to be a list" );
@@ -397,10 +387,10 @@ static void phongo_binaryvector_init_from_float32_array(php_phongo_binary_t* int
397
387
}
398
388
ZEND_HASH_FOREACH_END ();
399
389
400
- phongo_binaryvector_init_from_bson_key (intern , & doc , "vector" );
390
+ phongo_binary_init_vector_from_bson_key (intern , & doc , "vector" );
401
391
}
402
392
403
- static void phongo_binaryvector_init_from_int8_array (php_phongo_binary_t * intern , HashTable * vector )
393
+ static void phongo_binary_init_vector_from_int8_array (php_phongo_binary_t * intern , HashTable * vector )
404
394
{
405
395
if (!zend_array_is_list (vector )) {
406
396
phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Expected vector to be a list" );
@@ -443,10 +433,10 @@ static void phongo_binaryvector_init_from_int8_array(php_phongo_binary_t* intern
443
433
}
444
434
ZEND_HASH_FOREACH_END ();
445
435
446
- phongo_binaryvector_init_from_bson_key (intern , & doc , "vector" );
436
+ phongo_binary_init_vector_from_bson_key (intern , & doc , "vector" );
447
437
}
448
438
449
- static void phongo_binaryvector_init_from_packed_bit_array (php_phongo_binary_t * intern , HashTable * vector )
439
+ static void phongo_binary_init_vector_from_packed_bit_array (php_phongo_binary_t * intern , HashTable * vector )
450
440
{
451
441
if (!zend_array_is_list (vector )) {
452
442
phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Expected vector to be a list" );
@@ -489,84 +479,44 @@ static void phongo_binaryvector_init_from_packed_bit_array(php_phongo_binary_t*
489
479
}
490
480
ZEND_HASH_FOREACH_END ();
491
481
492
- phongo_binaryvector_init_from_bson_key (intern , & doc , "vector" );
482
+ phongo_binary_init_vector_from_bson_key (intern , & doc , "vector" );
493
483
}
494
484
495
- static PHP_METHOD (MongoDB_BSON_BinaryVector , __construct )
485
+ static PHP_METHOD (MongoDB_BSON_Binary , fromVector )
496
486
{
497
- php_phongo_binary_t * intern ;
498
487
HashTable * vector ;
499
- zend_long type = BSON_SUBTYPE_BINARY ;
488
+ zend_object * type ;
500
489
501
- intern = Z_BINARY_OBJ_P (getThis ());
490
+ object_init_ex (return_value , php_phongo_binary_ce );
491
+ php_phongo_binary_t * intern = Z_BINARY_OBJ_P (return_value );
502
492
503
493
PHONGO_PARSE_PARAMETERS_START (2 , 2 )
504
494
Z_PARAM_ARRAY_HT (vector )
505
- Z_PARAM_LONG (type )
495
+ Z_PARAM_OBJ_OF_CLASS (type , php_phongo_vectortype_ce )
506
496
PHONGO_PARSE_PARAMETERS_END ();
507
497
508
- switch (type ) {
509
- case PHONGO_BSON_VECTOR_TYPE_FLOAT32 :
510
- phongo_binaryvector_init_from_float32_array (intern , vector );
511
- break ;
512
-
513
- case PHONGO_BSON_VECTOR_TYPE_INT8 :
514
- phongo_binaryvector_init_from_int8_array (intern , vector );
515
- break ;
516
-
517
- case PHONGO_BSON_VECTOR_TYPE_PACKED_BIT :
518
- phongo_binaryvector_init_from_packed_bit_array (intern , vector );
519
- break ;
498
+ zval * type_name = zend_enum_fetch_case_name (type );
520
499
521
- default :
522
- phongo_throw_exception ( PHONGO_ERROR_INVALID_ARGUMENT , "Unsupported binary vector type: %" PHONGO_LONG_FORMAT , type );
523
- RETURN_THROWS () ;
500
+ if ( zend_string_equals_literal ( Z_STR_P ( type_name ), "Float32" )) {
501
+ phongo_binary_init_vector_from_float32_array ( intern , vector );
502
+ return ;
524
503
}
525
- }
526
504
527
- static PHP_METHOD (MongoDB_BSON_BinaryVector , fromFloat32Array )
528
- {
529
- HashTable * vector ;
530
-
531
- PHONGO_PARSE_PARAMETERS_START (1 , 1 )
532
- Z_PARAM_ARRAY_HT (vector )
533
- PHONGO_PARSE_PARAMETERS_END ();
534
-
535
- object_init_ex (return_value , php_phongo_binaryvector_ce );
536
- php_phongo_binary_t * intern = Z_BINARY_OBJ_P (return_value );
537
-
538
- phongo_binaryvector_init_from_float32_array (intern , vector );
539
- }
540
-
541
- static PHP_METHOD (MongoDB_BSON_BinaryVector , fromInt8Array )
542
- {
543
- HashTable * vector ;
544
-
545
- PHONGO_PARSE_PARAMETERS_START (1 , 1 )
546
- Z_PARAM_ARRAY_HT (vector )
547
- PHONGO_PARSE_PARAMETERS_END ();
548
-
549
- object_init_ex (return_value , php_phongo_binaryvector_ce );
550
- php_phongo_binary_t * intern = Z_BINARY_OBJ_P (return_value );
551
-
552
- phongo_binaryvector_init_from_int8_array (intern , vector );
553
- }
554
-
555
- static PHP_METHOD (MongoDB_BSON_BinaryVector , fromPackedBitArray )
556
- {
557
- HashTable * vector ;
558
-
559
- PHONGO_PARSE_PARAMETERS_START (1 , 1 )
560
- Z_PARAM_ARRAY_HT (vector )
561
- PHONGO_PARSE_PARAMETERS_END ();
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
+ }
562
509
563
- object_init_ex (return_value , php_phongo_binaryvector_ce );
564
- php_phongo_binary_t * intern = Z_BINARY_OBJ_P (return_value );
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 ;
513
+ }
565
514
566
- phongo_binaryvector_init_from_packed_bit_array (intern , vector );
515
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Unsupported binary vector type: %s" , Z_STR_P (type_name ));
516
+ RETURN_THROWS ();
567
517
}
568
518
569
- static phongo_bson_vector_type_t phongo_binaryvector_get_vector_type_from_data (const uint8_t * data , uint32_t data_len )
519
+ static phongo_bson_vector_type_t phongo_binary_get_vector_type_from_data (const uint8_t * data , uint32_t data_len )
570
520
{
571
521
if (bson_vector_int8_const_view_init (NULL , data , data_len )) {
572
522
return PHONGO_BSON_VECTOR_TYPE_INT8 ;
@@ -583,29 +533,47 @@ static phongo_bson_vector_type_t phongo_binaryvector_get_vector_type_from_data(c
583
533
return PHONGO_BSON_VECTOR_TYPE_INVALID ;
584
534
}
585
535
586
- static phongo_bson_vector_type_t phongo_binaryvector_get_vector_type (const php_phongo_binary_t * intern )
536
+ static phongo_bson_vector_type_t phongo_binary_get_vector_type (const php_phongo_binary_t * intern )
587
537
{
588
- return phongo_binaryvector_get_vector_type_from_data ((const uint8_t * ) intern -> data , intern -> data_len );
538
+ return phongo_binary_get_vector_type_from_data ((const uint8_t * ) intern -> data , intern -> data_len );
589
539
}
590
540
591
- static PHP_METHOD (MongoDB_BSON_BinaryVector , getVectorType )
541
+ static PHP_METHOD (MongoDB_BSON_Binary , getVectorType )
592
542
{
593
543
PHONGO_PARSE_PARAMETERS_NONE ();
594
544
595
- phongo_bson_vector_type_t type = phongo_binaryvector_get_vector_type ( Z_BINARY_OBJ_P (getThis () ));
545
+ php_phongo_binary_t * intern = Z_BINARY_OBJ_P (getThis ());
596
546
597
- // The vector should always be valid by this point, but check for an error
598
- if (type == PHONGO_BSON_VECTOR_TYPE_INVALID ) {
599
- phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE , "Binary vector data is invalid" );
547
+ if (intern -> type != BSON_SUBTYPE_VECTOR ) {
548
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Expected Binary of type vector (%" PRId8 ") but it is %" PHONGO_LONG_FORMAT , BSON_SUBTYPE_VECTOR , intern -> type );
600
549
RETURN_THROWS ();
601
550
}
602
551
603
- RETURN_LONG (type );
552
+ phongo_bson_vector_type_t type = phongo_binary_get_vector_type (Z_BINARY_OBJ_P (getThis ()));
553
+ const char * type_case ;
554
+
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 ();
569
+ }
570
+
571
+ RETVAL_OBJ_COPY (zend_enum_get_case_cstr (php_phongo_vectortype_ce , type_case ));
604
572
}
605
573
606
- static void phongo_binaryvector_get_vector_as_array (const php_phongo_binary_t * intern , zval * return_value )
574
+ static void phongo_binary_get_vector_as_array (const php_phongo_binary_t * intern , zval * return_value )
607
575
{
608
- phongo_bson_vector_type_t type = phongo_binaryvector_get_vector_type (intern );
576
+ phongo_bson_vector_type_t type = phongo_binary_get_vector_type (intern );
609
577
610
578
// The vector should always be valid by this point, but check for an error
611
579
if (type == PHONGO_BSON_VECTOR_TYPE_INVALID ) {
@@ -683,57 +651,16 @@ static void phongo_binaryvector_get_vector_as_array(const php_phongo_binary_t* i
683
651
RETURN_ZVAL (& state .zchild , 0 , 1 );
684
652
}
685
653
686
- static PHP_METHOD (MongoDB_BSON_BinaryVector , toArray )
654
+ static PHP_METHOD (MongoDB_BSON_Binary , toArray )
687
655
{
688
656
PHONGO_PARSE_PARAMETERS_NONE ();
689
657
690
- phongo_binaryvector_get_vector_as_array (Z_BINARY_OBJ_P (getThis ()), return_value );
691
- }
692
-
693
- static PHP_METHOD (MongoDB_BSON_BinaryVector , __set_state )
694
- {
695
- php_phongo_binary_t * intern ;
696
- HashTable * props ;
697
- zval * array ;
698
-
699
- PHONGO_PARSE_PARAMETERS_START (1 , 1 )
700
- Z_PARAM_ARRAY (array )
701
- PHONGO_PARSE_PARAMETERS_END ();
702
-
703
- /* This implementation is similar to Binary::__set_state(), except that we
704
- * initialize a BinaryVector object and also validate its data. */
705
- object_init_ex (return_value , php_phongo_binaryvector_ce );
658
+ php_phongo_binary_t * intern = Z_BINARY_OBJ_P (getThis ());
706
659
707
- intern = Z_BINARY_OBJ_P (return_value );
708
- props = Z_ARRVAL_P (array );
709
-
710
- php_phongo_binary_init_from_hash (intern , props );
711
-
712
- if (intern -> type != BSON_SUBTYPE_VECTOR || phongo_binaryvector_get_vector_type (intern ) == PHONGO_BSON_VECTOR_TYPE_INVALID ) {
713
- phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Binary vector data is invalid" );
660
+ if (intern -> type != BSON_SUBTYPE_VECTOR ) {
661
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Expected Binary of type vector (%" PRId8 ") but it is %" PHONGO_LONG_FORMAT , BSON_SUBTYPE_VECTOR , intern -> type );
714
662
RETURN_THROWS ();
715
663
}
716
- }
717
664
718
- /* MongoDB\BSON\BinaryVector object handlers */
719
- static zend_object_handlers php_phongo_handler_binaryvector ;
720
-
721
- static zend_object * php_phongo_binaryvector_create_object (zend_class_entry * class_type )
722
- {
723
- php_phongo_binary_t * intern = zend_object_alloc (sizeof (php_phongo_binary_t ), class_type );
724
-
725
- zend_object_std_init (& intern -> std , class_type );
726
- object_properties_init (& intern -> std , class_type );
727
-
728
- intern -> std .handlers = & php_phongo_handler_binaryvector ;
729
-
730
- return & intern -> std ;
665
+ phongo_binary_get_vector_as_array (Z_BINARY_OBJ_P (getThis ()), return_value );
731
666
}
732
-
733
- void php_phongo_binaryvector_init_ce (INIT_FUNC_ARGS )
734
- {
735
- php_phongo_binaryvector_ce = register_class_MongoDB_BSON_BinaryVector (php_phongo_binary_ce );
736
- php_phongo_binaryvector_ce -> create_object = php_phongo_binaryvector_create_object ;
737
-
738
- memcpy (& php_phongo_handler_binaryvector , & php_phongo_handler_binary , sizeof (zend_object_handlers ));
739
- }
0 commit comments