@@ -52,7 +52,7 @@ PHP_METHOD(ReadPreference, __construct)
5252{
5353 php_phongo_readpreference_t * intern ;
5454 zend_error_handling error_handling ;
55- long mode ;
55+ phongo_long mode ;
5656 zval * tagSets = NULL ;
5757 SUPPRESS_UNUSED_WARNING (return_value_ptr ) SUPPRESS_UNUSED_WARNING (return_value ) SUPPRESS_UNUSED_WARNING (return_value_used )
5858
@@ -74,22 +74,39 @@ PHP_METHOD(ReadPreference, __construct)
7474 case MONGOC_READ_SECONDARY_PREFERRED :
7575 case MONGOC_READ_NEAREST :
7676 intern -> read_preference = mongoc_read_prefs_new (mode );
77+ break ;
78+ default :
79+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Invalid mode: %" PHONGO_LONG_FORMAT , mode );
80+ return ;
81+ }
7782
83+ switch (ZEND_NUM_ARGS ()) {
84+ case 2 :
7885 if (tagSets ) {
7986 bson_t * tags = bson_new ();
8087
8188 phongo_zval_to_bson (tagSets , PHONGO_BSON_NONE , (bson_t * )tags , NULL TSRMLS_CC );
82- mongoc_read_prefs_set_tags (intern -> read_preference , tags );
83- bson_destroy (tags );
84- if (!mongoc_read_prefs_is_valid (intern -> read_preference )) {
85- phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Invalid tagSets" );
89+
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+ }
95+
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 );
8699 return ;
87100 }
101+
102+ mongoc_read_prefs_set_tags (intern -> read_preference , tags );
103+ bson_destroy (tags );
88104 }
89- break ;
90- default :
91- phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Invalid mode: %ld" , mode );
92- return ;
105+ }
106+
107+ if (!mongoc_read_prefs_is_valid (intern -> read_preference )) {
108+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Read preference is not valid" );
109+ return ;
93110 }
94111}
95112/* }}} */
0 commit comments