Skip to content

Commit 108f2b7

Browse files
committed
PHPC-1230: Allow cloning UTCDateTime objects
1 parent 87efc1b commit 108f2b7

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/BSON/UTCDateTime.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,32 @@ static phongo_create_object_retval php_phongo_utcdatetime_create_object(zend_cla
457457
#endif
458458
} /* }}} */
459459

460+
static phongo_create_object_retval php_phongo_utcdatetime_clone_object(zval* object TSRMLS_DC) /* {{{ */
461+
{
462+
php_phongo_utcdatetime_t* intern;
463+
php_phongo_utcdatetime_t* new_intern;
464+
phongo_create_object_retval new_object;
465+
466+
intern = Z_UTCDATETIME_OBJ_P(object);
467+
new_object = php_phongo_utcdatetime_create_object(Z_OBJCE_P(object) TSRMLS_CC);
468+
469+
#if PHP_VERSION_ID >= 70000
470+
new_intern = Z_OBJ_UTCDATETIME(new_object);
471+
zend_objects_clone_members(&new_intern->std, &intern->std TSRMLS_CC);
472+
#else
473+
{
474+
zend_object_handle handle = Z_OBJ_HANDLE_P(object);
475+
476+
new_intern = (php_phongo_utcdatetime_t*) zend_object_store_get_object_by_handle(new_object.handle TSRMLS_CC);
477+
zend_objects_clone_members(&new_intern->std, new_object, &intern->std, handle TSRMLS_CC);
478+
}
479+
#endif
480+
481+
php_phongo_utcdatetime_init(new_intern, intern->milliseconds);
482+
483+
return new_object;
484+
} /* }}} */
485+
460486
static int php_phongo_utcdatetime_compare_objects(zval* o1, zval* o2 TSRMLS_DC) /* {{{ */
461487
{
462488
php_phongo_utcdatetime_t *intern1, *intern2;
@@ -543,6 +569,7 @@ void php_phongo_utcdatetime_init_ce(INIT_FUNC_ARGS) /* {{{ */
543569
zend_class_implements(php_phongo_utcdatetime_ce TSRMLS_CC, 1, zend_ce_serializable);
544570

545571
memcpy(&php_phongo_handler_utcdatetime, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
572+
php_phongo_handler_utcdatetime.clone_obj = php_phongo_utcdatetime_clone_object;
546573
php_phongo_handler_utcdatetime.compare_objects = php_phongo_utcdatetime_compare_objects;
547574
php_phongo_handler_utcdatetime.get_debug_info = php_phongo_utcdatetime_get_debug_info;
548575
php_phongo_handler_utcdatetime.get_gc = php_phongo_utcdatetime_get_gc;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
MongoDB\BSON\UTCDateTime can be cloned
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . "/../utils/basic.inc";
7+
8+
$utcdatetime = new MongoDB\BSON\UTCDateTime("1416445411987");
9+
10+
$clone = clone $utcdatetime;
11+
12+
var_dump($clone == $utcdatetime);
13+
var_dump($clone === $utcdatetime);
14+
15+
unset($utcdatetime);
16+
17+
var_dump($clone);
18+
?>
19+
===DONE===
20+
<?php exit(0); ?>
21+
--EXPECTF--
22+
bool(true)
23+
bool(false)
24+
object(MongoDB\BSON\UTCDateTime)#%d (1) {
25+
["milliseconds"]=>
26+
string(13) "1416445411987"
27+
}
28+
===DONE===

0 commit comments

Comments
 (0)