Skip to content

Commit af45053

Browse files
committed
Merged pull request #413
2 parents e9d9f0d + 8f9e6c8 commit af45053

19 files changed

+108
-248
lines changed

php_phongo.c

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,6 @@ void php_phongo_read_concern_to_zval(zval *retval, const mongoc_read_concern_t *
931931

932932
if (level) {
933933
ADD_ASSOC_STRING(retval, "level", (char *)level);
934-
} else {
935-
ADD_ASSOC_NULL_EX(retval, "level");
936934
}
937935
} /* }}} */
938936

@@ -962,12 +960,21 @@ bool php_phongo_read_preference_tags_are_valid(const bson_t *tags) /* {{{ */
962960
void php_phongo_read_preference_to_zval(zval *retval, const mongoc_read_prefs_t *read_prefs) /* {{{ */
963961
{
964962
const bson_t *tags = mongoc_read_prefs_get_tags(read_prefs);
963+
mongoc_read_mode_t mode = mongoc_read_prefs_get_mode(read_prefs);
965964

966965
array_init_size(retval, 2);
967966

968-
ADD_ASSOC_LONG_EX(retval, "mode", mongoc_read_prefs_get_mode(read_prefs));
967+
switch (mode) {
968+
case MONGOC_READ_PRIMARY: ADD_ASSOC_STRING(retval, "mode", "primary"); break;
969+
case MONGOC_READ_PRIMARY_PREFERRED: ADD_ASSOC_STRING(retval, "mode", "primaryPreferred"); break;
970+
case MONGOC_READ_SECONDARY: ADD_ASSOC_STRING(retval, "mode", "secondary"); break;
971+
case MONGOC_READ_SECONDARY_PREFERRED: ADD_ASSOC_STRING(retval, "mode", "secondaryPreferred"); break;
972+
case MONGOC_READ_NEAREST: ADD_ASSOC_STRING(retval, "mode", "nearest"); break;
973+
default: /* Do nothing */
974+
break;
975+
}
969976

970-
if (tags->len) {
977+
if (!bson_empty0(tags)) {
971978
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
972979
/* Use native arrays for debugging output */
973980
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
@@ -979,15 +986,14 @@ void php_phongo_read_preference_to_zval(zval *retval, const mongoc_read_prefs_t
979986
#else
980987
ADD_ASSOC_ZVAL_EX(retval, "tags", state.zchild);
981988
#endif
982-
} else {
983-
ADD_ASSOC_NULL_EX(retval, "tags");
984989
}
985990
} /* }}} */
986991

987992
void php_phongo_write_concern_to_zval(zval *retval, const mongoc_write_concern_t *write_concern) /* {{{ */
988993
{
989994
const char *wtag = mongoc_write_concern_get_wtag(write_concern);
990995
const int32_t w = mongoc_write_concern_get_w(write_concern);
996+
const int32_t wtimeout = mongoc_write_concern_get_wtimeout(write_concern);
991997

992998
array_init_size(retval, 4);
993999

@@ -997,17 +1003,14 @@ void php_phongo_write_concern_to_zval(zval *retval, const mongoc_write_concern_t
9971003
ADD_ASSOC_STRING(retval, "w", (char *)PHONGO_WRITE_CONCERN_W_MAJORITY);
9981004
} else if (w != MONGOC_WRITE_CONCERN_W_DEFAULT) {
9991005
ADD_ASSOC_LONG_EX(retval, "w", w);
1000-
} else {
1001-
ADD_ASSOC_NULL_EX(retval, "w");
10021006
}
10031007

1004-
ADD_ASSOC_BOOL_EX(retval, "wmajority", mongoc_write_concern_get_wmajority(write_concern));
1005-
ADD_ASSOC_LONG_EX(retval, "wtimeout", mongoc_write_concern_get_wtimeout(write_concern));
1006-
10071008
if (mongoc_write_concern_journal_is_set(write_concern)) {
1008-
ADD_ASSOC_BOOL_EX(retval, "journal", mongoc_write_concern_get_journal(write_concern));
1009-
} else {
1010-
ADD_ASSOC_NULL_EX(retval, "journal");
1009+
ADD_ASSOC_BOOL_EX(retval, "j", mongoc_write_concern_get_journal(write_concern));
1010+
}
1011+
1012+
if (wtimeout != 0) {
1013+
ADD_ASSOC_LONG_EX(retval, "wtimeout", wtimeout);
10111014
}
10121015
} /* }}} */
10131016
/* }}} */
@@ -2105,6 +2108,20 @@ PHP_MINIT_FUNCTION(mongodb)
21052108

21062109
PHP_MINIT(bson)(INIT_FUNC_ARGS_PASSTHRU);
21072110

2111+
PHP_MINIT(Type)(INIT_FUNC_ARGS_PASSTHRU);
2112+
PHP_MINIT(Serializable)(INIT_FUNC_ARGS_PASSTHRU);
2113+
PHP_MINIT(Unserializable)(INIT_FUNC_ARGS_PASSTHRU);
2114+
PHP_MINIT(Persistable)(INIT_FUNC_ARGS_PASSTHRU);
2115+
PHP_MINIT(Binary)(INIT_FUNC_ARGS_PASSTHRU);
2116+
PHP_MINIT(Decimal128)(INIT_FUNC_ARGS_PASSTHRU);
2117+
PHP_MINIT(Javascript)(INIT_FUNC_ARGS_PASSTHRU);
2118+
PHP_MINIT(MaxKey)(INIT_FUNC_ARGS_PASSTHRU);
2119+
PHP_MINIT(MinKey)(INIT_FUNC_ARGS_PASSTHRU);
2120+
PHP_MINIT(ObjectID)(INIT_FUNC_ARGS_PASSTHRU);
2121+
PHP_MINIT(Regex)(INIT_FUNC_ARGS_PASSTHRU);
2122+
PHP_MINIT(Timestamp)(INIT_FUNC_ARGS_PASSTHRU);
2123+
PHP_MINIT(UTCDateTime)(INIT_FUNC_ARGS_PASSTHRU);
2124+
21082125
PHP_MINIT(Command)(INIT_FUNC_ARGS_PASSTHRU);
21092126
PHP_MINIT(Cursor)(INIT_FUNC_ARGS_PASSTHRU);
21102127
PHP_MINIT(CursorId)(INIT_FUNC_ARGS_PASSTHRU);
@@ -2132,20 +2149,6 @@ PHP_MINIT_FUNCTION(mongodb)
21322149
PHP_MINIT(ExecutionTimeoutException)(INIT_FUNC_ARGS_PASSTHRU);
21332150
PHP_MINIT(ConnectionTimeoutException)(INIT_FUNC_ARGS_PASSTHRU);
21342151

2135-
PHP_MINIT(Type)(INIT_FUNC_ARGS_PASSTHRU);
2136-
PHP_MINIT(Serializable)(INIT_FUNC_ARGS_PASSTHRU);
2137-
PHP_MINIT(Unserializable)(INIT_FUNC_ARGS_PASSTHRU);
2138-
PHP_MINIT(Persistable)(INIT_FUNC_ARGS_PASSTHRU);
2139-
PHP_MINIT(Binary)(INIT_FUNC_ARGS_PASSTHRU);
2140-
PHP_MINIT(Decimal128)(INIT_FUNC_ARGS_PASSTHRU);
2141-
PHP_MINIT(Javascript)(INIT_FUNC_ARGS_PASSTHRU);
2142-
PHP_MINIT(MaxKey)(INIT_FUNC_ARGS_PASSTHRU);
2143-
PHP_MINIT(MinKey)(INIT_FUNC_ARGS_PASSTHRU);
2144-
PHP_MINIT(ObjectID)(INIT_FUNC_ARGS_PASSTHRU);
2145-
PHP_MINIT(Regex)(INIT_FUNC_ARGS_PASSTHRU);
2146-
PHP_MINIT(Timestamp)(INIT_FUNC_ARGS_PASSTHRU);
2147-
PHP_MINIT(UTCDateTime)(INIT_FUNC_ARGS_PASSTHRU);
2148-
21492152
REGISTER_STRING_CONSTANT("MONGODB_VERSION", (char *)MONGODB_VERSION_S, CONST_CS | CONST_PERSISTENT);
21502153
REGISTER_STRING_CONSTANT("MONGODB_STABILITY", (char *)MONGODB_STABILITY_S, CONST_CS | CONST_PERSISTENT);
21512154

src/MongoDB/ReadConcern.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,20 @@ PHP_METHOD(ReadConcern, getLevel)
101101
}
102102
/* }}} */
103103

104+
/* {{{ proto array ReadConcern::bsonSerialize()
105+
*/
106+
PHP_METHOD(ReadConcern, bsonSerialize)
107+
{
108+
const mongoc_read_concern_t *read_concern = phongo_read_concern_from_zval(getThis() TSRMLS_CC);
109+
110+
if (zend_parse_parameters_none() == FAILURE) {
111+
return;
112+
}
113+
114+
php_phongo_read_concern_to_zval(return_value, read_concern);
115+
}
116+
/* }}} */
117+
104118
/**
105119
* Value object for read concern used in issuing read operations.
106120
*/
@@ -116,7 +130,7 @@ ZEND_END_ARG_INFO()
116130
static zend_function_entry php_phongo_readconcern_me[] = {
117131
PHP_ME(ReadConcern, __construct, ai_ReadConcern___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
118132
PHP_ME(ReadConcern, getLevel, ai_ReadConcern_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
119-
PHP_ME(Manager, __wakeup, ai_ReadConcern_void, ZEND_ACC_PUBLIC)
133+
PHP_ME(ReadConcern, bsonSerialize, ai_ReadConcern_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
120134
PHP_FE_END
121135
};
122136

@@ -192,6 +206,8 @@ PHP_MINIT_FUNCTION(ReadConcern)
192206
PHONGO_CE_FINAL(php_phongo_readconcern_ce);
193207
PHONGO_CE_DISABLE_SERIALIZATION(php_phongo_readconcern_ce);
194208

209+
zend_class_implements(php_phongo_readconcern_ce TSRMLS_CC, 1, php_phongo_serializable_ce);
210+
195211
memcpy(&php_phongo_handler_readconcern, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
196212
php_phongo_handler_readconcern.get_debug_info = php_phongo_readconcern_get_debug_info;
197213
#if PHP_VERSION_ID >= 70000

src/MongoDB/ReadPreference.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ PHP_METHOD(ReadPreference, getTagSets)
162162
}
163163
/* }}} */
164164

165+
/* {{{ proto array ReadPreference::bsonSerialize()
166+
*/
167+
PHP_METHOD(ReadPreference, bsonSerialize)
168+
{
169+
const mongoc_read_prefs_t *read_preference = phongo_read_preference_from_zval(getThis() TSRMLS_CC);
170+
171+
if (zend_parse_parameters_none() == FAILURE) {
172+
return;
173+
}
174+
175+
php_phongo_read_preference_to_zval(return_value, read_preference);
176+
}
177+
/* }}} */
178+
165179
/**
166180
* Value object for read preferences used in issuing commands and queries.
167181
*/
@@ -179,7 +193,7 @@ static zend_function_entry php_phongo_readpreference_me[] = {
179193
PHP_ME(ReadPreference, __construct, ai_ReadPreference___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
180194
PHP_ME(ReadPreference, getMode, ai_ReadPreference_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
181195
PHP_ME(ReadPreference, getTagSets, ai_ReadPreference_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
182-
PHP_ME(Manager, __wakeup, ai_ReadPreference_void, ZEND_ACC_PUBLIC)
196+
PHP_ME(ReadPreference, bsonSerialize, ai_ReadPreference_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
183197
PHP_FE_END
184198
};
185199

@@ -258,6 +272,8 @@ PHP_MINIT_FUNCTION(ReadPreference)
258272
PHONGO_CE_FINAL(php_phongo_readpreference_ce);
259273
PHONGO_CE_DISABLE_SERIALIZATION(php_phongo_readpreference_ce);
260274

275+
zend_class_implements(php_phongo_readpreference_ce TSRMLS_CC, 1, php_phongo_serializable_ce);
276+
261277
memcpy(&php_phongo_handler_readpreference, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
262278
php_phongo_handler_readpreference.get_debug_info = php_phongo_readpreference_get_debug_info;
263279
#if PHP_VERSION_ID >= 70000

src/MongoDB/WriteConcern.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ PHP_METHOD(WriteConcern, getJournal)
182182
}
183183
/* }}} */
184184

185+
/* {{{ proto array WriteConcern::bsonSerialize()
186+
*/
187+
PHP_METHOD(WriteConcern, bsonSerialize)
188+
{
189+
const mongoc_write_concern_t *write_concern = phongo_write_concern_from_zval(getThis() TSRMLS_CC);
190+
191+
if (zend_parse_parameters_none() == FAILURE) {
192+
return;
193+
}
194+
195+
php_phongo_write_concern_to_zval(return_value, write_concern);
196+
}
197+
/* }}} */
198+
185199
/**
186200
* Value object for write concern used in issuing write operations.
187201
*/
@@ -201,7 +215,7 @@ static zend_function_entry php_phongo_writeconcern_me[] = {
201215
PHP_ME(WriteConcern, getW, ai_WriteConcern_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
202216
PHP_ME(WriteConcern, getWtimeout, ai_WriteConcern_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
203217
PHP_ME(WriteConcern, getJournal, ai_WriteConcern_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
204-
PHP_ME(Manager, __wakeup, ai_WriteConcern_void, ZEND_ACC_PUBLIC)
218+
PHP_ME(WriteConcern, bsonSerialize, ai_WriteConcern_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
205219
PHP_FE_END
206220
};
207221

@@ -277,6 +291,8 @@ PHP_MINIT_FUNCTION(WriteConcern)
277291
PHONGO_CE_FINAL(php_phongo_writeconcern_ce);
278292
PHONGO_CE_DISABLE_SERIALIZATION(php_phongo_writeconcern_ce);
279293

294+
zend_class_implements(php_phongo_writeconcern_ce TSRMLS_CC, 1, php_phongo_serializable_ce);
295+
280296
memcpy(&php_phongo_handler_writeconcern, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
281297
php_phongo_handler_writeconcern.get_debug_info = php_phongo_writeconcern_get_debug_info;
282298
#if PHP_VERSION_ID >= 70000

tests/bulk/write-0002.phpt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,8 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
6767
array(%d) {
6868
["w"]=>
6969
int(1)
70-
["wmajority"]=>
71-
bool(false)
7270
["wtimeout"]=>
7371
int(1000)
74-
["journal"]=>
75-
NULL
7672
}
7773
}
7874
Inserted 2 documents to %s

tests/manager/manager-getreadconcern-001.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ foreach ($tests as $i => $test) {
3232
<?php exit(0); ?>
3333
--EXPECTF--
3434
object(MongoDB\Driver\ReadConcern)#%d (%d) {
35-
["level"]=>
36-
NULL
3735
}
3836
object(MongoDB\Driver\ReadConcern)#%d (%d) {
3937
["level"]=>

tests/manager/manager-getreadpreference-001.phpt

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,23 @@ foreach ($tests as $i => $test) {
3232
--EXPECTF--
3333
object(MongoDB\Driver\ReadPreference)#%d (%d) {
3434
["mode"]=>
35-
int(1)
36-
["tags"]=>
37-
array(0) {
38-
}
35+
string(7) "primary"
3936
}
4037
object(MongoDB\Driver\ReadPreference)#%d (%d) {
4138
["mode"]=>
42-
int(2)
43-
["tags"]=>
44-
array(0) {
45-
}
39+
string(9) "secondary"
4640
}
4741
object(MongoDB\Driver\ReadPreference)#%d (%d) {
4842
["mode"]=>
49-
int(5)
50-
["tags"]=>
51-
array(0) {
52-
}
43+
string(16) "primaryPreferred"
5344
}
5445
object(MongoDB\Driver\ReadPreference)#%d (%d) {
5546
["mode"]=>
56-
int(6)
57-
["tags"]=>
58-
array(0) {
59-
}
47+
string(18) "secondaryPreferred"
6048
}
6149
object(MongoDB\Driver\ReadPreference)#%d (%d) {
6250
["mode"]=>
63-
int(2)
51+
string(9) "secondary"
6452
["tags"]=>
6553
array(2) {
6654
[0]=>
@@ -77,7 +65,7 @@ object(MongoDB\Driver\ReadPreference)#%d (%d) {
7765
}
7866
object(MongoDB\Driver\ReadPreference)#%d (%d) {
7967
["mode"]=>
80-
int(2)
68+
string(9) "secondary"
8169
["tags"]=>
8270
array(2) {
8371
[0]=>
@@ -94,7 +82,7 @@ object(MongoDB\Driver\ReadPreference)#%d (%d) {
9482
}
9583
object(MongoDB\Driver\ReadPreference)#%d (%d) {
9684
["mode"]=>
97-
int(2)
85+
string(9) "secondary"
9886
["tags"]=>
9987
array(1) {
10088
[0]=>

0 commit comments

Comments
 (0)