Skip to content

Commit 847a02f

Browse files
committed
PHPC-1230: Allow cloning Timestamp objects
1 parent fd1984a commit 847a02f

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/BSON/Timestamp.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,32 @@ static phongo_create_object_retval php_phongo_timestamp_create_object(zend_class
430430
#endif
431431
} /* }}} */
432432

433+
static phongo_create_object_retval php_phongo_timestamp_clone_object(zval* object TSRMLS_DC) /* {{{ */
434+
{
435+
php_phongo_timestamp_t* intern;
436+
php_phongo_timestamp_t* new_intern;
437+
phongo_create_object_retval new_object;
438+
439+
intern = Z_TIMESTAMP_OBJ_P(object);
440+
new_object = php_phongo_timestamp_create_object(Z_OBJCE_P(object) TSRMLS_CC);
441+
442+
#if PHP_VERSION_ID >= 70000
443+
new_intern = Z_OBJ_TIMESTAMP(new_object);
444+
zend_objects_clone_members(&new_intern->std, &intern->std TSRMLS_CC);
445+
#else
446+
{
447+
zend_object_handle handle = Z_OBJ_HANDLE_P(object);
448+
449+
new_intern = (php_phongo_timestamp_t*) zend_object_store_get_object_by_handle(new_object.handle TSRMLS_CC);
450+
zend_objects_clone_members(&new_intern->std, new_object, &intern->std, handle TSRMLS_CC);
451+
}
452+
#endif
453+
454+
php_phongo_timestamp_init(new_intern, intern->increment, intern->timestamp TSRMLS_CC);
455+
456+
return new_object;
457+
} /* }}} */
458+
433459
static int php_phongo_timestamp_compare_objects(zval* o1, zval* o2 TSRMLS_DC) /* {{{ */
434460
{
435461
php_phongo_timestamp_t *intern1, *intern2;
@@ -531,6 +557,7 @@ void php_phongo_timestamp_init_ce(INIT_FUNC_ARGS) /* {{{ */
531557
zend_class_implements(php_phongo_timestamp_ce TSRMLS_CC, 1, zend_ce_serializable);
532558

533559
memcpy(&php_phongo_handler_timestamp, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
560+
php_phongo_handler_timestamp.clone_obj = php_phongo_timestamp_clone_object;
534561
php_phongo_handler_timestamp.compare_objects = php_phongo_timestamp_compare_objects;
535562
php_phongo_handler_timestamp.get_debug_info = php_phongo_timestamp_get_debug_info;
536563
php_phongo_handler_timestamp.get_gc = php_phongo_timestamp_get_gc;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
MongoDB\BSON\Timestamp can be cloned
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/tools.php';
7+
8+
$timestamp = new MongoDB\BSON\Timestamp(1234, 5678);
9+
10+
$clone = clone $timestamp;
11+
12+
var_dump($clone == $timestamp);
13+
var_dump($clone === $timestamp);
14+
15+
unset($timestamp);
16+
17+
var_dump($clone);
18+
?>
19+
===DONE===
20+
<?php exit(0); ?>
21+
--EXPECTF--
22+
bool(true)
23+
bool(false)
24+
object(MongoDB\BSON\Timestamp)#%d (2) {
25+
["increment"]=>
26+
string(4) "1234"
27+
["timestamp"]=>
28+
string(4) "5678"
29+
}
30+
===DONE===

0 commit comments

Comments
 (0)