34
34
35
35
zend_class_entry * php_phongo_readpreference_ce ;
36
36
37
+ #define PHONGO_READ_PRIMARY "primary"
38
+ #define PHONGO_READ_PRIMARY_PREFERRED "primaryPreferred"
39
+ #define PHONGO_READ_SECONDARY "secondary"
40
+ #define PHONGO_READ_SECONDARY_PREFERRED "secondaryPreferred"
41
+ #define PHONGO_READ_NEAREST "nearest"
42
+
37
43
/* Initialize the object from a HashTable and return whether it was successful.
38
44
* An exception will be thrown on error. */
39
45
static bool php_phongo_readpreference_init_from_hash (php_phongo_readpreference_t * intern , HashTable * props TSRMLS_DC ) /* {{{ */
@@ -42,15 +48,15 @@ static bool php_phongo_readpreference_init_from_hash(php_phongo_readpreference_t
42
48
zval * mode , * tagSets , * maxStalenessSeconds ;
43
49
44
50
if ((mode = zend_hash_str_find (props , "mode" , sizeof ("mode" ) - 1 )) && Z_TYPE_P (mode ) == IS_STRING ) {
45
- if (strcasecmp (Z_STRVAL_P (mode ), "primary" ) == 0 ) {
51
+ if (strcasecmp (Z_STRVAL_P (mode ), PHONGO_READ_PRIMARY ) == 0 ) {
46
52
intern -> read_preference = mongoc_read_prefs_new (MONGOC_READ_PRIMARY );
47
- } else if (strcasecmp (Z_STRVAL_P (mode ), "primaryPreferred" ) == 0 ) {
53
+ } else if (strcasecmp (Z_STRVAL_P (mode ), PHONGO_READ_PRIMARY_PREFERRED ) == 0 ) {
48
54
intern -> read_preference = mongoc_read_prefs_new (MONGOC_READ_PRIMARY_PREFERRED );
49
- } else if (strcasecmp (Z_STRVAL_P (mode ), "secondary" ) == 0 ) {
55
+ } else if (strcasecmp (Z_STRVAL_P (mode ), PHONGO_READ_SECONDARY ) == 0 ) {
50
56
intern -> read_preference = mongoc_read_prefs_new (MONGOC_READ_SECONDARY );
51
- } else if (strcasecmp (Z_STRVAL_P (mode ), "secondaryPreferred" ) == 0 ) {
57
+ } else if (strcasecmp (Z_STRVAL_P (mode ), PHONGO_READ_SECONDARY_PREFERRED ) == 0 ) {
52
58
intern -> read_preference = mongoc_read_prefs_new (MONGOC_READ_SECONDARY_PREFERRED );
53
- } else if (strcasecmp (Z_STRVAL_P (mode ), "nearest" ) == 0 ) {
59
+ } else if (strcasecmp (Z_STRVAL_P (mode ), PHONGO_READ_NEAREST ) == 0 ) {
54
60
intern -> read_preference = mongoc_read_prefs_new (MONGOC_READ_NEAREST );
55
61
} else {
56
62
phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "%s initialization requires specific values for \"mode\" string field" , ZSTR_VAL (php_phongo_readpreference_ce -> name ));
@@ -113,15 +119,15 @@ static bool php_phongo_readpreference_init_from_hash(php_phongo_readpreference_t
113
119
zval * * mode , * * tagSets , * * maxStalenessSeconds ;
114
120
115
121
if (zend_hash_find (props , "mode" , sizeof ("mode" ), (void * * ) & mode ) == SUCCESS && Z_TYPE_PP (mode ) == IS_STRING ) {
116
- if (strcasecmp (Z_STRVAL_PP (mode ), "primary" ) == 0 ) {
122
+ if (strcasecmp (Z_STRVAL_PP (mode ), PHONGO_READ_PRIMARY ) == 0 ) {
117
123
intern -> read_preference = mongoc_read_prefs_new (MONGOC_READ_PRIMARY );
118
- } else if (strcasecmp (Z_STRVAL_PP (mode ), "primaryPreferred" ) == 0 ) {
124
+ } else if (strcasecmp (Z_STRVAL_PP (mode ), PHONGO_READ_PRIMARY_PREFERRED ) == 0 ) {
119
125
intern -> read_preference = mongoc_read_prefs_new (MONGOC_READ_PRIMARY_PREFERRED );
120
- } else if (strcasecmp (Z_STRVAL_PP (mode ), "secondary" ) == 0 ) {
126
+ } else if (strcasecmp (Z_STRVAL_PP (mode ), PHONGO_READ_SECONDARY ) == 0 ) {
121
127
intern -> read_preference = mongoc_read_prefs_new (MONGOC_READ_SECONDARY );
122
- } else if (strcasecmp (Z_STRVAL_PP (mode ), "secondaryPreferred" ) == 0 ) {
128
+ } else if (strcasecmp (Z_STRVAL_PP (mode ), PHONGO_READ_SECONDARY_PREFERRED ) == 0 ) {
123
129
intern -> read_preference = mongoc_read_prefs_new (MONGOC_READ_SECONDARY_PREFERRED );
124
- } else if (strcasecmp (Z_STRVAL_PP (mode ), "nearest" ) == 0 ) {
130
+ } else if (strcasecmp (Z_STRVAL_PP (mode ), PHONGO_READ_NEAREST ) == 0 ) {
125
131
intern -> read_preference = mongoc_read_prefs_new (MONGOC_READ_NEAREST );
126
132
} else {
127
133
phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "%s initialization requires specific values for \"mode\" string field" , ZSTR_VAL (php_phongo_readpreference_ce -> name ));
@@ -190,6 +196,28 @@ static bool php_phongo_readpreference_init_from_hash(php_phongo_readpreference_t
190
196
return false;
191
197
} /* }}} */
192
198
199
+ static const char * php_phongo_readpreference_get_mode_string (mongoc_read_mode_t mode TSRMLS_DC ) /* {{{ */
200
+ {
201
+ switch (mode ) {
202
+ case MONGOC_READ_PRIMARY :
203
+ return PHONGO_READ_PRIMARY ;
204
+ case MONGOC_READ_PRIMARY_PREFERRED :
205
+ return PHONGO_READ_PRIMARY_PREFERRED ;
206
+ case MONGOC_READ_SECONDARY :
207
+ return PHONGO_READ_SECONDARY ;
208
+ case MONGOC_READ_SECONDARY_PREFERRED :
209
+ return PHONGO_READ_SECONDARY_PREFERRED ;
210
+ case MONGOC_READ_NEAREST :
211
+ return PHONGO_READ_NEAREST ;
212
+ default :
213
+ /* Should never happen, but if it does: exception */
214
+ phongo_throw_exception (PHONGO_ERROR_LOGIC TSRMLS_CC , "Mode '%d' should never have been passed to php_phongo_readpreference_get_mode_string, please file a bug report" , mode );
215
+ break ;
216
+ }
217
+
218
+ return NULL ;
219
+ } /* }}} */
220
+
193
221
/* {{{ proto void MongoDB\Driver\ReadPreference::__construct(int|string $mode[, array $tagSets = array()[, array $options = array()]])
194
222
Constructs a new ReadPreference */
195
223
static PHP_METHOD (ReadPreference , __construct )
@@ -225,15 +253,15 @@ static PHP_METHOD(ReadPreference, __construct)
225
253
return ;
226
254
}
227
255
} else if (Z_TYPE_P (mode ) == IS_STRING ) {
228
- if (strcasecmp (Z_STRVAL_P (mode ), "primary" ) == 0 ) {
256
+ if (strcasecmp (Z_STRVAL_P (mode ), PHONGO_READ_PRIMARY ) == 0 ) {
229
257
intern -> read_preference = mongoc_read_prefs_new (MONGOC_READ_PRIMARY );
230
- } else if (strcasecmp (Z_STRVAL_P (mode ), "primaryPreferred" ) == 0 ) {
258
+ } else if (strcasecmp (Z_STRVAL_P (mode ), PHONGO_READ_PRIMARY_PREFERRED ) == 0 ) {
231
259
intern -> read_preference = mongoc_read_prefs_new (MONGOC_READ_PRIMARY_PREFERRED );
232
- } else if (strcasecmp (Z_STRVAL_P (mode ), "secondary" ) == 0 ) {
260
+ } else if (strcasecmp (Z_STRVAL_P (mode ), PHONGO_READ_SECONDARY ) == 0 ) {
233
261
intern -> read_preference = mongoc_read_prefs_new (MONGOC_READ_SECONDARY );
234
- } else if (strcasecmp (Z_STRVAL_P (mode ), "secondaryPreferred" ) == 0 ) {
262
+ } else if (strcasecmp (Z_STRVAL_P (mode ), PHONGO_READ_SECONDARY_PREFERRED ) == 0 ) {
235
263
intern -> read_preference = mongoc_read_prefs_new (MONGOC_READ_SECONDARY_PREFERRED );
236
- } else if (strcasecmp (Z_STRVAL_P (mode ), "nearest" ) == 0 ) {
264
+ } else if (strcasecmp (Z_STRVAL_P (mode ), PHONGO_READ_NEAREST ) == 0 ) {
237
265
intern -> read_preference = mongoc_read_prefs_new (MONGOC_READ_NEAREST );
238
266
} else {
239
267
phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Invalid mode: '%s'" , Z_STRVAL_P (mode ));
@@ -343,6 +371,28 @@ static PHP_METHOD(ReadPreference, getMode)
343
371
RETURN_LONG (mongoc_read_prefs_get_mode (intern -> read_preference ));
344
372
} /* }}} */
345
373
374
+ /* {{{ proto string MongoDB\Driver\ReadPreference::getModeString()
375
+ Returns the ReadPreference mode as string */
376
+ static PHP_METHOD (ReadPreference , getModeString )
377
+ {
378
+ php_phongo_readpreference_t * intern ;
379
+ const char * mode_string ;
380
+
381
+ intern = Z_READPREFERENCE_OBJ_P (getThis ());
382
+
383
+ if (zend_parse_parameters_none () == FAILURE ) {
384
+ return ;
385
+ }
386
+
387
+ mode_string = php_phongo_readpreference_get_mode_string (mongoc_read_prefs_get_mode (intern -> read_preference ) TSRMLS_CC );
388
+ if (!mode_string ) {
389
+ /* Exception already thrown */
390
+ return ;
391
+ }
392
+
393
+ PHONGO_RETURN_STRING (mode_string );
394
+ } /* }}} */
395
+
346
396
/* {{{ proto array MongoDB\Driver\ReadPreference::getTagSets()
347
397
Returns the ReadPreference tag sets */
348
398
static PHP_METHOD (ReadPreference , getTagSets )
@@ -378,26 +428,6 @@ static PHP_METHOD(ReadPreference, getTagSets)
378
428
}
379
429
} /* }}} */
380
430
381
- static const char * php_phongo_readpreference_get_mode_string (mongoc_read_mode_t mode ) /* {{{ */
382
- {
383
- switch (mode ) {
384
- case MONGOC_READ_PRIMARY :
385
- return "primary" ;
386
- case MONGOC_READ_PRIMARY_PREFERRED :
387
- return "primaryPreferred" ;
388
- case MONGOC_READ_SECONDARY :
389
- return "secondary" ;
390
- case MONGOC_READ_SECONDARY_PREFERRED :
391
- return "secondaryPreferred" ;
392
- case MONGOC_READ_NEAREST :
393
- return "nearest" ;
394
- default : /* Do nothing */
395
- break ;
396
- }
397
-
398
- return NULL ;
399
- } /* }}} */
400
-
401
431
static HashTable * php_phongo_readpreference_get_properties_hash (zval * object , bool is_debug TSRMLS_DC ) /* {{{ */
402
432
{
403
433
php_phongo_readpreference_t * intern ;
@@ -416,7 +446,7 @@ static HashTable* php_phongo_readpreference_get_properties_hash(zval* object, bo
416
446
417
447
tags = mongoc_read_prefs_get_tags (intern -> read_preference );
418
448
mode = mongoc_read_prefs_get_mode (intern -> read_preference );
419
- modeString = php_phongo_readpreference_get_mode_string (mode );
449
+ modeString = php_phongo_readpreference_get_mode_string (mode TSRMLS_CC );
420
450
421
451
if (modeString ) {
422
452
#if PHP_VERSION_ID >= 70000
@@ -510,7 +540,7 @@ static PHP_METHOD(ReadPreference, serialize)
510
540
511
541
tags = mongoc_read_prefs_get_tags (intern -> read_preference );
512
542
mode = mongoc_read_prefs_get_mode (intern -> read_preference );
513
- modeString = php_phongo_readpreference_get_mode_string (mode );
543
+ modeString = php_phongo_readpreference_get_mode_string (mode TSRMLS_CC );
514
544
maxStalenessSeconds = mongoc_read_prefs_get_max_staleness_seconds (intern -> read_preference );
515
545
516
546
#if PHP_VERSION_ID >= 70000
@@ -638,6 +668,7 @@ static zend_function_entry php_phongo_readpreference_me[] = {
638
668
PHP_ME (ReadPreference , __set_state , ai_ReadPreference___set_state , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC )
639
669
PHP_ME (ReadPreference , getMaxStalenessSeconds , ai_ReadPreference_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
640
670
PHP_ME (ReadPreference , getMode , ai_ReadPreference_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
671
+ PHP_ME (ReadPreference , getModeString , ai_ReadPreference_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
641
672
PHP_ME (ReadPreference , getTagSets , ai_ReadPreference_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
642
673
PHP_ME (ReadPreference , bsonSerialize , ai_ReadPreference_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
643
674
PHP_ME (ReadPreference , serialize , ai_ReadPreference_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
@@ -733,6 +764,12 @@ void php_phongo_readpreference_init_ce(INIT_FUNC_ARGS) /* {{{ */
733
764
zend_declare_class_constant_long (php_phongo_readpreference_ce , ZEND_STRL ("RP_NEAREST" ), MONGOC_READ_NEAREST TSRMLS_CC );
734
765
zend_declare_class_constant_long (php_phongo_readpreference_ce , ZEND_STRL ("NO_MAX_STALENESS" ), MONGOC_NO_MAX_STALENESS TSRMLS_CC );
735
766
zend_declare_class_constant_long (php_phongo_readpreference_ce , ZEND_STRL ("SMALLEST_MAX_STALENESS_SECONDS" ), MONGOC_SMALLEST_MAX_STALENESS_SECONDS TSRMLS_CC );
767
+
768
+ zend_declare_class_constant_string (php_phongo_readpreference_ce , ZEND_STRL ("PRIMARY" ), PHONGO_READ_PRIMARY TSRMLS_CC );
769
+ zend_declare_class_constant_string (php_phongo_readpreference_ce , ZEND_STRL ("PRIMARY_PREFERRED" ), PHONGO_READ_PRIMARY_PREFERRED TSRMLS_CC );
770
+ zend_declare_class_constant_string (php_phongo_readpreference_ce , ZEND_STRL ("SECONDARY" ), PHONGO_READ_SECONDARY TSRMLS_CC );
771
+ zend_declare_class_constant_string (php_phongo_readpreference_ce , ZEND_STRL ("SECONDARY_PREFERRED" ), PHONGO_READ_SECONDARY_PREFERRED TSRMLS_CC );
772
+ zend_declare_class_constant_string (php_phongo_readpreference_ce , ZEND_STRL ("NEAREST" ), PHONGO_READ_NEAREST TSRMLS_CC );
736
773
} /* }}} */
737
774
738
775
/*
0 commit comments