3434#include <ext/standard/info.h>
3535#include <Zend/zend_interfaces.h>
3636#include <ext/spl/spl_iterators.h>
37+ /* PHP array helpers */
38+ #include "php_array_api.h"
3739/* Our Compatability header */
3840#include "phongo_compat.h"
3941
@@ -46,21 +48,22 @@ PHONGO_API zend_class_entry *php_phongo_readpreference_ce;
4648
4749zend_object_handlers php_phongo_handler_readpreference ;
4850
49- /* {{{ proto void ReadPreference::__construct(integer $mode[, array $tagSets = array()])
51+ /* {{{ proto void ReadPreference::__construct(integer $mode[, array $tagSets = array()[, array $options = array()] ])
5052 Constructs a new ReadPreference */
5153PHP_METHOD (ReadPreference , __construct )
5254{
5355 php_phongo_readpreference_t * intern ;
5456 zend_error_handling error_handling ;
5557 phongo_long mode ;
5658 zval * tagSets = NULL ;
59+ zval * options = NULL ;
5760 SUPPRESS_UNUSED_WARNING (return_value_ptr ) SUPPRESS_UNUSED_WARNING (return_value ) SUPPRESS_UNUSED_WARNING (return_value_used )
5861
5962
6063 zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
6164 intern = Z_READPREFERENCE_OBJ_P (getThis ());
6265
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 ) {
6467 zend_restore_error_handling (& error_handling TSRMLS_CC );
6568 return ;
6669 }
@@ -80,28 +83,46 @@ PHP_METHOD(ReadPreference, __construct)
8083 return ;
8184 }
8285
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 ();
8788
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 );
8990
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" );
95109
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+ }
101119
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 );
105126 }
106127
107128 if (!mongoc_read_prefs_is_valid (intern -> read_preference )) {
@@ -111,6 +132,23 @@ PHP_METHOD(ReadPreference, __construct)
111132}
112133/* }}} */
113134
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+
114152/* {{{ proto integer ReadPreference::getMode()
115153 Returns the ReadPreference mode */
116154PHP_METHOD (ReadPreference , getMode )
@@ -184,13 +222,15 @@ PHP_METHOD(ReadPreference, bsonSerialize)
184222ZEND_BEGIN_ARG_INFO_EX (ai_ReadPreference___construct , 0 , 0 , 1 )
185223 ZEND_ARG_INFO (0 , mode )
186224 ZEND_ARG_ARRAY_INFO (0 , tagSets , 1 )
225+ ZEND_ARG_ARRAY_INFO (0 , options , 1 )
187226ZEND_END_ARG_INFO ()
188227
189228ZEND_BEGIN_ARG_INFO_EX (ai_ReadPreference_void , 0 , 0 , 0 )
190229ZEND_END_ARG_INFO ()
191230
192231static zend_function_entry php_phongo_readpreference_me [] = {
193232 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 )
194234 PHP_ME (ReadPreference , getMode , ai_ReadPreference_void , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
195235 PHP_ME (ReadPreference , getTagSets , ai_ReadPreference_void , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
196236 PHP_ME (ReadPreference , bsonSerialize , ai_ReadPreference_void , ZEND_ACC_PUBLIC |ZEND_ACC_FINAL )
0 commit comments