34
34
#include <ext/standard/info.h>
35
35
#include <Zend/zend_interfaces.h>
36
36
#include <ext/spl/spl_iterators.h>
37
+ /* PHP array helpers */
38
+ #include "php_array_api.h"
37
39
/* Our Compatability header */
38
40
#include "phongo_compat.h"
39
41
@@ -46,21 +48,22 @@ PHONGO_API zend_class_entry *php_phongo_readpreference_ce;
46
48
47
49
zend_object_handlers php_phongo_handler_readpreference ;
48
50
49
- /* {{{ proto void ReadPreference::__construct(integer $mode[, array $tagSets = array()])
51
+ /* {{{ proto void ReadPreference::__construct(integer $mode[, array $tagSets = array()[, array $options = array()] ])
50
52
Constructs a new ReadPreference */
51
53
PHP_METHOD (ReadPreference , __construct )
52
54
{
53
55
php_phongo_readpreference_t * intern ;
54
56
zend_error_handling error_handling ;
55
57
phongo_long mode ;
56
58
zval * tagSets = NULL ;
59
+ zval * options = NULL ;
57
60
SUPPRESS_UNUSED_WARNING (return_value_ptr ) SUPPRESS_UNUSED_WARNING (return_value ) SUPPRESS_UNUSED_WARNING (return_value_used )
58
61
59
62
60
63
zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
61
64
intern = Z_READPREFERENCE_OBJ_P (getThis ());
62
65
63
- if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "l|a!" , & mode , & tagSets ) == FAILURE ) {
66
+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "l|a!a! " , & mode , & tagSets , & options ) == FAILURE ) {
64
67
zend_restore_error_handling (& error_handling TSRMLS_CC );
65
68
return ;
66
69
}
@@ -80,28 +83,46 @@ PHP_METHOD(ReadPreference, __construct)
80
83
return ;
81
84
}
82
85
83
- switch (ZEND_NUM_ARGS ()) {
84
- case 2 :
85
- if (tagSets ) {
86
- bson_t * tags = bson_new ();
86
+ if (tagSets ) {
87
+ bson_t * tags = bson_new ();
87
88
88
- phongo_zval_to_bson (tagSets , PHONGO_BSON_NONE , (bson_t * )tags , NULL TSRMLS_CC );
89
+ phongo_zval_to_bson (tagSets , PHONGO_BSON_NONE , (bson_t * )tags , NULL TSRMLS_CC );
89
90
90
- if (!php_phongo_read_preference_tags_are_valid (tags )) {
91
- phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "tagSets must be an array of zero or more documents" );
92
- bson_destroy (tags );
93
- return ;
94
- }
91
+ if (!php_phongo_read_preference_tags_are_valid (tags )) {
92
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "tagSets must be an array of zero or more documents" );
93
+ bson_destroy (tags );
94
+ return ;
95
+ }
96
+
97
+ if (!bson_empty (tags ) && mode == MONGOC_READ_PRIMARY ) {
98
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "tagSets may not be used with primary mode" );
99
+ bson_destroy (tags );
100
+ return ;
101
+ }
102
+
103
+ mongoc_read_prefs_set_tags (intern -> read_preference , tags );
104
+ bson_destroy (tags );
105
+ }
106
+
107
+ if (options && php_array_exists (options , "maxStalenessMS" )) {
108
+ phongo_long maxStalenessMS = php_array_fetchc_long (options , "maxStalenessMS" );
95
109
96
- if (!bson_empty (tags ) && mode == MONGOC_READ_PRIMARY ) {
97
- phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "tagSets may not be used with primary mode" );
98
- bson_destroy (tags );
99
- return ;
100
- }
110
+ if (maxStalenessMS < 0 ) {
111
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Expected maxStalenessMS to be >= 0, %" PHONGO_LONG_FORMAT " given" , maxStalenessMS );
112
+ return ;
113
+ }
114
+
115
+ if (maxStalenessMS > INT32_MAX ) {
116
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Expected maxStalenessMS to be <= %" PRId32 ", %" PHONGO_LONG_FORMAT " given" , INT32_MAX , maxStalenessMS );
117
+ return ;
118
+ }
101
119
102
- mongoc_read_prefs_set_tags (intern -> read_preference , tags );
103
- bson_destroy (tags );
104
- }
120
+ if (maxStalenessMS > 0 && mode == MONGOC_READ_PRIMARY ) {
121
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "maxStalenessMS may not be used with primary mode" );
122
+ return ;
123
+ }
124
+
125
+ mongoc_read_prefs_set_max_staleness_ms (intern -> read_preference , maxStalenessMS );
105
126
}
106
127
107
128
if (!mongoc_read_prefs_is_valid (intern -> read_preference )) {
@@ -111,6 +132,23 @@ PHP_METHOD(ReadPreference, __construct)
111
132
}
112
133
/* }}} */
113
134
135
+ /* {{{ proto integer ReadPreference::getMaxStalenessMS()
136
+ Returns the ReadPreference maxStalenessMS value */
137
+ PHP_METHOD (ReadPreference , getMaxStalenessMS )
138
+ {
139
+ php_phongo_readpreference_t * intern ;
140
+ SUPPRESS_UNUSED_WARNING (return_value_ptr ) SUPPRESS_UNUSED_WARNING (return_value_used )
141
+
142
+ intern = Z_READPREFERENCE_OBJ_P (getThis ());
143
+
144
+ if (zend_parse_parameters_none () == FAILURE ) {
145
+ return ;
146
+ }
147
+
148
+ RETURN_LONG (mongoc_read_prefs_get_max_staleness_ms (intern -> read_preference ));
149
+ }
150
+ /* }}} */
151
+
114
152
/* {{{ proto integer ReadPreference::getMode()
115
153
Returns the ReadPreference mode */
116
154
PHP_METHOD (ReadPreference , getMode )
@@ -184,13 +222,15 @@ PHP_METHOD(ReadPreference, bsonSerialize)
184
222
ZEND_BEGIN_ARG_INFO_EX (ai_ReadPreference___construct , 0 , 0 , 1 )
185
223
ZEND_ARG_INFO (0 , mode )
186
224
ZEND_ARG_ARRAY_INFO (0 , tagSets , 1 )
225
+ ZEND_ARG_ARRAY_INFO (0 , options , 1 )
187
226
ZEND_END_ARG_INFO ()
188
227
189
228
ZEND_BEGIN_ARG_INFO_EX (ai_ReadPreference_void , 0 , 0 , 0 )
190
229
ZEND_END_ARG_INFO ()
191
230
192
231
static zend_function_entry php_phongo_readpreference_me [] = {
193
232
PHP_ME (ReadPreference , __construct , ai_ReadPreference___construct , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
233
+ PHP_ME (ReadPreference , getMaxStalenessMS , ai_ReadPreference_void , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
194
234
PHP_ME (ReadPreference , getMode , ai_ReadPreference_void , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
195
235
PHP_ME (ReadPreference , getTagSets , ai_ReadPreference_void , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
196
236
PHP_ME (ReadPreference , bsonSerialize , ai_ReadPreference_void , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
0 commit comments