Skip to content

Commit 03f71eb

Browse files
zacekstamhankar999
authored andcommitted
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.
1 parent 720aae0 commit 03f71eb

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

Diff for: ext/src/Timeuuid.c

+28-7
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ zend_class_entry *php_driver_timeuuid_ce = NULL;
2727
void
2828
php_driver_timeuuid_init(INTERNAL_FUNCTION_PARAMETERS)
2929
{
30-
long timestamp;
3130
php_driver_uuid *self;
31+
zval *param;
32+
int version;
3233

33-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &timestamp) == FAILURE) {
34+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &param) == FAILURE) {
3435
return;
3536
}
3637

@@ -41,14 +42,34 @@ php_driver_timeuuid_init(INTERNAL_FUNCTION_PARAMETERS)
4142
self = PHP_DRIVER_GET_UUID(return_value);
4243
}
4344

45+
4446
if (ZEND_NUM_ARGS() == 0) {
4547
php_driver_uuid_generate_time(&self->uuid TSRMLS_CC);
4648
} else {
47-
if (timestamp < 0) {
48-
zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Timestamp must be a positive integer, %d given", timestamp);
49-
return;
50-
}
51-
php_driver_uuid_generate_from_time(timestamp, &self->uuid TSRMLS_CC);
49+
50+
switch (Z_TYPE_P(param)) {
51+
case IS_LONG:
52+
if (Z_LVAL_P(param) < 0) {
53+
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));
54+
return;
55+
}
56+
php_driver_uuid_generate_from_time(Z_LVAL_P(param), &self->uuid TSRMLS_CC);
57+
break;
58+
case IS_STRING:
59+
if (cass_uuid_from_string(Z_STRVAL_P(param), &self->uuid) != CASS_OK) {
60+
zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid UUID: '%.*s'", Z_STRLEN_P(param), Z_STRVAL_P(param));
61+
return;
62+
}
63+
64+
version = cass_uuid_version(self->uuid);
65+
if (version != 1) {
66+
zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "UUID must be of type 1, type %d given", version);
67+
}
68+
break;
69+
default:
70+
zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid argument - integer or string expected");
71+
}
72+
5273
}
5374
}
5475

Diff for: tests/unit/Cassandra/TimeUuidTest.php

+38
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,42 @@ public function notEqualTypes()
5454
array(new TimeUuid(0), new TimeUuid(2))
5555
);
5656
}
57+
58+
/**
59+
* TimeUuid can be created from string
60+
*/
61+
public function testInitFromStringType1()
62+
{
63+
new TimeUuid('5f344f20-52a3-11e7-915b-5f4f349b532d');
64+
}
65+
66+
/**
67+
* TimeUuid cannot be created from UUID type 4
68+
* @expectedException Cassandra\Exception\InvalidArgumentException
69+
* @expectedExceptionMessage UUID must be of type 1, type 4 given
70+
*/
71+
public function testInitFromStringType4()
72+
{
73+
new TimeUuid('65f9e722-036a-4029-b03b-a9046b23b4c9');
74+
}
75+
76+
/**
77+
* TimeUuid cannot be created from invalid string
78+
* @expectedException Cassandra\Exception\InvalidArgumentException
79+
* @expectedExceptionMessage Invalid UUID
80+
*/
81+
public function testInitFromInvalidString()
82+
{
83+
new TimeUuid('invalid');
84+
}
85+
86+
/**
87+
* TimeUuid requires string or integer in constructor
88+
* @expectedException Cassandra\Exception\InvalidArgumentException
89+
* @expectedExceptionMessage Invalid argument
90+
*/
91+
public function testInitInvalidArgument()
92+
{
93+
new TimeUuid(new \Datetime());
94+
}
5795
}

0 commit comments

Comments
 (0)