Skip to content

Commit 833b57d

Browse files
committed
Implement object compare handler for WriteConcern
1 parent 034974e commit 833b57d

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

src/MongoDB/WriteConcern.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,75 @@ static HashTable* phongo_writeconcern_get_properties(zend_object* object)
401401
return phongo_writeconcern_get_properties_hash(object, false, false, false);
402402
}
403403

404+
/**
405+
* Note: only equality comparison is supported for objects.
406+
* Inequality will always return -1, making sorting arbitrary.
407+
*/
408+
static int phongo_writeconcern_compare_objects(zval* o1, zval* o2)
409+
{
410+
ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2);
411+
412+
phongo_writeconcern_t* intern1;
413+
phongo_writeconcern_t* intern2;
414+
415+
const mongoc_write_concern_t* wc1 = NULL;
416+
const mongoc_write_concern_t* wc2 = NULL;
417+
418+
intern1 = Z_WRITECONCERN_OBJ_P(o1);
419+
intern2 = Z_WRITECONCERN_OBJ_P(o2);
420+
421+
if (intern1 && intern1->write_concern) {
422+
wc1 = intern1->write_concern;
423+
}
424+
425+
if (intern2 && intern2->write_concern) {
426+
wc2 = intern2->write_concern;
427+
}
428+
429+
if (wc1 == wc2) {
430+
return 0;
431+
}
432+
433+
if (wc1 == NULL || wc2 == NULL) {
434+
return -1;
435+
}
436+
437+
if (mongoc_write_concern_get_w(wc1) != mongoc_write_concern_get_w(wc2)) {
438+
return -1;
439+
}
440+
441+
const char* wtag1 = NULL;
442+
const char* wtag2 = NULL;
443+
444+
wtag1 = mongoc_write_concern_get_wtag(wc1);
445+
wtag2 = mongoc_write_concern_get_wtag(wc2);
446+
447+
if (wtag1 == wtag2) {
448+
return 0;
449+
}
450+
451+
if (wtag1 == NULL || wtag2 == NULL) {
452+
return -1;
453+
}
454+
455+
if (strcmp(wtag1, wtag2) != 0) {
456+
return -1;
457+
}
458+
459+
if (mongoc_write_concern_get_journal(wc1) != mongoc_write_concern_get_journal(wc2)) {
460+
return -1;
461+
}
462+
463+
return 0;
464+
}
465+
404466
void phongo_writeconcern_init_ce(INIT_FUNC_ARGS)
405467
{
406468
phongo_writeconcern_ce = register_class_MongoDB_Driver_WriteConcern(phongo_serializable_ce);
407469
phongo_writeconcern_ce->create_object = phongo_writeconcern_create_object;
408470

409471
memcpy(&phongo_handler_writeconcern, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
472+
phongo_handler_writeconcern.compare = phongo_writeconcern_compare_objects;
410473
phongo_handler_writeconcern.get_debug_info = phongo_writeconcern_get_debug_info;
411474
phongo_handler_writeconcern.get_properties = phongo_writeconcern_get_properties;
412475
phongo_handler_writeconcern.free_obj = phongo_writeconcern_free_object;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern equality comparison
3+
--FILE--
4+
<?php
5+
6+
var_dump(new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY) == new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY));
7+
var_dump(new MongoDB\Driver\WriteConcern("foo") == new MongoDB\Driver\WriteConcern("foo"));
8+
var_dump(new MongoDB\Driver\WriteConcern(1, 0, 1) == new MongoDB\Driver\WriteConcern(1, 0, 1));
9+
10+
// False for anything else
11+
var_dump(new MongoDB\Driver\WriteConcern(1) == new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY));
12+
var_dump(new MongoDB\Driver\WriteConcern("foo") == new MongoDB\Driver\WriteConcern("bar"));
13+
var_dump(new MongoDB\Driver\WriteConcern("foo", 0, 1) == new MongoDB\Driver\WriteConcern("foo", 0, 0));
14+
15+
// Object comparison fallback if one value is not a WriteConcern
16+
var_dump(new MongoDB\Driver\WriteConcern(1) > new MongoDB\BSON\Int64('8589934592'));
17+
18+
?>
19+
===DONE===
20+
<?php exit(0); ?>
21+
--EXPECT--
22+
bool(true)
23+
bool(true)
24+
bool(true)
25+
bool(false)
26+
bool(false)
27+
bool(false)
28+
bool(false)
29+
===DONE===

0 commit comments

Comments
 (0)