Skip to content

Commit d92d942

Browse files
committed
PHPC-850: var_export() support for WriteConcern
1 parent a801b8c commit d92d942

File tree

3 files changed

+175
-9
lines changed

3 files changed

+175
-9
lines changed

php_phongo_structs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ typedef struct {
119119

120120
typedef struct {
121121
PHONGO_ZEND_OBJECT_PRE
122+
HashTable* properties;
122123
mongoc_write_concern_t* write_concern;
123124
PHONGO_ZEND_OBJECT_POST
124125
} php_phongo_writeconcern_t;

src/MongoDB/WriteConcern.c

Lines changed: 99 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,102 @@ static PHP_METHOD(WriteConcern, isDefault)
179179
RETURN_BOOL(mongoc_write_concern_is_default(intern->write_concern));
180180
} /* }}} */
181181

182+
static HashTable* php_phongo_write_concern_get_properties_hash(zval* object, bool is_debug TSRMLS_DC) /* {{{ */
183+
{
184+
php_phongo_writeconcern_t* intern;
185+
HashTable* props;
186+
const char* wtag;
187+
int32_t w;
188+
int32_t wtimeout;
189+
190+
intern = Z_WRITECONCERN_OBJ_P(object);
191+
192+
PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, 4);
193+
194+
if (!intern->write_concern) {
195+
return props;
196+
}
197+
198+
wtag = mongoc_write_concern_get_wtag(intern->write_concern);
199+
w = mongoc_write_concern_get_w(intern->write_concern);
200+
wtimeout = mongoc_write_concern_get_wtimeout(intern->write_concern);
201+
202+
#if PHP_VERSION_ID >= 70000
203+
{
204+
zval z_w;
205+
206+
if (wtag) {
207+
ZVAL_STRING(&z_w, wtag);
208+
zend_hash_str_update(props, "w", sizeof("w") - 1, &z_w);
209+
} else if (mongoc_write_concern_get_wmajority(intern->write_concern)) {
210+
ZVAL_STRING(&z_w, PHONGO_WRITE_CONCERN_W_MAJORITY);
211+
zend_hash_str_update(props, "w", sizeof("w") - 1, &z_w);
212+
} else if (w != MONGOC_WRITE_CONCERN_W_DEFAULT) {
213+
ZVAL_LONG(&z_w, w);
214+
zend_hash_str_update(props, "w", sizeof("w") - 1, &z_w);
215+
}
216+
217+
if (mongoc_write_concern_journal_is_set(intern->write_concern)) {
218+
zval z_j;
219+
220+
ZVAL_BOOL(&z_j, mongoc_write_concern_get_journal(intern->write_concern));
221+
zend_hash_str_update(props, "j", sizeof("j") - 1, &z_j);
222+
}
223+
224+
if (wtimeout != 0) {
225+
zval z_wtimeout;
226+
227+
ZVAL_LONG(&z_wtimeout, wtimeout);
228+
zend_hash_str_update(props, "wtimeout", sizeof("wtimeout") - 1, &z_wtimeout);
229+
}
230+
#else
231+
{
232+
zval* z_w;
233+
234+
if (wtag) {
235+
MAKE_STD_ZVAL(z_w);
236+
ZVAL_STRING(z_w, wtag, 1);
237+
zend_hash_update(props, "w", sizeof("w"), &z_w, sizeof(z_w), NULL);
238+
} else if (mongoc_write_concern_get_wmajority(intern->write_concern)) {
239+
MAKE_STD_ZVAL(z_w);
240+
ZVAL_STRING(z_w, PHONGO_WRITE_CONCERN_W_MAJORITY, 1);
241+
zend_hash_update(props, "w", sizeof("w"), &z_w, sizeof(z_w), NULL);
242+
} else if (w != MONGOC_WRITE_CONCERN_W_DEFAULT) {
243+
MAKE_STD_ZVAL(z_w);
244+
ZVAL_LONG(z_w, w);
245+
zend_hash_update(props, "w", sizeof("w"), &z_w, sizeof(z_w), NULL);
246+
}
247+
248+
if (mongoc_write_concern_journal_is_set(intern->write_concern)) {
249+
zval* z_j;
250+
251+
MAKE_STD_ZVAL(z_j);
252+
ZVAL_BOOL(z_j, mongoc_write_concern_get_journal(intern->write_concern));
253+
zend_hash_update(props, "j", sizeof("j"), &z_j, sizeof(z_j), NULL);
254+
}
255+
256+
if (wtimeout != 0) {
257+
zval* z_wtimeout;
258+
259+
MAKE_STD_ZVAL(z_wtimeout);
260+
ZVAL_LONG(z_wtimeout, wtimeout);
261+
zend_hash_update(props, "wtimeout", sizeof("wtimeout"), &z_wtimeout, sizeof(z_wtimeout), NULL);
262+
}
263+
#endif
264+
}
265+
266+
return props;
267+
} /* }}} */
268+
182269
/* {{{ proto array MongoDB\Driver\WriteConcern::bsonSerialize()
183270
*/
184271
static PHP_METHOD(WriteConcern, bsonSerialize)
185272
{
186-
const mongoc_write_concern_t* write_concern = phongo_write_concern_from_zval(getThis() TSRMLS_CC);
187-
188273
if (zend_parse_parameters_none() == FAILURE) {
189274
return;
190275
}
191276

192-
php_phongo_write_concern_to_zval(return_value, write_concern);
277+
ZVAL_ARR(return_value, php_phongo_write_concern_get_properties_hash(getThis(), true TSRMLS_CC));
193278
convert_to_object(return_value);
194279
} /* }}} */
195280

@@ -225,6 +310,11 @@ static void php_phongo_writeconcern_free_object(phongo_free_object_arg* object T
225310

226311
zend_object_std_dtor(&intern->std TSRMLS_CC);
227312

313+
if (intern->properties) {
314+
zend_hash_destroy(intern->properties);
315+
FREE_HASHTABLE(intern->properties);
316+
}
317+
228318
if (intern->write_concern) {
229319
mongoc_write_concern_destroy(intern->write_concern);
230320
}
@@ -260,15 +350,14 @@ static phongo_create_object_retval php_phongo_writeconcern_create_object(zend_cl
260350

261351
static HashTable* php_phongo_writeconcern_get_debug_info(zval* object, int* is_temp TSRMLS_DC) /* {{{ */
262352
{
263-
zval retval = ZVAL_STATIC_INIT;
264-
const mongoc_write_concern_t* write_concern = phongo_write_concern_from_zval(object TSRMLS_CC);
265-
266353
*is_temp = 1;
267-
php_phongo_write_concern_to_zval(&retval, write_concern);
354+
return php_phongo_write_concern_get_properties_hash(object, true TSRMLS_CC);
355+
} /* }}} */
268356

269-
return Z_ARRVAL(retval);
357+
static HashTable* php_phongo_writeconcern_get_properties(zval* object TSRMLS_DC) /* {{{ */
358+
{
359+
return php_phongo_write_concern_get_properties_hash(object, false TSRMLS_CC);
270360
} /* }}} */
271-
/* }}} */
272361

273362
void php_phongo_writeconcern_init_ce(INIT_FUNC_ARGS) /* {{{ */
274363
{
@@ -284,6 +373,7 @@ void php_phongo_writeconcern_init_ce(INIT_FUNC_ARGS) /* {{{ */
284373

285374
memcpy(&php_phongo_handler_writeconcern, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
286375
php_phongo_handler_writeconcern.get_debug_info = php_phongo_writeconcern_get_debug_info;
376+
php_phongo_handler_writeconcern.get_properties = php_phongo_writeconcern_get_properties;
287377
#if PHP_VERSION_ID >= 70000
288378
php_phongo_handler_writeconcern.free_obj = php_phongo_writeconcern_free_object;
289379
php_phongo_handler_writeconcern.offset = XtOffsetOf(php_phongo_writeconcern_t, std);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern::bsonSerialize()
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/tools.php';
7+
8+
$tests = [
9+
new MongoDB\Driver\WriteConcern(-3), // MONGOC_WRITE_CONCERN_W_MAJORITY
10+
new MongoDB\Driver\WriteConcern(-2), // MONGOC_WRITE_CONCERN_W_DEFAULT
11+
new MongoDB\Driver\WriteConcern(-1),
12+
new MongoDB\Driver\WriteConcern(0),
13+
new MongoDB\Driver\WriteConcern(1),
14+
new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY),
15+
new MongoDB\Driver\WriteConcern('tag'),
16+
new MongoDB\Driver\WriteConcern(1, 0),
17+
new MongoDB\Driver\WriteConcern(1, 0, false),
18+
new MongoDB\Driver\WriteConcern(1, 1000),
19+
new MongoDB\Driver\WriteConcern(1, 1000, true),
20+
new MongoDB\Driver\WriteConcern(-2, 0, true),
21+
// Note: wtimeout is only applicable applies for w > 1
22+
new MongoDB\Driver\WriteConcern(-2, 1000),
23+
];
24+
25+
foreach ($tests as $test) {
26+
echo var_export($test, true), "\n";
27+
}
28+
29+
?>
30+
===DONE===
31+
<?php exit(0); ?>
32+
--EXPECT--
33+
MongoDB\Driver\WriteConcern::__set_state(array(
34+
'w' => 'majority',
35+
))
36+
MongoDB\Driver\WriteConcern::__set_state(array(
37+
))
38+
MongoDB\Driver\WriteConcern::__set_state(array(
39+
'w' => -1,
40+
))
41+
MongoDB\Driver\WriteConcern::__set_state(array(
42+
'w' => 0,
43+
))
44+
MongoDB\Driver\WriteConcern::__set_state(array(
45+
'w' => 1,
46+
))
47+
MongoDB\Driver\WriteConcern::__set_state(array(
48+
'w' => 'majority',
49+
))
50+
MongoDB\Driver\WriteConcern::__set_state(array(
51+
'w' => 'tag',
52+
))
53+
MongoDB\Driver\WriteConcern::__set_state(array(
54+
'w' => 1,
55+
))
56+
MongoDB\Driver\WriteConcern::__set_state(array(
57+
'w' => 1,
58+
'j' => false,
59+
))
60+
MongoDB\Driver\WriteConcern::__set_state(array(
61+
'w' => 1,
62+
'wtimeout' => 1000,
63+
))
64+
MongoDB\Driver\WriteConcern::__set_state(array(
65+
'w' => 1,
66+
'j' => true,
67+
'wtimeout' => 1000,
68+
))
69+
MongoDB\Driver\WriteConcern::__set_state(array(
70+
'j' => true,
71+
))
72+
MongoDB\Driver\WriteConcern::__set_state(array(
73+
'wtimeout' => 1000,
74+
))
75+
===DONE===

0 commit comments

Comments
 (0)