Skip to content

Commit 4a568e3

Browse files
committed
PHPC-1206: Add __set_state() support for ReadConcern
1 parent cc317b3 commit 4a568e3

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
lines changed

src/MongoDB/ReadConcern.c

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,48 @@
2626

2727
zend_class_entry* php_phongo_readconcern_ce;
2828

29+
/* Initialize the object from a HashTable and return whether it was successful.
30+
* An exception will be thrown on error. */
31+
static bool php_phongo_readconcern_init_from_hash(php_phongo_readconcern_t* intern, HashTable* props TSRMLS_DC) /* {{{ */
32+
{
33+
#if PHP_VERSION_ID >= 70000
34+
zval* level;
35+
36+
intern->read_concern = mongoc_read_concern_new();
37+
38+
if ((level = zend_hash_str_find(props, "level", sizeof("level") - 1))) {
39+
if (Z_TYPE_P(level) == IS_STRING) {
40+
mongoc_read_concern_set_level(intern->read_concern, Z_STRVAL_P(level));
41+
return true;
42+
}
43+
44+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s initialization requires \"level\" string field", ZSTR_VAL(php_phongo_readconcern_ce->name));
45+
goto failure;
46+
}
47+
#else
48+
zval** level;
49+
50+
intern->read_concern = mongoc_read_concern_new();
51+
52+
if (zend_hash_find(props, "level", sizeof("level"), (void**) &level) == SUCCESS) {
53+
if (Z_TYPE_PP(level) == IS_STRING) {
54+
mongoc_read_concern_set_level(intern->read_concern, Z_STRVAL_PP(level));
55+
return true;
56+
}
57+
58+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s initialization requires \"level\" string field", ZSTR_VAL(php_phongo_readconcern_ce->name));
59+
goto failure;
60+
}
61+
#endif
62+
63+
return true;
64+
65+
failure:
66+
mongoc_read_concern_destroy(intern->read_concern);
67+
intern->read_concern = NULL;
68+
return false;
69+
} /* }}} */
70+
2971
/* {{{ proto void MongoDB\Driver\ReadConcern::__construct([string $level])
3072
Constructs a new ReadConcern */
3173
static PHP_METHOD(ReadConcern, __construct)
@@ -51,6 +93,26 @@ static PHP_METHOD(ReadConcern, __construct)
5193
}
5294
} /* }}} */
5395

96+
/* {{{ proto void MongoDB\BSON\ReadConcern::__set_state(array $properties)
97+
*/
98+
static PHP_METHOD(ReadConcern, __set_state)
99+
{
100+
php_phongo_readconcern_t* intern;
101+
HashTable* props;
102+
zval* array;
103+
104+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) == FAILURE) {
105+
RETURN_FALSE;
106+
}
107+
108+
object_init_ex(return_value, php_phongo_readconcern_ce);
109+
110+
intern = Z_READCONCERN_OBJ_P(return_value);
111+
props = Z_ARRVAL_P(array);
112+
113+
php_phongo_readconcern_init_from_hash(intern, props TSRMLS_CC);
114+
} /* }}} */
115+
54116
/* {{{ proto string|null MongoDB\Driver\ReadConcern::getLevel()
55117
Returns the ReadConcern "level" option */
56118
static PHP_METHOD(ReadConcern, getLevel)
@@ -140,12 +202,17 @@ ZEND_BEGIN_ARG_INFO_EX(ai_ReadConcern___construct, 0, 0, 0)
140202
ZEND_ARG_INFO(0, level)
141203
ZEND_END_ARG_INFO()
142204

205+
ZEND_BEGIN_ARG_INFO_EX(ai_ReadConcern___set_state, 0, 0, 1)
206+
ZEND_ARG_ARRAY_INFO(0, properties, 0)
207+
ZEND_END_ARG_INFO()
208+
143209
ZEND_BEGIN_ARG_INFO_EX(ai_ReadConcern_void, 0, 0, 0)
144210
ZEND_END_ARG_INFO()
145211

146212
static zend_function_entry php_phongo_readconcern_me[] = {
147213
/* clang-format off */
148214
PHP_ME(ReadConcern, __construct, ai_ReadConcern___construct, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
215+
PHP_ME(ReadConcern, __set_state, ai_ReadConcern___set_state, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
149216
PHP_ME(ReadConcern, getLevel, ai_ReadConcern_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
150217
PHP_ME(ReadConcern, isDefault, ai_ReadConcern_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
151218
PHP_ME(ReadConcern, bsonSerialize, ai_ReadConcern_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
@@ -175,7 +242,7 @@ static void php_phongo_readconcern_free_object(phongo_free_object_arg* object TS
175242
#if PHP_VERSION_ID < 70000
176243
efree(intern);
177244
#endif
178-
} /* }}} */
245+
}
179246

180247
static phongo_create_object_retval php_phongo_readconcern_create_object(zend_class_entry* class_type TSRMLS_DC) /* {{{ */
181248
{
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--TEST--
2+
MongoDB\Driver\ReadConcern::__set_state()
3+
--FILE--
4+
<?php
5+
6+
$tests = [
7+
MongoDB\Driver\ReadConcern::AVAILABLE,
8+
MongoDB\Driver\ReadConcern::LINEARIZABLE,
9+
MongoDB\Driver\ReadConcern::LOCAL,
10+
MongoDB\Driver\ReadConcern::MAJORITY,
11+
];
12+
13+
foreach ($tests as $level) {
14+
var_export(MongoDB\Driver\ReadConcern::__set_state([
15+
'level' => $level,
16+
]));
17+
echo "\n\n";
18+
}
19+
20+
/* Test with level unset */
21+
var_export(MongoDB\Driver\ReadConcern::__set_state([
22+
]));
23+
echo "\n\n";
24+
25+
?>
26+
===DONE===
27+
<?php exit(0); ?>
28+
--EXPECTF--
29+
MongoDB\Driver\ReadConcern::__set_state(array(
30+
%w'level' => 'available',
31+
))
32+
33+
MongoDB\Driver\ReadConcern::__set_state(array(
34+
%w'level' => 'linearizable',
35+
))
36+
37+
MongoDB\Driver\ReadConcern::__set_state(array(
38+
%w'level' => 'local',
39+
))
40+
41+
MongoDB\Driver\ReadConcern::__set_state(array(
42+
%w'level' => 'majority',
43+
))
44+
45+
MongoDB\Driver\ReadConcern::__set_state(array(
46+
))
47+
48+
===DONE===
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
MongoDB\Driver\ReadConcern::__set_state() requires "level" string field
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/tools.php';
7+
8+
echo throws(function() {
9+
MongoDB\Driver\ReadConcern::__set_state(['level' => 0]);
10+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
11+
12+
?>
13+
===DONE===
14+
<?php exit(0); ?>
15+
--EXPECT--
16+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
17+
MongoDB\Driver\ReadConcern initialization requires "level" string field
18+
===DONE===

0 commit comments

Comments
 (0)