Skip to content

Commit

Permalink
Timeuuid can be initialized from UUID string
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zacek authored and stamhankar999 committed Aug 1, 2017
1 parent 720aae0 commit 03f71eb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
35 changes: 28 additions & 7 deletions ext/src/Timeuuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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", &timestamp) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &param) == FAILURE) {
return;
}

Expand All @@ -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");
}

}
}

Expand Down
38 changes: 38 additions & 0 deletions tests/unit/Cassandra/TimeUuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 03f71eb

Please sign in to comment.