From 03f71eb55c9ad0f1d4931b915505b07c18a63e39 Mon Sep 17 00:00:00 2001 From: zacek Date: Tue, 1 Aug 2017 18:22:01 +0200 Subject: [PATCH] Timeuuid can be initialized from UUID string Allow a TimeUuid to be constructed from a long or string, where a string is passed to cass_uuid_from_string to produce the uuid under the hood. --- ext/src/Timeuuid.c | 35 +++++++++++++++++++----- tests/unit/Cassandra/TimeUuidTest.php | 38 +++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/ext/src/Timeuuid.c b/ext/src/Timeuuid.c index 73326c4db..9c834eef8 100644 --- a/ext/src/Timeuuid.c +++ b/ext/src/Timeuuid.c @@ -27,10 +27,11 @@ zend_class_entry *php_driver_timeuuid_ce = NULL; void php_driver_timeuuid_init(INTERNAL_FUNCTION_PARAMETERS) { - long timestamp; php_driver_uuid *self; + zval *param; + int version; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", ×tamp) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", ¶m) == FAILURE) { return; } @@ -41,14 +42,34 @@ php_driver_timeuuid_init(INTERNAL_FUNCTION_PARAMETERS) self = PHP_DRIVER_GET_UUID(return_value); } + if (ZEND_NUM_ARGS() == 0) { php_driver_uuid_generate_time(&self->uuid TSRMLS_CC); } else { - if (timestamp < 0) { - zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Timestamp must be a positive integer, %d given", timestamp); - return; - } - php_driver_uuid_generate_from_time(timestamp, &self->uuid TSRMLS_CC); + + switch (Z_TYPE_P(param)) { + case IS_LONG: + if (Z_LVAL_P(param) < 0) { + zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Timestamp must be a positive integer, %d given", Z_LVAL_P(param)); + return; + } + php_driver_uuid_generate_from_time(Z_LVAL_P(param), &self->uuid TSRMLS_CC); + break; + case IS_STRING: + if (cass_uuid_from_string(Z_STRVAL_P(param), &self->uuid) != CASS_OK) { + zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid UUID: '%.*s'", Z_STRLEN_P(param), Z_STRVAL_P(param)); + return; + } + + version = cass_uuid_version(self->uuid); + if (version != 1) { + zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "UUID must be of type 1, type %d given", version); + } + break; + default: + zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid argument - integer or string expected"); + } + } } diff --git a/tests/unit/Cassandra/TimeUuidTest.php b/tests/unit/Cassandra/TimeUuidTest.php index f5be3c1fc..22ced85d6 100644 --- a/tests/unit/Cassandra/TimeUuidTest.php +++ b/tests/unit/Cassandra/TimeUuidTest.php @@ -54,4 +54,42 @@ public function notEqualTypes() array(new TimeUuid(0), new TimeUuid(2)) ); } + + /** + * TimeUuid can be created from string + */ + public function testInitFromStringType1() + { + new TimeUuid('5f344f20-52a3-11e7-915b-5f4f349b532d'); + } + + /** + * TimeUuid cannot be created from UUID type 4 + * @expectedException Cassandra\Exception\InvalidArgumentException + * @expectedExceptionMessage UUID must be of type 1, type 4 given + */ + public function testInitFromStringType4() + { + new TimeUuid('65f9e722-036a-4029-b03b-a9046b23b4c9'); + } + + /** + * TimeUuid cannot be created from invalid string + * @expectedException Cassandra\Exception\InvalidArgumentException + * @expectedExceptionMessage Invalid UUID + */ + public function testInitFromInvalidString() + { + new TimeUuid('invalid'); + } + + /** + * TimeUuid requires string or integer in constructor + * @expectedException Cassandra\Exception\InvalidArgumentException + * @expectedExceptionMessage Invalid argument + */ + public function testInitInvalidArgument() + { + new TimeUuid(new \Datetime()); + } }