@@ -40,7 +40,7 @@ zend_class_entry* php_phongo_readpreference_ce;
40
40
* An exception will be thrown on error. */
41
41
static bool php_phongo_readpreference_init_from_hash (php_phongo_readpreference_t * intern , HashTable * props ) /* {{{ */
42
42
{
43
- zval * mode , * tagSets , * maxStalenessSeconds ;
43
+ zval * mode , * tagSets , * maxStalenessSeconds , * hedge ;
44
44
45
45
if ((mode = zend_hash_str_find (props , "mode" , sizeof ("mode" ) - 1 )) && Z_TYPE_P (mode ) == IS_STRING ) {
46
46
if (strcasecmp (Z_STRVAL_P (mode ), PHONGO_READ_PRIMARY ) == 0 ) {
@@ -111,6 +111,31 @@ static bool php_phongo_readpreference_init_from_hash(php_phongo_readpreference_t
111
111
}
112
112
}
113
113
114
+ if ((hedge = zend_hash_str_find (props , "hedge" , sizeof ("hedge" ) - 1 ))) {
115
+ if (Z_TYPE_P (hedge ) == IS_ARRAY || Z_TYPE_P (hedge ) == IS_OBJECT ) {
116
+ bson_t * hedge_doc = bson_new ();
117
+
118
+ if (mongoc_read_prefs_get_mode (intern -> read_preference ) == MONGOC_READ_PRIMARY ) {
119
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "%s initialization requires \"hedge\" field to not be present with \"primary\" mode" , ZSTR_VAL (php_phongo_readpreference_ce -> name ));
120
+ bson_destroy (hedge_doc );
121
+ goto failure ;
122
+ }
123
+
124
+ php_phongo_zval_to_bson (hedge , PHONGO_BSON_NONE , hedge_doc , NULL );
125
+
126
+ if (EG (exception )) {
127
+ bson_destroy (hedge_doc );
128
+ goto failure ;
129
+ }
130
+
131
+ mongoc_read_prefs_set_hedge (intern -> read_preference , hedge_doc );
132
+ bson_destroy (hedge_doc );
133
+ } else {
134
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "%s initialization requires \"hedge\" field to be an array or object" , ZSTR_VAL (php_phongo_readpreference_ce -> name ));
135
+ goto failure ;
136
+ }
137
+ }
138
+
114
139
return true;
115
140
116
141
failure :
@@ -238,6 +263,33 @@ static PHP_METHOD(ReadPreference, __construct)
238
263
mongoc_read_prefs_set_max_staleness_seconds (intern -> read_preference , maxStalenessSeconds );
239
264
}
240
265
266
+ if (options && php_array_exists (options , "hedge" )) {
267
+ zval * hedge = php_array_fetchc (options , "hedge" );
268
+
269
+ if (Z_TYPE_P (hedge ) == IS_ARRAY || Z_TYPE_P (hedge ) == IS_OBJECT ) {
270
+ bson_t * hedge_doc = bson_new ();
271
+
272
+ if (mongoc_read_prefs_get_mode (intern -> read_preference ) == MONGOC_READ_PRIMARY ) {
273
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "hedge may not be used with primary mode" );
274
+ bson_destroy (hedge_doc );
275
+ return ;
276
+ }
277
+
278
+ php_phongo_zval_to_bson (hedge , PHONGO_BSON_NONE , hedge_doc , NULL );
279
+
280
+ if (EG (exception )) {
281
+ bson_destroy (hedge_doc );
282
+ return ;
283
+ }
284
+
285
+ mongoc_read_prefs_set_hedge (intern -> read_preference , hedge_doc );
286
+ bson_destroy (hedge_doc );
287
+ } else {
288
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "%s initialization requires \"hedge\" field to be an array or object" , ZSTR_VAL (php_phongo_readpreference_ce -> name ));
289
+ return ;
290
+ }
291
+ }
292
+
241
293
if (!mongoc_read_prefs_is_valid (intern -> read_preference )) {
242
294
phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "Read preference is not valid" );
243
295
return ;
@@ -264,6 +316,37 @@ static PHP_METHOD(ReadPreference, __set_state)
264
316
php_phongo_readpreference_init_from_hash (intern , props );
265
317
} /* }}} */
266
318
319
+ /* {{{ proto array|null MongoDB\Driver\ReadPreference::getHedge()
320
+ Returns the ReadPreference hedge document */
321
+ static PHP_METHOD (ReadPreference , getHedge )
322
+ {
323
+ php_phongo_readpreference_t * intern ;
324
+ const bson_t * hedge ;
325
+
326
+ intern = Z_READPREFERENCE_OBJ_P (getThis ());
327
+
328
+ if (zend_parse_parameters_none () == FAILURE ) {
329
+ return ;
330
+ }
331
+
332
+ hedge = mongoc_read_prefs_get_hedge (intern -> read_preference );
333
+
334
+ if (!bson_empty0 (hedge )) {
335
+ php_phongo_bson_state state ;
336
+
337
+ PHONGO_BSON_INIT_STATE (state );
338
+
339
+ if (!php_phongo_bson_to_zval_ex (bson_get_data (hedge ), hedge -> len , & state )) {
340
+ zval_ptr_dtor (& state .zchild );
341
+ return ;
342
+ }
343
+
344
+ RETURN_ZVAL (& state .zchild , 0 , 1 );
345
+ } else {
346
+ RETURN_NULL ();
347
+ }
348
+ } /* }}} */
349
+
267
350
/* {{{ proto integer MongoDB\Driver\ReadPreference::getMaxStalenessSeconds()
268
351
Returns the ReadPreference maxStalenessSeconds value */
269
352
static PHP_METHOD (ReadPreference , getMaxStalenessSeconds )
@@ -353,11 +436,12 @@ static HashTable* php_phongo_readpreference_get_properties_hash(zval* object, bo
353
436
HashTable * props ;
354
437
const char * modeString = NULL ;
355
438
const bson_t * tags ;
439
+ const bson_t * hedge ;
356
440
mongoc_read_mode_t mode ;
357
441
358
442
intern = Z_READPREFERENCE_OBJ_P (object );
359
443
360
- PHONGO_GET_PROPERTY_HASH_INIT_PROPS (is_debug , intern , props , 3 );
444
+ PHONGO_GET_PROPERTY_HASH_INIT_PROPS (is_debug , intern , props , 4 );
361
445
362
446
if (!intern -> read_preference ) {
363
447
return props ;
@@ -366,6 +450,7 @@ static HashTable* php_phongo_readpreference_get_properties_hash(zval* object, bo
366
450
tags = mongoc_read_prefs_get_tags (intern -> read_preference );
367
451
mode = mongoc_read_prefs_get_mode (intern -> read_preference );
368
452
modeString = php_phongo_readpreference_get_mode_string (mode );
453
+ hedge = mongoc_read_prefs_get_hedge (intern -> read_preference );
369
454
370
455
if (modeString ) {
371
456
zval z_mode ;
@@ -398,6 +483,19 @@ static HashTable* php_phongo_readpreference_get_properties_hash(zval* object, bo
398
483
zend_hash_str_update (props , "maxStalenessSeconds" , sizeof ("maxStalenessSeconds" ) - 1 , & z_max_ss );
399
484
}
400
485
486
+ if (!bson_empty0 (hedge )) {
487
+ php_phongo_bson_state state ;
488
+
489
+ PHONGO_BSON_INIT_STATE (state );
490
+
491
+ if (!php_phongo_bson_to_zval_ex (bson_get_data (hedge ), hedge -> len , & state )) {
492
+ zval_ptr_dtor (& state .zchild );
493
+ goto done ;
494
+ }
495
+
496
+ zend_hash_str_update (props , "hedge" , sizeof ("hedge" ) - 1 , & state .zchild );
497
+ }
498
+
401
499
done :
402
500
return props ;
403
501
} /* }}} */
@@ -424,6 +522,7 @@ static PHP_METHOD(ReadPreference, serialize)
424
522
smart_str buf = { 0 };
425
523
const char * modeString = NULL ;
426
524
const bson_t * tags ;
525
+ const bson_t * hedge ;
427
526
int64_t maxStalenessSeconds ;
428
527
mongoc_read_mode_t mode ;
429
528
@@ -441,8 +540,9 @@ static PHP_METHOD(ReadPreference, serialize)
441
540
mode = mongoc_read_prefs_get_mode (intern -> read_preference );
442
541
modeString = php_phongo_readpreference_get_mode_string (mode );
443
542
maxStalenessSeconds = mongoc_read_prefs_get_max_staleness_seconds (intern -> read_preference );
543
+ hedge = mongoc_read_prefs_get_hedge (intern -> read_preference );
444
544
445
- array_init_size (& retval , 3 );
545
+ array_init_size (& retval , 4 );
446
546
447
547
if (modeString ) {
448
548
ADD_ASSOC_STRING (& retval , "mode" , modeString );
@@ -465,6 +565,19 @@ static PHP_METHOD(ReadPreference, serialize)
465
565
ADD_ASSOC_LONG_EX (& retval , "maxStalenessSeconds" , maxStalenessSeconds );
466
566
}
467
567
568
+ if (!bson_empty0 (hedge )) {
569
+ php_phongo_bson_state state ;
570
+
571
+ PHONGO_BSON_INIT_STATE (state );
572
+
573
+ if (!php_phongo_bson_to_zval_ex (bson_get_data (hedge ), hedge -> len , & state )) {
574
+ zval_ptr_dtor (& state .zchild );
575
+ return ;
576
+ }
577
+
578
+ ADD_ASSOC_ZVAL_EX (& retval , "hedge" , & state .zchild );
579
+ }
580
+
468
581
PHP_VAR_SERIALIZE_INIT (var_hash );
469
582
php_var_serialize (& buf , & retval , & var_hash );
470
583
smart_str_0 (& buf );
@@ -537,6 +650,7 @@ static zend_function_entry php_phongo_readpreference_me[] = {
537
650
/* clang-format off */
538
651
PHP_ME (ReadPreference , __construct , ai_ReadPreference___construct , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
539
652
PHP_ME (ReadPreference , __set_state , ai_ReadPreference___set_state , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC )
653
+ PHP_ME (ReadPreference , getHedge , ai_ReadPreference_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
540
654
PHP_ME (ReadPreference , getMaxStalenessSeconds , ai_ReadPreference_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
541
655
PHP_ME (ReadPreference , getMode , ai_ReadPreference_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
542
656
PHP_ME (ReadPreference , getModeString , ai_ReadPreference_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
0 commit comments