diff --git a/src/BSON/ObjectId.c b/src/BSON/ObjectId.c index fbcddcf00..d13fadd1c 100644 --- a/src/BSON/ObjectId.c +++ b/src/BSON/ObjectId.c @@ -198,11 +198,11 @@ static PHP_METHOD(ObjectId, serialize) } #if PHP_VERSION_ID >= 70000 - array_init_size(&retval, 2); + array_init_size(&retval, 1); ADD_ASSOC_STRINGL(&retval, "oid", intern->oid, 24); #else ALLOC_INIT_ZVAL(retval); - array_init_size(retval, 2); + array_init_size(retval, 1); ADD_ASSOC_STRINGL(retval, "oid", intern->oid, 24); #endif diff --git a/src/MongoDB/ReadConcern.c b/src/MongoDB/ReadConcern.c index 09cac58f2..5cdd945c0 100644 --- a/src/MongoDB/ReadConcern.c +++ b/src/MongoDB/ReadConcern.c @@ -16,6 +16,12 @@ #include #include +#include +#if PHP_VERSION_ID >= 70000 +#include +#else +#include +#endif #ifdef HAVE_CONFIG_H #include "config.h" @@ -197,6 +203,102 @@ static PHP_METHOD(ReadConcern, bsonSerialize) convert_to_object(return_value); } /* }}} */ +/* {{{ proto string MongoDB\Driver\ReadConcern::serialize() +*/ +static PHP_METHOD(ReadConcern, serialize) +{ + php_phongo_readconcern_t* intern; + ZVAL_RETVAL_TYPE retval; + php_serialize_data_t var_hash; + smart_str buf = { 0 }; + const char* level; + + intern = Z_READCONCERN_OBJ_P(getThis()); + + if (zend_parse_parameters_none() == FAILURE) { + return; + } + + if (!intern->read_concern) { + return; + } + + level = mongoc_read_concern_get_level(intern->read_concern); + + if (!level) { + PHONGO_RETURN_STRING(""); + } + +#if PHP_VERSION_ID >= 70000 + array_init_size(&retval, 1); + ADD_ASSOC_STRING(&retval, "level", level); +#else + ALLOC_INIT_ZVAL(retval); + array_init_size(retval, 1); + ADD_ASSOC_STRING(retval, "level", level); +#endif + + PHP_VAR_SERIALIZE_INIT(var_hash); + php_var_serialize(&buf, &retval, &var_hash TSRMLS_CC); + smart_str_0(&buf); + PHP_VAR_SERIALIZE_DESTROY(var_hash); + + PHONGO_RETVAL_SMART_STR(buf); + + smart_str_free(&buf); + zval_ptr_dtor(&retval); +} /* }}} */ + +/* {{{ proto void MongoDB\Driver\ReadConcern::unserialize(string $serialized) +*/ +static PHP_METHOD(ReadConcern, unserialize) +{ + php_phongo_readconcern_t* intern; + zend_error_handling error_handling; + char* serialized; + phongo_zpp_char_len serialized_len; +#if PHP_VERSION_ID >= 70000 + zval props; +#else + zval* props; +#endif + php_unserialize_data_t var_hash; + + intern = Z_READCONCERN_OBJ_P(getThis()); + + zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &serialized, &serialized_len) == FAILURE) { + zend_restore_error_handling(&error_handling TSRMLS_CC); + return; + } + zend_restore_error_handling(&error_handling TSRMLS_CC); + + if (!serialized_len) { + return; + } + +#if PHP_VERSION_ID < 70000 + ALLOC_INIT_ZVAL(props); +#endif + PHP_VAR_UNSERIALIZE_INIT(var_hash); + if (!php_var_unserialize(&props, (const unsigned char**) &serialized, (unsigned char*) serialized + serialized_len, &var_hash TSRMLS_CC)) { + zval_ptr_dtor(&props); + phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "%s unserialization failed", ZSTR_VAL(php_phongo_readconcern_ce->name)); + + PHP_VAR_UNSERIALIZE_DESTROY(var_hash); + return; + } + PHP_VAR_UNSERIALIZE_DESTROY(var_hash); + +#if PHP_VERSION_ID >= 70000 + php_phongo_readconcern_init_from_hash(intern, HASH_OF(&props) TSRMLS_CC); +#else + php_phongo_readconcern_init_from_hash(intern, HASH_OF(props) TSRMLS_CC); +#endif + zval_ptr_dtor(&props); +} /* }}} */ + /* {{{ MongoDB\Driver\ReadConcern function entries */ ZEND_BEGIN_ARG_INFO_EX(ai_ReadConcern___construct, 0, 0, 0) ZEND_ARG_INFO(0, level) @@ -206,6 +308,10 @@ ZEND_BEGIN_ARG_INFO_EX(ai_ReadConcern___set_state, 0, 0, 1) ZEND_ARG_ARRAY_INFO(0, properties, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(ai_ReadConcern_unserialize, 0, 0, 1) + ZEND_ARG_INFO(0, serialized) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(ai_ReadConcern_void, 0, 0, 0) ZEND_END_ARG_INFO() @@ -216,6 +322,8 @@ static zend_function_entry php_phongo_readconcern_me[] = { PHP_ME(ReadConcern, getLevel, ai_ReadConcern_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) PHP_ME(ReadConcern, isDefault, ai_ReadConcern_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) PHP_ME(ReadConcern, bsonSerialize, ai_ReadConcern_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) + PHP_ME(ReadConcern, serialize, ai_ReadConcern_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) + PHP_ME(ReadConcern, unserialize, ai_ReadConcern_unserialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) PHP_FE_END /* clang-format on */ }; @@ -287,9 +395,9 @@ void php_phongo_readconcern_init_ce(INIT_FUNC_ARGS) /* {{{ */ php_phongo_readconcern_ce = zend_register_internal_class(&ce TSRMLS_CC); php_phongo_readconcern_ce->create_object = php_phongo_readconcern_create_object; PHONGO_CE_FINAL(php_phongo_readconcern_ce); - PHONGO_CE_DISABLE_SERIALIZATION(php_phongo_readconcern_ce); zend_class_implements(php_phongo_readconcern_ce TSRMLS_CC, 1, php_phongo_serializable_ce); + zend_class_implements(php_phongo_readconcern_ce TSRMLS_CC, 1, zend_ce_serializable); memcpy(&php_phongo_handler_readconcern, phongo_get_std_object_handlers(), sizeof(zend_object_handlers)); php_phongo_handler_readconcern.get_debug_info = php_phongo_readconcern_get_debug_info; diff --git a/src/MongoDB/ReadPreference.c b/src/MongoDB/ReadPreference.c index d11414630..4d4ed3fb3 100644 --- a/src/MongoDB/ReadPreference.c +++ b/src/MongoDB/ReadPreference.c @@ -16,6 +16,12 @@ #include #include +#include +#if PHP_VERSION_ID >= 70000 +#include +#else +#include +#endif #ifdef HAVE_CONFIG_H #include "config.h" @@ -369,6 +375,26 @@ static PHP_METHOD(ReadPreference, getTagSets) } } /* }}} */ +static const char* php_phongo_readpreference_get_mode_string(mongoc_read_mode_t mode) /* {{{ */ +{ + switch (mode) { + case MONGOC_READ_PRIMARY: + return "primary"; + case MONGOC_READ_PRIMARY_PREFERRED: + return "primaryPreferred"; + case MONGOC_READ_SECONDARY: + return "secondary"; + case MONGOC_READ_SECONDARY_PREFERRED: + return "secondaryPreferred"; + case MONGOC_READ_NEAREST: + return "nearest"; + default: /* Do nothing */ + break; + } + + return NULL; +} /* }}} */ + static HashTable* php_phongo_readpreference_get_properties_hash(zval* object, bool is_debug TSRMLS_DC) /* {{{ */ { php_phongo_readpreference_t* intern; @@ -387,26 +413,7 @@ static HashTable* php_phongo_readpreference_get_properties_hash(zval* object, bo tags = mongoc_read_prefs_get_tags(intern->read_preference); mode = mongoc_read_prefs_get_mode(intern->read_preference); - - switch (mode) { - case MONGOC_READ_PRIMARY: - modeString = "primary"; - break; - case MONGOC_READ_PRIMARY_PREFERRED: - modeString = "primaryPreferred"; - break; - case MONGOC_READ_SECONDARY: - modeString = "secondary"; - break; - case MONGOC_READ_SECONDARY_PREFERRED: - modeString = "secondaryPreferred"; - break; - case MONGOC_READ_NEAREST: - modeString = "nearest"; - break; - default: /* Do nothing */ - break; - } + modeString = php_phongo_readpreference_get_mode_string(mode); if (modeString) { #if PHP_VERSION_ID >= 70000 @@ -471,6 +478,132 @@ static PHP_METHOD(ReadPreference, bsonSerialize) convert_to_object(return_value); } /* }}} */ +/* {{{ proto string MongoDB\Driver\ReadPreference::serialize() +*/ +static PHP_METHOD(ReadPreference, serialize) +{ + php_phongo_readpreference_t* intern; + ZVAL_RETVAL_TYPE retval; + php_serialize_data_t var_hash; + smart_str buf = { 0 }; + const char* modeString = NULL; + const bson_t* tags; + int64_t maxStalenessSeconds; + mongoc_read_mode_t mode; + + intern = Z_READPREFERENCE_OBJ_P(getThis()); + + if (zend_parse_parameters_none() == FAILURE) { + return; + } + + if (!intern->read_preference) { + return; + } + + tags = mongoc_read_prefs_get_tags(intern->read_preference); + mode = mongoc_read_prefs_get_mode(intern->read_preference); + modeString = php_phongo_readpreference_get_mode_string(mode); + maxStalenessSeconds = mongoc_read_prefs_get_max_staleness_seconds(intern->read_preference); + +#if PHP_VERSION_ID >= 70000 + array_init_size(&retval, 3); +#else + ALLOC_INIT_ZVAL(retval); + array_init_size(retval, 3); +#endif + + if (modeString) { +#if PHP_VERSION_ID >= 70000 + ADD_ASSOC_STRING(&retval, "mode", modeString); +#else + ADD_ASSOC_STRING(retval, "mode", modeString); +#endif + } + + if (!bson_empty0(tags)) { + php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER; + /* Use native arrays for debugging output */ + state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY; + state.map.document_type = PHONGO_TYPEMAP_NATIVE_ARRAY; + + php_phongo_bson_to_zval_ex(bson_get_data(tags), tags->len, &state); +#if PHP_VERSION_ID >= 70000 + ADD_ASSOC_ZVAL_EX(&retval, "tags", &state.zchild); +#else + ADD_ASSOC_ZVAL_EX(retval, "tags", state.zchild); +#endif + } + + if (maxStalenessSeconds != MONGOC_NO_MAX_STALENESS) { +#if PHP_VERSION_ID >= 70000 + ADD_ASSOC_LONG_EX(&retval, "maxStalenessSeconds", maxStalenessSeconds); +#else + ADD_ASSOC_LONG_EX(retval, "maxStalenessSeconds", maxStalenessSeconds); +#endif + } + + PHP_VAR_SERIALIZE_INIT(var_hash); + php_var_serialize(&buf, &retval, &var_hash TSRMLS_CC); + smart_str_0(&buf); + PHP_VAR_SERIALIZE_DESTROY(var_hash); + + PHONGO_RETVAL_SMART_STR(buf); + + smart_str_free(&buf); + zval_ptr_dtor(&retval); +} /* }}} */ + +/* {{{ proto void MongoDB\Driver\ReadPreference::unserialize(string $serialized) +*/ +static PHP_METHOD(ReadPreference, unserialize) +{ + php_phongo_readpreference_t* intern; + zend_error_handling error_handling; + char* serialized; + phongo_zpp_char_len serialized_len; +#if PHP_VERSION_ID >= 70000 + zval props; +#else + zval* props; +#endif + php_unserialize_data_t var_hash; + + intern = Z_READPREFERENCE_OBJ_P(getThis()); + + zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &serialized, &serialized_len) == FAILURE) { + zend_restore_error_handling(&error_handling TSRMLS_CC); + return; + } + zend_restore_error_handling(&error_handling TSRMLS_CC); + + if (!serialized_len) { + return; + } + +#if PHP_VERSION_ID < 70000 + ALLOC_INIT_ZVAL(props); +#endif + PHP_VAR_UNSERIALIZE_INIT(var_hash); + if (!php_var_unserialize(&props, (const unsigned char**) &serialized, (unsigned char*) serialized + serialized_len, &var_hash TSRMLS_CC)) { + zval_ptr_dtor(&props); + phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "%s unserialization failed", ZSTR_VAL(php_phongo_readpreference_ce->name)); + + PHP_VAR_UNSERIALIZE_DESTROY(var_hash); + return; + } + PHP_VAR_UNSERIALIZE_DESTROY(var_hash); + +#if PHP_VERSION_ID >= 70000 + php_phongo_readpreference_init_from_hash(intern, HASH_OF(&props) TSRMLS_CC); +#else + php_phongo_readpreference_init_from_hash(intern, HASH_OF(props) TSRMLS_CC); +#endif + zval_ptr_dtor(&props); +} /* }}} */ + /* {{{ MongoDB\Driver\ReadPreference function entries */ ZEND_BEGIN_ARG_INFO_EX(ai_ReadPreference___construct, 0, 0, 1) ZEND_ARG_INFO(0, mode) @@ -482,6 +615,10 @@ ZEND_BEGIN_ARG_INFO_EX(ai_ReadPreference___set_state, 0, 0, 1) ZEND_ARG_ARRAY_INFO(0, properties, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(ai_ReadPreference_unserialize, 0, 0, 1) + ZEND_ARG_INFO(0, serialized) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(ai_ReadPreference_void, 0, 0, 0) ZEND_END_ARG_INFO() @@ -493,6 +630,8 @@ static zend_function_entry php_phongo_readpreference_me[] = { PHP_ME(ReadPreference, getMode, ai_ReadPreference_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) PHP_ME(ReadPreference, getTagSets, ai_ReadPreference_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) PHP_ME(ReadPreference, bsonSerialize, ai_ReadPreference_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) + PHP_ME(ReadPreference, serialize, ai_ReadPreference_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) + PHP_ME(ReadPreference, unserialize, ai_ReadPreference_unserialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) PHP_FE_END /* clang-format on */ }; @@ -565,9 +704,9 @@ void php_phongo_readpreference_init_ce(INIT_FUNC_ARGS) /* {{{ */ php_phongo_readpreference_ce = zend_register_internal_class(&ce TSRMLS_CC); php_phongo_readpreference_ce->create_object = php_phongo_readpreference_create_object; PHONGO_CE_FINAL(php_phongo_readpreference_ce); - PHONGO_CE_DISABLE_SERIALIZATION(php_phongo_readpreference_ce); zend_class_implements(php_phongo_readpreference_ce TSRMLS_CC, 1, php_phongo_serializable_ce); + zend_class_implements(php_phongo_readpreference_ce TSRMLS_CC, 1, zend_ce_serializable); memcpy(&php_phongo_handler_readpreference, phongo_get_std_object_handlers(), sizeof(zend_object_handlers)); php_phongo_handler_readpreference.get_debug_info = php_phongo_readpreference_get_debug_info; diff --git a/src/MongoDB/WriteConcern.c b/src/MongoDB/WriteConcern.c index a9e2a651c..1fb8a86f9 100644 --- a/src/MongoDB/WriteConcern.c +++ b/src/MongoDB/WriteConcern.c @@ -16,6 +16,12 @@ #include #include +#include +#if PHP_VERSION_ID >= 70000 +#include +#else +#include +#endif #ifdef HAVE_CONFIG_H #include "config.h" @@ -406,6 +412,134 @@ static PHP_METHOD(WriteConcern, bsonSerialize) convert_to_object(return_value); } /* }}} */ +/* {{{ proto string MongoDB\Driver\WriteConcern::serialize() +*/ +static PHP_METHOD(WriteConcern, serialize) +{ + php_phongo_writeconcern_t* intern; + ZVAL_RETVAL_TYPE retval; + php_serialize_data_t var_hash; + smart_str buf = { 0 }; + const char* wtag; + int32_t w; + int32_t wtimeout; + + intern = Z_WRITECONCERN_OBJ_P(getThis()); + + if (zend_parse_parameters_none() == FAILURE) { + return; + } + + if (!intern->write_concern) { + return; + } + + wtag = mongoc_write_concern_get_wtag(intern->write_concern); + w = mongoc_write_concern_get_w(intern->write_concern); + /* Note: PHP currently enforces that wimeoutMS is a 32-bit integer, so + * casting will never truncate the value. This may change with PHPC-1411. */ + wtimeout = (int32_t) mongoc_write_concern_get_wtimeout_int64(intern->write_concern); + +#if PHP_VERSION_ID >= 70000 + array_init_size(&retval, 3); + + if (wtag) { + ADD_ASSOC_STRING(&retval, "w", wtag); + } else if (mongoc_write_concern_get_wmajority(intern->write_concern)) { + ADD_ASSOC_STRING(&retval, "w", PHONGO_WRITE_CONCERN_W_MAJORITY); + } else if (w != MONGOC_WRITE_CONCERN_W_DEFAULT) { + ADD_ASSOC_LONG_EX(&retval, "w", w); + } + + if (mongoc_write_concern_journal_is_set(intern->write_concern)) { + ADD_ASSOC_BOOL_EX(&retval, "j", mongoc_write_concern_get_journal(intern->write_concern)); + } + + if (wtimeout != 0) { + ADD_ASSOC_LONG_EX(&retval, "wtimeout", wtimeout); + } +#else + ALLOC_INIT_ZVAL(retval); + array_init_size(retval, 3); + + if (wtag) { + ADD_ASSOC_STRING(retval, "w", wtag); + } else if (mongoc_write_concern_get_wmajority(intern->write_concern)) { + ADD_ASSOC_STRING(retval, "w", PHONGO_WRITE_CONCERN_W_MAJORITY); + } else if (w != MONGOC_WRITE_CONCERN_W_DEFAULT) { + ADD_ASSOC_LONG_EX(retval, "w", w); + } + + if (mongoc_write_concern_journal_is_set(intern->write_concern)) { + ADD_ASSOC_BOOL_EX(retval, "j", mongoc_write_concern_get_journal(intern->write_concern)); + } + + if (wtimeout != 0) { + ADD_ASSOC_LONG_EX(retval, "wtimeout", wtimeout); + } +#endif + + PHP_VAR_SERIALIZE_INIT(var_hash); + php_var_serialize(&buf, &retval, &var_hash TSRMLS_CC); + smart_str_0(&buf); + PHP_VAR_SERIALIZE_DESTROY(var_hash); + + PHONGO_RETVAL_SMART_STR(buf); + + smart_str_free(&buf); + zval_ptr_dtor(&retval); +} /* }}} */ + +/* {{{ proto void MongoDB\Driver\WriteConcern::unserialize(string $serialized) +*/ +static PHP_METHOD(WriteConcern, unserialize) +{ + php_phongo_writeconcern_t* intern; + zend_error_handling error_handling; + char* serialized; + phongo_zpp_char_len serialized_len; +#if PHP_VERSION_ID >= 70000 + zval props; +#else + zval* props; +#endif + php_unserialize_data_t var_hash; + + intern = Z_WRITECONCERN_OBJ_P(getThis()); + + zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &serialized, &serialized_len) == FAILURE) { + zend_restore_error_handling(&error_handling TSRMLS_CC); + return; + } + zend_restore_error_handling(&error_handling TSRMLS_CC); + + if (!serialized_len) { + return; + } + +#if PHP_VERSION_ID < 70000 + ALLOC_INIT_ZVAL(props); +#endif + PHP_VAR_UNSERIALIZE_INIT(var_hash); + if (!php_var_unserialize(&props, (const unsigned char**) &serialized, (unsigned char*) serialized + serialized_len, &var_hash TSRMLS_CC)) { + zval_ptr_dtor(&props); + phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "%s unserialization failed", ZSTR_VAL(php_phongo_writeconcern_ce->name)); + + PHP_VAR_UNSERIALIZE_DESTROY(var_hash); + return; + } + PHP_VAR_UNSERIALIZE_DESTROY(var_hash); + +#if PHP_VERSION_ID >= 70000 + php_phongo_writeconcern_init_from_hash(intern, HASH_OF(&props) TSRMLS_CC); +#else + php_phongo_writeconcern_init_from_hash(intern, HASH_OF(props) TSRMLS_CC); +#endif + zval_ptr_dtor(&props); +} /* }}} */ + /* {{{ MongoDB\Driver\WriteConcern function entries */ ZEND_BEGIN_ARG_INFO_EX(ai_WriteConcern___construct, 0, 0, 1) ZEND_ARG_INFO(0, w) @@ -417,6 +551,10 @@ ZEND_BEGIN_ARG_INFO_EX(ai_WriteConcern___set_state, 0, 0, 1) ZEND_ARG_ARRAY_INFO(0, properties, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(ai_WriteConcern_unserialize, 0, 0, 1) + ZEND_ARG_INFO(0, serialized) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(ai_WriteConcern_void, 0, 0, 0) ZEND_END_ARG_INFO() @@ -429,6 +567,8 @@ static zend_function_entry php_phongo_writeconcern_me[] = { PHP_ME(WriteConcern, getJournal, ai_WriteConcern_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) PHP_ME(WriteConcern, isDefault, ai_WriteConcern_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) PHP_ME(WriteConcern, bsonSerialize, ai_WriteConcern_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) + PHP_ME(WriteConcern, serialize, ai_WriteConcern_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) + PHP_ME(WriteConcern, unserialize, ai_WriteConcern_unserialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) PHP_FE_END /* clang-format on */ }; @@ -500,9 +640,9 @@ void php_phongo_writeconcern_init_ce(INIT_FUNC_ARGS) /* {{{ */ php_phongo_writeconcern_ce = zend_register_internal_class(&ce TSRMLS_CC); php_phongo_writeconcern_ce->create_object = php_phongo_writeconcern_create_object; PHONGO_CE_FINAL(php_phongo_writeconcern_ce); - PHONGO_CE_DISABLE_SERIALIZATION(php_phongo_writeconcern_ce); zend_class_implements(php_phongo_writeconcern_ce TSRMLS_CC, 1, php_phongo_serializable_ce); + zend_class_implements(php_phongo_writeconcern_ce TSRMLS_CC, 1, zend_ce_serializable); memcpy(&php_phongo_handler_writeconcern, phongo_get_std_object_handlers(), sizeof(zend_object_handlers)); php_phongo_handler_writeconcern.get_debug_info = php_phongo_writeconcern_get_debug_info; diff --git a/tests/readConcern/readconcern-serialization-001.phpt b/tests/readConcern/readconcern-serialization-001.phpt new file mode 100644 index 000000000..6d48be36a --- /dev/null +++ b/tests/readConcern/readconcern-serialization-001.phpt @@ -0,0 +1,79 @@ +--TEST-- +MongoDB\Driver\ReadConcern serialization +--FILE-- + +===DONE=== + +--EXPECTF-- +object(MongoDB\Driver\ReadConcern)#%d (%d) { +} +bool(true) +C:26:"MongoDB\Driver\ReadConcern":0:{} +object(MongoDB\Driver\ReadConcern)#%d (%d) { +} + +object(MongoDB\Driver\ReadConcern)#%d (%d) { + ["level"]=> + string(12) "linearizable" +} +bool(true) +C:26:"MongoDB\Driver\ReadConcern":38:{a:1:{s:5:"level";s:12:"linearizable";}} +object(MongoDB\Driver\ReadConcern)#%d (%d) { + ["level"]=> + string(12) "linearizable" +} + +object(MongoDB\Driver\ReadConcern)#%d (%d) { + ["level"]=> + string(5) "local" +} +bool(true) +C:26:"MongoDB\Driver\ReadConcern":30:{a:1:{s:5:"level";s:5:"local";}} +object(MongoDB\Driver\ReadConcern)#%d (%d) { + ["level"]=> + string(5) "local" +} + +object(MongoDB\Driver\ReadConcern)#%d (%d) { + ["level"]=> + string(8) "majority" +} +bool(true) +C:26:"MongoDB\Driver\ReadConcern":33:{a:1:{s:5:"level";s:8:"majority";}} +object(MongoDB\Driver\ReadConcern)#%d (%d) { + ["level"]=> + string(8) "majority" +} + +object(MongoDB\Driver\ReadConcern)#%d (%d) { + ["level"]=> + string(9) "available" +} +bool(true) +C:26:"MongoDB\Driver\ReadConcern":34:{a:1:{s:5:"level";s:9:"available";}} +object(MongoDB\Driver\ReadConcern)#%d (%d) { + ["level"]=> + string(9) "available" +} + +===DONE=== diff --git a/tests/readPreference/readpreference-serialization-001.phpt b/tests/readPreference/readpreference-serialization-001.phpt new file mode 100644 index 000000000..71526c52d --- /dev/null +++ b/tests/readPreference/readpreference-serialization-001.phpt @@ -0,0 +1,187 @@ +--TEST-- +MongoDB\Driver\ReadPreference serialization +--FILE-- + 'ny']]), + new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY, [['dc' => 'ny'], ['dc' => 'sf', 'use' => 'reporting'], []]), + new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY, null, ['maxStalenessSeconds' => 1000]), +]; + +foreach ($tests as $test) { + var_dump($test); + var_dump($test instanceof Serializable); + echo $s = serialize($test), "\n"; + var_dump(unserialize($s)); + echo "\n"; +} + +?> +===DONE=== + +--EXPECTF-- +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(7) "primary" +} +bool(true) +C:29:"MongoDB\Driver\ReadPreference":31:{a:1:{s:4:"mode";s:7:"primary";}} +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(7) "primary" +} + +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(16) "primaryPreferred" +} +bool(true) +C:29:"MongoDB\Driver\ReadPreference":41:{a:1:{s:4:"mode";s:16:"primaryPreferred";}} +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(16) "primaryPreferred" +} + +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(9) "secondary" +} +bool(true) +C:29:"MongoDB\Driver\ReadPreference":33:{a:1:{s:4:"mode";s:9:"secondary";}} +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(9) "secondary" +} + +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(18) "secondaryPreferred" +} +bool(true) +C:29:"MongoDB\Driver\ReadPreference":43:{a:1:{s:4:"mode";s:18:"secondaryPreferred";}} +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(18) "secondaryPreferred" +} + +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(7) "nearest" +} +bool(true) +C:29:"MongoDB\Driver\ReadPreference":31:{a:1:{s:4:"mode";s:7:"nearest";}} +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(7) "nearest" +} + +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(9) "secondary" +} +bool(true) +C:29:"MongoDB\Driver\ReadPreference":33:{a:1:{s:4:"mode";s:9:"secondary";}} +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(9) "secondary" +} + +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(9) "secondary" + ["tags"]=> + array(1) { + [0]=> + object(stdClass)#%d (%d) { + ["dc"]=> + string(2) "ny" + } + } +} +bool(true) +C:29:"MongoDB\Driver\ReadPreference":78:{a:2:{s:4:"mode";s:9:"secondary";s:4:"tags";a:1:{i:0;a:1:{s:2:"dc";s:2:"ny";}}}} +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(9) "secondary" + ["tags"]=> + array(1) { + [0]=> + object(stdClass)#%d (%d) { + ["dc"]=> + string(2) "ny" + } + } +} + +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(9) "secondary" + ["tags"]=> + array(3) { + [0]=> + object(stdClass)#%d (%d) { + ["dc"]=> + string(2) "ny" + } + [1]=> + object(stdClass)#%d (%d) { + ["dc"]=> + string(2) "sf" + ["use"]=> + string(9) "reporting" + } + [2]=> + object(stdClass)#%d (%d) { + } + } +} +bool(true) +C:29:"MongoDB\Driver\ReadPreference":142:{a:2:{s:4:"mode";s:9:"secondary";s:4:"tags";a:3:{i:0;a:1:{s:2:"dc";s:2:"ny";}i:1;a:2:{s:2:"dc";s:2:"sf";s:3:"use";s:9:"reporting";}i:2;a:0:{}}}} +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(9) "secondary" + ["tags"]=> + array(3) { + [0]=> + object(stdClass)#%d (%d) { + ["dc"]=> + string(2) "ny" + } + [1]=> + object(stdClass)#%d (%d) { + ["dc"]=> + string(2) "sf" + ["use"]=> + string(9) "reporting" + } + [2]=> + object(stdClass)#%d (%d) { + } + } +} + +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(9) "secondary" + ["maxStalenessSeconds"]=> + int(1000) +} +bool(true) +C:29:"MongoDB\Driver\ReadPreference":67:{a:2:{s:4:"mode";s:9:"secondary";s:19:"maxStalenessSeconds";i:1000;}} +object(MongoDB\Driver\ReadPreference)#%d (%d) { + ["mode"]=> + string(9) "secondary" + ["maxStalenessSeconds"]=> + int(1000) +} + +===DONE=== diff --git a/tests/writeConcern/writeconcern-bsonserialize-001.phpt b/tests/writeConcern/writeconcern-bsonserialize-001.phpt index 569e99e8c..6ef80103e 100644 --- a/tests/writeConcern/writeconcern-bsonserialize-001.phpt +++ b/tests/writeConcern/writeconcern-bsonserialize-001.phpt @@ -18,7 +18,7 @@ $tests = [ new MongoDB\Driver\WriteConcern(1, 1000), new MongoDB\Driver\WriteConcern(1, 1000, true), new MongoDB\Driver\WriteConcern(-2, 0, true), - // Note: wtimeout is only applicable applies for w > 1 + // Note: wtimeout is only applicable for w > 1 new MongoDB\Driver\WriteConcern(-2, 1000), ]; diff --git a/tests/writeConcern/writeconcern-bsonserialize-002.phpt b/tests/writeConcern/writeconcern-bsonserialize-002.phpt index 7f95ae3d8..62763eb25 100644 --- a/tests/writeConcern/writeconcern-bsonserialize-002.phpt +++ b/tests/writeConcern/writeconcern-bsonserialize-002.phpt @@ -18,7 +18,7 @@ $tests = [ new MongoDB\Driver\WriteConcern(1, 1000), new MongoDB\Driver\WriteConcern(1, 1000, true), new MongoDB\Driver\WriteConcern(-2, 0, true), - // Note: wtimeout is only applicable applies for w > 1 + // Note: wtimeout is only applicable for w > 1 new MongoDB\Driver\WriteConcern(-2, 1000), ]; diff --git a/tests/writeConcern/writeconcern-debug-003.phpt b/tests/writeConcern/writeconcern-debug-003.phpt index 940445fa2..72fcab70f 100644 --- a/tests/writeConcern/writeconcern-debug-003.phpt +++ b/tests/writeConcern/writeconcern-debug-003.phpt @@ -18,7 +18,7 @@ $tests = [ new MongoDB\Driver\WriteConcern(1, 1000), new MongoDB\Driver\WriteConcern(1, 1000, true), new MongoDB\Driver\WriteConcern(-2, 0, true), - // Note: wtimeout is only applicable applies for w > 1 + // Note: wtimeout is only applicable for w > 1 new MongoDB\Driver\WriteConcern(-2, 1000), ]; diff --git a/tests/writeConcern/writeconcern-serialization-001.phpt b/tests/writeConcern/writeconcern-serialization-001.phpt new file mode 100644 index 000000000..ab76babc6 --- /dev/null +++ b/tests/writeConcern/writeconcern-serialization-001.phpt @@ -0,0 +1,192 @@ +--TEST-- +MongoDB\Driver\WriteConcern serialization +--FILE-- + 1 + new MongoDB\Driver\WriteConcern(-2, 1000), +]; + +foreach ($tests as $test) { + var_dump($test); + var_dump($test instanceof Serializable); + echo $s = serialize($test), "\n"; + var_dump(unserialize($s)); + echo "\n"; +} + +?> +===DONE=== + +--EXPECTF-- +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + string(8) "majority" +} +bool(true) +C:27:"MongoDB\Driver\WriteConcern":29:{a:1:{s:1:"w";s:8:"majority";}} +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + string(8) "majority" +} + +object(MongoDB\Driver\WriteConcern)#%d (%d) { +} +bool(true) +C:27:"MongoDB\Driver\WriteConcern":6:{a:0:{}} +object(MongoDB\Driver\WriteConcern)#%d (%d) { +} + +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + int(-1) +} +bool(true) +C:27:"MongoDB\Driver\WriteConcern":19:{a:1:{s:1:"w";i:-1;}} +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + int(-1) +} + +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + int(0) +} +bool(true) +C:27:"MongoDB\Driver\WriteConcern":18:{a:1:{s:1:"w";i:0;}} +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + int(0) +} + +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + int(1) +} +bool(true) +C:27:"MongoDB\Driver\WriteConcern":18:{a:1:{s:1:"w";i:1;}} +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + int(1) +} + +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + string(8) "majority" +} +bool(true) +C:27:"MongoDB\Driver\WriteConcern":29:{a:1:{s:1:"w";s:8:"majority";}} +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + string(8) "majority" +} + +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + string(3) "tag" +} +bool(true) +C:27:"MongoDB\Driver\WriteConcern":24:{a:1:{s:1:"w";s:3:"tag";}} +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + string(3) "tag" +} + +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + int(1) +} +bool(true) +C:27:"MongoDB\Driver\WriteConcern":18:{a:1:{s:1:"w";i:1;}} +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + int(1) +} + +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + int(1) + ["j"]=> + bool(false) +} +bool(true) +C:27:"MongoDB\Driver\WriteConcern":30:{a:2:{s:1:"w";i:1;s:1:"j";b:0;}} +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + int(1) + ["j"]=> + bool(false) +} + +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + int(1) + ["wtimeout"]=> + int(1000) +} +bool(true) +C:27:"MongoDB\Driver\WriteConcern":40:{a:2:{s:1:"w";i:1;s:8:"wtimeout";i:1000;}} +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + int(1) + ["wtimeout"]=> + int(1000) +} + +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + int(1) + ["j"]=> + bool(true) + ["wtimeout"]=> + int(1000) +} +bool(true) +C:27:"MongoDB\Driver\WriteConcern":52:{a:3:{s:1:"w";i:1;s:1:"j";b:1;s:8:"wtimeout";i:1000;}} +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["w"]=> + int(1) + ["j"]=> + bool(true) + ["wtimeout"]=> + int(1000) +} + +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["j"]=> + bool(true) +} +bool(true) +C:27:"MongoDB\Driver\WriteConcern":18:{a:1:{s:1:"j";b:1;}} +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["j"]=> + bool(true) +} + +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["wtimeout"]=> + int(1000) +} +bool(true) +C:27:"MongoDB\Driver\WriteConcern":28:{a:1:{s:8:"wtimeout";i:1000;}} +object(MongoDB\Driver\WriteConcern)#%d (%d) { + ["wtimeout"]=> + int(1000) +} + +===DONE=== diff --git a/tests/writeConcern/writeconcern-var_export-001.phpt b/tests/writeConcern/writeconcern-var_export-001.phpt index 37374b642..a195538a5 100644 --- a/tests/writeConcern/writeconcern-var_export-001.phpt +++ b/tests/writeConcern/writeconcern-var_export-001.phpt @@ -18,7 +18,7 @@ $tests = [ new MongoDB\Driver\WriteConcern(1, 1000), new MongoDB\Driver\WriteConcern(1, 1000, true), new MongoDB\Driver\WriteConcern(-2, 0, true), - // Note: wtimeout is only applicable applies for w > 1 + // Note: wtimeout is only applicable for w > 1 new MongoDB\Driver\WriteConcern(-2, 1000), ];