Skip to content

Commit 7a12b86

Browse files
committed
PHPC-654: Timestamp comparison handler
1 parent bdb5f3c commit 7a12b86

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/BSON/Timestamp.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,25 @@ phongo_create_object_retval php_phongo_timestamp_create_object(zend_class_entry
427427
#endif
428428
} /* }}} */
429429

430+
static int php_phongo_timestamp_compare_objects(zval *o1, zval *o2 TSRMLS_DC) /* {{{ */
431+
{
432+
php_phongo_timestamp_t *intern1, *intern2;
433+
434+
intern1 = Z_TIMESTAMP_OBJ_P(o1);
435+
intern2 = Z_TIMESTAMP_OBJ_P(o2);
436+
437+
/* MongoDB compares the timestamp before the increment. */
438+
if (intern1->timestamp != intern2->timestamp) {
439+
return intern1->timestamp < intern2->timestamp ? -1 : 1;
440+
}
441+
442+
if (intern1->increment != intern2->increment) {
443+
return intern1->increment < intern2->increment ? -1 : 1;
444+
}
445+
446+
return 0;
447+
} /* }}} */
448+
430449
HashTable *php_phongo_timestamp_get_properties(zval *object TSRMLS_DC) /* {{{ */
431450
{
432451
php_phongo_timestamp_t *intern;
@@ -490,6 +509,7 @@ PHP_MINIT_FUNCTION(Timestamp)
490509
zend_class_implements(php_phongo_timestamp_ce TSRMLS_CC, 1, zend_ce_serializable);
491510

492511
memcpy(&php_phongo_handler_timestamp, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
512+
php_phongo_handler_timestamp.compare_objects = php_phongo_timestamp_compare_objects;
493513
php_phongo_handler_timestamp.get_properties = php_phongo_timestamp_get_properties;
494514
#if PHP_VERSION_ID >= 70000
495515
php_phongo_handler_timestamp.free_obj = php_phongo_timestamp_free_object;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
MongoDB\BSON\Timestamp comparisons
3+
--FILE--
4+
<?php
5+
6+
var_dump(new MongoDB\BSON\Timestamp(1234, 5678) == new MongoDB\BSON\Timestamp(1234, 5678));
7+
var_dump(new MongoDB\BSON\Timestamp(1234, 5678) < new MongoDB\BSON\Timestamp(1234, 5678));
8+
var_dump(new MongoDB\BSON\Timestamp(1234, 5678) > new MongoDB\BSON\Timestamp(1234, 5678));
9+
10+
// Timestamp is compared first
11+
var_dump(new MongoDB\BSON\Timestamp(1234, 5678) < new MongoDB\BSON\Timestamp(1233, 5679));
12+
var_dump(new MongoDB\BSON\Timestamp(1234, 5678) > new MongoDB\BSON\Timestamp(1235, 5677));
13+
14+
// Increment is compared second
15+
var_dump(new MongoDB\BSON\Timestamp(1234, 5678) < new MongoDB\BSON\Timestamp(1235, 5678));
16+
var_dump(new MongoDB\BSON\Timestamp(1234, 5678) > new MongoDB\BSON\Timestamp(1233, 5678));
17+
18+
?>
19+
===DONE===
20+
<?php exit(0); ?>
21+
--EXPECTF--
22+
bool(true)
23+
bool(false)
24+
bool(false)
25+
bool(true)
26+
bool(true)
27+
bool(true)
28+
bool(true)
29+
===DONE===

0 commit comments

Comments
 (0)