16
16
17
17
#include <php.h>
18
18
#include <Zend/zend_interfaces.h>
19
+ #include <ext/standard/php_var.h>
20
+ #if PHP_VERSION_ID >= 70000
21
+ #include <zend_smart_str.h>
22
+ #else
23
+ #include <ext/standard/php_smart_str.h>
24
+ #endif
19
25
20
26
#ifdef HAVE_CONFIG_H
21
27
#include "config.h"
@@ -369,6 +375,26 @@ static PHP_METHOD(ReadPreference, getTagSets)
369
375
}
370
376
} /* }}} */
371
377
378
+ static const char * php_phongo_readpreference_get_mode_string (mongoc_read_mode_t mode ) /* {{{ */
379
+ {
380
+ switch (mode ) {
381
+ case MONGOC_READ_PRIMARY :
382
+ return "primary" ;
383
+ case MONGOC_READ_PRIMARY_PREFERRED :
384
+ return "primaryPreferred" ;
385
+ case MONGOC_READ_SECONDARY :
386
+ return "secondary" ;
387
+ case MONGOC_READ_SECONDARY_PREFERRED :
388
+ return "secondaryPreferred" ;
389
+ case MONGOC_READ_NEAREST :
390
+ return "nearest" ;
391
+ default : /* Do nothing */
392
+ break ;
393
+ }
394
+
395
+ return NULL ;
396
+ } /* }}} */
397
+
372
398
static HashTable * php_phongo_readpreference_get_properties_hash (zval * object , bool is_debug TSRMLS_DC ) /* {{{ */
373
399
{
374
400
php_phongo_readpreference_t * intern ;
@@ -387,26 +413,7 @@ static HashTable* php_phongo_readpreference_get_properties_hash(zval* object, bo
387
413
388
414
tags = mongoc_read_prefs_get_tags (intern -> read_preference );
389
415
mode = mongoc_read_prefs_get_mode (intern -> read_preference );
390
-
391
- switch (mode ) {
392
- case MONGOC_READ_PRIMARY :
393
- modeString = "primary" ;
394
- break ;
395
- case MONGOC_READ_PRIMARY_PREFERRED :
396
- modeString = "primaryPreferred" ;
397
- break ;
398
- case MONGOC_READ_SECONDARY :
399
- modeString = "secondary" ;
400
- break ;
401
- case MONGOC_READ_SECONDARY_PREFERRED :
402
- modeString = "secondaryPreferred" ;
403
- break ;
404
- case MONGOC_READ_NEAREST :
405
- modeString = "nearest" ;
406
- break ;
407
- default : /* Do nothing */
408
- break ;
409
- }
416
+ modeString = php_phongo_readpreference_get_mode_string (mode );
410
417
411
418
if (modeString ) {
412
419
#if PHP_VERSION_ID >= 70000
@@ -471,6 +478,132 @@ static PHP_METHOD(ReadPreference, bsonSerialize)
471
478
convert_to_object (return_value );
472
479
} /* }}} */
473
480
481
+ /* {{{ proto string MongoDB\Driver\ReadPreference::serialize()
482
+ */
483
+ static PHP_METHOD (ReadPreference , serialize )
484
+ {
485
+ php_phongo_readpreference_t * intern ;
486
+ ZVAL_RETVAL_TYPE retval ;
487
+ php_serialize_data_t var_hash ;
488
+ smart_str buf = { 0 };
489
+ const char * modeString = NULL ;
490
+ const bson_t * tags ;
491
+ int64_t maxStalenessSeconds ;
492
+ mongoc_read_mode_t mode ;
493
+
494
+ intern = Z_READPREFERENCE_OBJ_P (getThis ());
495
+
496
+ if (zend_parse_parameters_none () == FAILURE ) {
497
+ return ;
498
+ }
499
+
500
+ if (!intern -> read_preference ) {
501
+ return ;
502
+ }
503
+
504
+ tags = mongoc_read_prefs_get_tags (intern -> read_preference );
505
+ mode = mongoc_read_prefs_get_mode (intern -> read_preference );
506
+ modeString = php_phongo_readpreference_get_mode_string (mode );
507
+ maxStalenessSeconds = mongoc_read_prefs_get_max_staleness_seconds (intern -> read_preference );
508
+
509
+ #if PHP_VERSION_ID >= 70000
510
+ array_init_size (& retval , 3 );
511
+ #else
512
+ ALLOC_INIT_ZVAL (retval );
513
+ array_init_size (retval , 3 );
514
+ #endif
515
+
516
+ if (modeString ) {
517
+ #if PHP_VERSION_ID >= 70000
518
+ ADD_ASSOC_STRING (& retval , "mode" , modeString );
519
+ #else
520
+ ADD_ASSOC_STRING (retval , "mode" , modeString );
521
+ #endif
522
+ }
523
+
524
+ if (!bson_empty0 (tags )) {
525
+ php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER ;
526
+ /* Use native arrays for debugging output */
527
+ state .map .root_type = PHONGO_TYPEMAP_NATIVE_ARRAY ;
528
+ state .map .document_type = PHONGO_TYPEMAP_NATIVE_ARRAY ;
529
+
530
+ php_phongo_bson_to_zval_ex (bson_get_data (tags ), tags -> len , & state );
531
+ #if PHP_VERSION_ID >= 70000
532
+ ADD_ASSOC_ZVAL_EX (& retval , "tags" , & state .zchild );
533
+ #else
534
+ ADD_ASSOC_ZVAL_EX (retval , "tags" , state .zchild );
535
+ #endif
536
+ }
537
+
538
+ if (maxStalenessSeconds != MONGOC_NO_MAX_STALENESS ) {
539
+ #if PHP_VERSION_ID >= 70000
540
+ ADD_ASSOC_LONG_EX (& retval , "maxStalenessSeconds" , maxStalenessSeconds );
541
+ #else
542
+ ADD_ASSOC_LONG_EX (retval , "maxStalenessSeconds" , maxStalenessSeconds );
543
+ #endif
544
+ }
545
+
546
+ PHP_VAR_SERIALIZE_INIT (var_hash );
547
+ php_var_serialize (& buf , & retval , & var_hash TSRMLS_CC );
548
+ smart_str_0 (& buf );
549
+ PHP_VAR_SERIALIZE_DESTROY (var_hash );
550
+
551
+ PHONGO_RETVAL_SMART_STR (buf );
552
+
553
+ smart_str_free (& buf );
554
+ zval_ptr_dtor (& retval );
555
+ } /* }}} */
556
+
557
+ /* {{{ proto void MongoDB\Driver\ReadPreference::unserialize(string $serialized)
558
+ */
559
+ static PHP_METHOD (ReadPreference , unserialize )
560
+ {
561
+ php_phongo_readpreference_t * intern ;
562
+ zend_error_handling error_handling ;
563
+ char * serialized ;
564
+ phongo_zpp_char_len serialized_len ;
565
+ #if PHP_VERSION_ID >= 70000
566
+ zval props ;
567
+ #else
568
+ zval * props ;
569
+ #endif
570
+ php_unserialize_data_t var_hash ;
571
+
572
+ intern = Z_READPREFERENCE_OBJ_P (getThis ());
573
+
574
+ zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
575
+
576
+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "s" , & serialized , & serialized_len ) == FAILURE ) {
577
+ zend_restore_error_handling (& error_handling TSRMLS_CC );
578
+ return ;
579
+ }
580
+ zend_restore_error_handling (& error_handling TSRMLS_CC );
581
+
582
+ if (!serialized_len ) {
583
+ return ;
584
+ }
585
+
586
+ #if PHP_VERSION_ID < 70000
587
+ ALLOC_INIT_ZVAL (props );
588
+ #endif
589
+ PHP_VAR_UNSERIALIZE_INIT (var_hash );
590
+ if (!php_var_unserialize (& props , (const unsigned char * * ) & serialized , (unsigned char * ) serialized + serialized_len , & var_hash TSRMLS_CC )) {
591
+ zval_ptr_dtor (& props );
592
+ phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC , "%s unserialization failed" , ZSTR_VAL (php_phongo_readpreference_ce -> name ));
593
+
594
+ PHP_VAR_UNSERIALIZE_DESTROY (var_hash );
595
+ return ;
596
+ }
597
+ PHP_VAR_UNSERIALIZE_DESTROY (var_hash );
598
+
599
+ #if PHP_VERSION_ID >= 70000
600
+ php_phongo_readpreference_init_from_hash (intern , HASH_OF (& props ) TSRMLS_CC );
601
+ #else
602
+ php_phongo_readpreference_init_from_hash (intern , HASH_OF (props ) TSRMLS_CC );
603
+ #endif
604
+ zval_ptr_dtor (& props );
605
+ } /* }}} */
606
+
474
607
/* {{{ MongoDB\Driver\ReadPreference function entries */
475
608
ZEND_BEGIN_ARG_INFO_EX (ai_ReadPreference___construct , 0 , 0 , 1 )
476
609
ZEND_ARG_INFO (0 , mode )
@@ -482,6 +615,10 @@ ZEND_BEGIN_ARG_INFO_EX(ai_ReadPreference___set_state, 0, 0, 1)
482
615
ZEND_ARG_ARRAY_INFO (0 , properties , 0 )
483
616
ZEND_END_ARG_INFO ()
484
617
618
+ ZEND_BEGIN_ARG_INFO_EX (ai_ReadPreference_unserialize , 0 , 0 , 1 )
619
+ ZEND_ARG_INFO (0 , serialized )
620
+ ZEND_END_ARG_INFO ()
621
+
485
622
ZEND_BEGIN_ARG_INFO_EX (ai_ReadPreference_void , 0 , 0 , 0 )
486
623
ZEND_END_ARG_INFO ()
487
624
@@ -493,6 +630,8 @@ static zend_function_entry php_phongo_readpreference_me[] = {
493
630
PHP_ME (ReadPreference , getMode , ai_ReadPreference_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
494
631
PHP_ME (ReadPreference , getTagSets , ai_ReadPreference_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
495
632
PHP_ME (ReadPreference , bsonSerialize , ai_ReadPreference_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
633
+ PHP_ME (ReadPreference , serialize , ai_ReadPreference_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
634
+ PHP_ME (ReadPreference , unserialize , ai_ReadPreference_unserialize , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
496
635
PHP_FE_END
497
636
/* clang-format on */
498
637
};
@@ -565,9 +704,9 @@ void php_phongo_readpreference_init_ce(INIT_FUNC_ARGS) /* {{{ */
565
704
php_phongo_readpreference_ce = zend_register_internal_class (& ce TSRMLS_CC );
566
705
php_phongo_readpreference_ce -> create_object = php_phongo_readpreference_create_object ;
567
706
PHONGO_CE_FINAL (php_phongo_readpreference_ce );
568
- PHONGO_CE_DISABLE_SERIALIZATION (php_phongo_readpreference_ce );
569
707
570
708
zend_class_implements (php_phongo_readpreference_ce TSRMLS_CC , 1 , php_phongo_serializable_ce );
709
+ zend_class_implements (php_phongo_readpreference_ce TSRMLS_CC , 1 , zend_ce_serializable );
571
710
572
711
memcpy (& php_phongo_handler_readpreference , phongo_get_std_object_handlers (), sizeof (zend_object_handlers ));
573
712
php_phongo_handler_readpreference .get_debug_info = php_phongo_readpreference_get_debug_info ;
0 commit comments