diff --git a/src/BSON/UTCDateTime.c b/src/BSON/UTCDateTime.c index 8cb153f84..8a732bfbc 100644 --- a/src/BSON/UTCDateTime.c +++ b/src/BSON/UTCDateTime.c @@ -104,6 +104,35 @@ static bool php_phongo_utcdatetime_init_from_date(php_phongo_utcdatetime_t* inte return true; } +static bool php_phongo_utcdatetime_init_from_object(php_phongo_utcdatetime_t* intern, zend_object* object) +{ + if (instanceof_function(object->ce, php_date_get_interface_ce())) { + php_phongo_utcdatetime_init_from_date(intern, php_date_obj_from_obj(object)); + + return true; + } + + if (instanceof_function(object->ce, php_phongo_int64_ce)) { + php_phongo_utcdatetime_init(intern, php_int64_fetch_object(object)->integer); + + return true; + } + + phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected instance of %s or %s, %s given", ZSTR_VAL(php_date_get_interface_ce()->name), ZSTR_VAL(php_phongo_int64_ce->name), ZSTR_VAL(object->ce->name)); + + return false; +} + +static bool php_phongo_utcdatetime_init_from_double(php_phongo_utcdatetime_t* intern, double milliseconds) +{ + char tmp[24]; + int tmp_len; + + tmp_len = snprintf(tmp, sizeof(tmp), "%.0f", milliseconds > 0 ? floor(milliseconds) : ceil(milliseconds)); + + return php_phongo_utcdatetime_init_from_string(intern, tmp, tmp_len); +} + static HashTable* php_phongo_utcdatetime_get_properties_hash(phongo_compat_object_handler_type* object, bool is_temp) { php_phongo_utcdatetime_t* intern; @@ -179,36 +208,27 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, __construct) return; } - if (Z_TYPE_P(milliseconds) == IS_OBJECT) { - if (instanceof_function(Z_OBJCE_P(milliseconds), php_date_get_interface_ce())) { - php_phongo_utcdatetime_init_from_date(intern, Z_PHPDATE_P(milliseconds)); - } else { - phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected instance of DateTimeInterface, %s given", ZSTR_VAL(Z_OBJCE_P(milliseconds)->name)); - } - return; - } - - if (Z_TYPE_P(milliseconds) == IS_LONG) { - php_phongo_utcdatetime_init(intern, Z_LVAL_P(milliseconds)); - return; - } + switch (Z_TYPE_P(milliseconds)) { + case IS_OBJECT: + php_phongo_utcdatetime_init_from_object(intern, Z_OBJ_P(milliseconds)); + return; - if (Z_TYPE_P(milliseconds) == IS_DOUBLE) { - char tmp[24]; - int tmp_len; + case IS_LONG: + php_phongo_utcdatetime_init(intern, Z_LVAL_P(milliseconds)); + return; - tmp_len = snprintf(tmp, sizeof(tmp), "%.0f", Z_DVAL_P(milliseconds) > 0 ? floor(Z_DVAL_P(milliseconds)) : ceil(Z_DVAL_P(milliseconds))); + case IS_DOUBLE: + php_phongo_utcdatetime_init_from_double(intern, Z_DVAL_P(milliseconds)); + return; - php_phongo_utcdatetime_init_from_string(intern, tmp, tmp_len); - return; - } + case IS_STRING: + php_error_docref(NULL, E_DEPRECATED, "Creating a %s instance with a string is deprecated and will be removed in ext-mongodb 2.0", ZSTR_VAL(php_phongo_utcdatetime_ce->name)); - if (Z_TYPE_P(milliseconds) != IS_STRING) { - phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(milliseconds)); - return; + php_phongo_utcdatetime_init_from_string(intern, Z_STRVAL_P(milliseconds), Z_STRLEN_P(milliseconds)); + return; } - php_phongo_utcdatetime_init_from_string(intern, Z_STRVAL_P(milliseconds), Z_STRLEN_P(milliseconds)); + phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(milliseconds)); } static PHP_METHOD(MongoDB_BSON_UTCDateTime, __set_state) diff --git a/src/BSON/UTCDateTime.stub.php b/src/BSON/UTCDateTime.stub.php index ac9143a33..575a70641 100644 --- a/src/BSON/UTCDateTime.stub.php +++ b/src/BSON/UTCDateTime.stub.php @@ -10,9 +10,9 @@ final class UTCDateTime implements UTCDateTimeInterface, \JsonSerializable, Type, \Serializable { #if PHP_VERSION_ID >= 80000 - final public function __construct(int|string|float|\DateTimeInterface|null $milliseconds = null) {} + final public function __construct(int|string|float|\DateTimeInterface|Int64|null $milliseconds = null) {} #else - /** @param int|string|float|\DateTimeInterface|null $milliseconds */ + /** @param int|string|float|\DateTimeInterface|Int64|null $milliseconds */ final public function __construct($milliseconds = null) {} #endif diff --git a/src/BSON/UTCDateTime_arginfo.h b/src/BSON/UTCDateTime_arginfo.h index df008acda..581ef5fb6 100644 --- a/src/BSON/UTCDateTime_arginfo.h +++ b/src/BSON/UTCDateTime_arginfo.h @@ -1,9 +1,9 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 2b7ac84585a74a210af3cb2061541977cc309e2d */ + * Stub hash: 11f44ab06f1806ec74876a6034c6fd75672fe2a8 */ #if PHP_VERSION_ID >= 80000 ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MongoDB_BSON_UTCDateTime___construct, 0, 0, 0) - ZEND_ARG_OBJ_TYPE_MASK(0, milliseconds, DateTimeInterface, MAY_BE_LONG|MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_NULL, "null") + ZEND_ARG_OBJ_TYPE_MASK(0, milliseconds, DateTimeInterface|MongoDB\\BSON\\Int64, MAY_BE_LONG|MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_NULL, "null") ZEND_END_ARG_INFO() #endif diff --git a/tests/bson/bson-fromPHP-003.phpt b/tests/bson/bson-fromPHP-003.phpt index 38df0c17e..85fc850b2 100644 --- a/tests/bson/bson-fromPHP-003.phpt +++ b/tests/bson/bson-fromPHP-003.phpt @@ -12,8 +12,8 @@ class MyDocument { } $tests = array( - array(new MongoDB\BSON\UTCDateTime('1416445411987')), - array('x' => new MongoDB\BSON\UTCDateTime('1416445411987')), + array(new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1416445411987'))), + array('x' => new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1416445411987'))), array(new MyDocument), array('x' => new MyDocument), ); diff --git a/tests/bson/bson-fromPHP_error-003.phpt b/tests/bson/bson-fromPHP_error-003.phpt index b6f355ec8..195dccf0a 100644 --- a/tests/bson/bson-fromPHP_error-003.phpt +++ b/tests/bson/bson-fromPHP_error-003.phpt @@ -16,7 +16,7 @@ $tests = array( new MongoDB\BSON\ObjectId, new MongoDB\BSON\Regex('regexp', 'i'), new MongoDB\BSON\Timestamp(1234, 5678), - new MongoDB\BSON\UTCDateTime('1416445411987'), + new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1416445411987')), ); foreach ($tests as $document) { diff --git a/tests/bson/bson-toPHP-004.phpt b/tests/bson/bson-toPHP-004.phpt index 1bf4f1b81..c8e5497f8 100644 --- a/tests/bson/bson-toPHP-004.phpt +++ b/tests/bson/bson-toPHP-004.phpt @@ -29,7 +29,7 @@ $tests = [ new MongoDB\BSON\ObjectId('586c18d86118fd6c9012dec1'), new MongoDB\BSON\Regex('foo'), new MongoDB\BSON\Timestamp(1234, 5678), - new MongoDB\BSON\UTCDateTime('1483479256924'), + new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1483479256924')), ]; foreach ($tests as $value) { diff --git a/tests/bson/bson-utcdatetime-001.phpt b/tests/bson/bson-utcdatetime-001.phpt index 73bf8922e..7175e4004 100644 --- a/tests/bson/bson-utcdatetime-001.phpt +++ b/tests/bson/bson-utcdatetime-001.phpt @@ -11,7 +11,7 @@ require_once __DIR__ . "/../utils/basic.inc"; $manager = create_test_manager(); -$utcdatetime = new MongoDB\BSON\UTCDateTime("1416445411987"); +$utcdatetime = new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1416445411987')); $bulk = new MongoDB\Driver\BulkWrite(); $bulk->insert(array('_id' => 1, 'x' => $utcdatetime)); diff --git a/tests/bson/bson-utcdatetime-002.phpt b/tests/bson/bson-utcdatetime-002.phpt index 6da9fd291..27db72e4c 100644 --- a/tests/bson/bson-utcdatetime-002.phpt +++ b/tests/bson/bson-utcdatetime-002.phpt @@ -3,7 +3,7 @@ MongoDB\BSON\UTCDateTime debug handler --FILE-- - %rint\(|string\(13\) "|%r1416445411987%r"|\)%r + string(13) "1416445411987" } ===DONE=== diff --git a/tests/bson/bson-utcdatetime-008.phpt b/tests/bson/bson-utcdatetime-008.phpt new file mode 100644 index 000000000..d39e02088 --- /dev/null +++ b/tests/bson/bson-utcdatetime-008.phpt @@ -0,0 +1,21 @@ +--TEST-- +MongoDB\BSON\UTCDateTime construction from 64-bit integer as string +--FILE-- + +===DONE=== + +--EXPECTF-- +Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and will be removed in ext-mongodb 2.0 in %s +object(MongoDB\BSON\UTCDateTime)#%d (%d) { + ["milliseconds"]=> + string(13) "1416445411987" +} +===DONE=== diff --git a/tests/bson/bson-utcdatetime-009.phpt b/tests/bson/bson-utcdatetime-009.phpt new file mode 100644 index 000000000..f17bc0e35 --- /dev/null +++ b/tests/bson/bson-utcdatetime-009.phpt @@ -0,0 +1,20 @@ +--TEST-- +MongoDB\BSON\UTCDateTime construction from Int64 object +--FILE-- + +===DONE=== + +--EXPECTF-- +object(MongoDB\BSON\UTCDateTime)#%d (%d) { + ["milliseconds"]=> + string(13) "1416445411987" +} +===DONE=== diff --git a/tests/bson/bson-utcdatetime-clone-001.phpt b/tests/bson/bson-utcdatetime-clone-001.phpt index 1a516c367..ac7e05111 100644 --- a/tests/bson/bson-utcdatetime-clone-001.phpt +++ b/tests/bson/bson-utcdatetime-clone-001.phpt @@ -8,7 +8,7 @@ MongoDB\BSON\UTCDateTime can be cloned (PHP < 8.2) require_once __DIR__ . "/../utils/basic.inc"; -$utcdatetime = new MongoDB\BSON\UTCDateTime("1416445411987"); +$utcdatetime = new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1416445411987')); $clone = clone $utcdatetime; diff --git a/tests/bson/bson-utcdatetime-clone-002.phpt b/tests/bson/bson-utcdatetime-clone-002.phpt index a7834ae59..b9e8922c1 100644 --- a/tests/bson/bson-utcdatetime-clone-002.phpt +++ b/tests/bson/bson-utcdatetime-clone-002.phpt @@ -8,7 +8,7 @@ MongoDB\BSON\UTCDateTime can be cloned (PHP >= 8.2) require_once __DIR__ . "/../utils/basic.inc"; -$utcdatetime = new MongoDB\BSON\UTCDateTime("1416445411987"); +$utcdatetime = new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1416445411987')); $clone = clone $utcdatetime; diff --git a/tests/bson/bson-utcdatetime-get_properties-001.phpt b/tests/bson/bson-utcdatetime-get_properties-001.phpt index ab4dd66d9..572884228 100644 --- a/tests/bson/bson-utcdatetime-get_properties-001.phpt +++ b/tests/bson/bson-utcdatetime-get_properties-001.phpt @@ -3,7 +3,7 @@ MongoDB\BSON\UTCDateTime get_properties handler (get_object_vars) --FILE-- $value) { var_dump($key); diff --git a/tests/bson/bson-utcdatetime-int-size-001.phpt b/tests/bson/bson-utcdatetime-int-size-001.phpt deleted file mode 100644 index be2eaf9d7..000000000 --- a/tests/bson/bson-utcdatetime-int-size-001.phpt +++ /dev/null @@ -1,30 +0,0 @@ ---TEST-- -MongoDB\BSON\UTCDateTime integer parsing from string ---INI-- -date.timezone=UTC -error_reporting=-1 -dislay_errors=1 ---FILE-- -toDateTime()); - -?> -===DONE=== - ---EXPECTF-- -object(MongoDB\BSON\UTCDateTime)#%d (1) { - ["milliseconds"]=> - string(13) "1416445411987" -} -object(DateTime)#%d (3) { - ["date"]=> - string(26) "2014-11-20 01:03:31.987000" - ["timezone_type"]=> - int(1) - ["timezone"]=> - string(6) "+00:00" -} -===DONE=== diff --git a/tests/bson/bson-utcdatetime-int-size-002.phpt b/tests/bson/bson-utcdatetime-int-size-002.phpt deleted file mode 100644 index d303c9574..000000000 --- a/tests/bson/bson-utcdatetime-int-size-002.phpt +++ /dev/null @@ -1,32 +0,0 @@ ---TEST-- -MongoDB\BSON\UTCDateTime integer parsing from number (64-bit) ---SKIPIF-- - ---INI-- -date.timezone=UTC -error_reporting=-1 -dislay_errors=1 ---FILE-- -toDateTime()); - -?> -===DONE=== - ---EXPECTF-- -object(MongoDB\BSON\UTCDateTime)#%d (1) { - ["milliseconds"]=> - string(13) "1416445411987" -} -object(DateTime)#%d (3) { - ["date"]=> - string(26) "2014-11-20 01:03:31.987000" - ["timezone_type"]=> - int(1) - ["timezone"]=> - string(6) "+00:00" -} -===DONE=== diff --git a/tests/bson/bson-utcdatetime-serialization-003.phpt b/tests/bson/bson-utcdatetime-serialization-003.phpt index 941057adf..c15c7548f 100644 --- a/tests/bson/bson-utcdatetime-serialization-003.phpt +++ b/tests/bson/bson-utcdatetime-serialization-003.phpt @@ -4,9 +4,9 @@ MongoDB\BSON\UTCDateTime serialization (__serialize and __unserialize) toDateTime(); var_dump(get_class($datetime)); var_dump($datetime->format(DATE_RSS)); diff --git a/tests/bson/bson-utcdatetime-todatetime-002.phpt b/tests/bson/bson-utcdatetime-todatetime-002.phpt index e003cd188..f127f19fe 100644 --- a/tests/bson/bson-utcdatetime-todatetime-002.phpt +++ b/tests/bson/bson-utcdatetime-todatetime-002.phpt @@ -5,7 +5,7 @@ date.timezone=UTC --FILE-- toDateTime(); var_dump(get_class($datetime)); echo $datetime->format('U.u'), "\n"; diff --git a/tests/bson/bson-utcdatetime-todatetimeimmutable-001.phpt b/tests/bson/bson-utcdatetime-todatetimeimmutable-001.phpt index 4d7bd8385..3e17543fb 100644 --- a/tests/bson/bson-utcdatetime-todatetimeimmutable-001.phpt +++ b/tests/bson/bson-utcdatetime-todatetimeimmutable-001.phpt @@ -5,7 +5,7 @@ date.timezone=America/Los_Angeles --FILE-- toDateTimeImmutable(); var_dump(get_class($datetime)); var_dump($datetime->format(DATE_RSS)); diff --git a/tests/bson/bson-utcdatetime-todatetimeimmutable-002.phpt b/tests/bson/bson-utcdatetime-todatetimeimmutable-002.phpt index b959d0bc1..8ccf140c9 100644 --- a/tests/bson/bson-utcdatetime-todatetimeimmutable-002.phpt +++ b/tests/bson/bson-utcdatetime-todatetimeimmutable-002.phpt @@ -5,7 +5,7 @@ date.timezone=UTC --FILE-- toDateTimeImmutable(); var_dump(get_class($datetime)); echo $datetime->format('U.u'), "\n"; diff --git a/tests/bson/bson-utcdatetime-tostring-001.phpt b/tests/bson/bson-utcdatetime-tostring-001.phpt index 53b9fdd82..b88fbe867 100644 --- a/tests/bson/bson-utcdatetime-tostring-001.phpt +++ b/tests/bson/bson-utcdatetime-tostring-001.phpt @@ -3,7 +3,7 @@ MongoDB\BSON\UTCDateTime::__toString() --FILE-- diff --git a/tests/bson/bson-utcdatetime_error-001.phpt b/tests/bson/bson-utcdatetime_error-001.phpt index 54534fbf6..7dc837507 100644 --- a/tests/bson/bson-utcdatetime_error-001.phpt +++ b/tests/bson/bson-utcdatetime_error-001.phpt @@ -14,5 +14,5 @@ echo throws(function() { --EXPECT-- OK: Got MongoDB\Driver\Exception\InvalidArgumentException -Expected instance of DateTimeInterface, stdClass given +Expected instance of DateTimeInterface or MongoDB\BSON\Int64, stdClass given ===DONE=== diff --git a/tests/bson/bson-utcdatetime_error-003.phpt b/tests/bson/bson-utcdatetime_error-003.phpt index 760eff8b2..6eb1fa741 100644 --- a/tests/bson/bson-utcdatetime_error-003.phpt +++ b/tests/bson/bson-utcdatetime_error-003.phpt @@ -26,13 +26,20 @@ echo throws(function() { ?> ===DONE=== ---EXPECT-- +--EXPECTF-- +Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and will be removed in ext-mongodb 2.0 in %s OK: Got MongoDB\Driver\Exception\InvalidArgumentException Error parsing "1234.5678" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization + +Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and will be removed in ext-mongodb 2.0 in %s OK: Got MongoDB\Driver\Exception\InvalidArgumentException Error parsing "9223372036854775808" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization + +Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and will be removed in ext-mongodb 2.0 in %s OK: Got MongoDB\Driver\Exception\InvalidArgumentException Error parsing "-9223372036854775809" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization + +Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and will be removed in ext-mongodb 2.0 in %s OK: Got MongoDB\Driver\Exception\InvalidArgumentException Error parsing "18446744073709551615" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization ===DONE=== diff --git a/tests/bson/bson-utcdatetimeinterface-001.phpt b/tests/bson/bson-utcdatetimeinterface-001.phpt index 26c31b2fb..babd2f798 100644 --- a/tests/bson/bson-utcdatetimeinterface-001.phpt +++ b/tests/bson/bson-utcdatetimeinterface-001.phpt @@ -3,7 +3,7 @@ MongoDB\BSON\UTCDateTimeInterface is implemented by MongoDB\BSON\UTCDateTime --FILE-- diff --git a/tests/bson/bug0631.phpt b/tests/bson/bug0631.phpt index c07f95863..3099076cd 100644 --- a/tests/bson/bug0631.phpt +++ b/tests/bson/bug0631.phpt @@ -7,7 +7,7 @@ date.timezone=UTC require_once __DIR__ . '/../utils/basic.inc'; -$utcdatetime = new MongoDB\BSON\UTCDateTime('1466540755123'); +$utcdatetime = new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1466540755123')); $datetime = $utcdatetime->toDateTime(); $s = serialize($datetime); diff --git a/tests/writeResult/writeresult-getupsertedids-002.phpt b/tests/writeResult/writeresult-getupsertedids-002.phpt index 786fa0956..713a8c212 100644 --- a/tests/writeResult/writeresult-getupsertedids-002.phpt +++ b/tests/writeResult/writeresult-getupsertedids-002.phpt @@ -25,7 +25,7 @@ $tests = [ new MongoDB\BSON\MinKey, new MongoDB\BSON\ObjectId('586c18d86118fd6c9012dec1'), new MongoDB\BSON\Timestamp(1234, 5678), - new MongoDB\BSON\UTCDateTime('1483479256924'), + new MongoDB\BSON\UTCDateTime(new MongoDB\BSON\Int64('1483479256924')), ]; $manager = create_test_manager();