Skip to content

Commit

Permalink
Add function uuid_format
Browse files Browse the repository at this point in the history
  • Loading branch information
maximkott committed Dec 19, 2016
1 parent b06295b commit 1b726f7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
9 changes: 8 additions & 1 deletion readme.md
Expand Up @@ -22,6 +22,7 @@
- [str_random](#str_random)
- [str_explode](#str_explode)
- [uuid](#uuid)
- [uuid_format](#uuid_format)
- [simple_uuid](#simple_uuid)
- [format_xml](#format_xml)

Expand Down Expand Up @@ -105,7 +106,13 @@ Split a string by one or multiple delimiters. Works the same way as the `explode

Generate a v4 uuid.

`string uuid()`
`string uuid([string $prefix = null, int $length = 36])`

#### uuid\_format

Format string as a v4 uuid.

`string uuid_format(string $string [, string $prefix = null, int $length = null])`

#### simple\_uuid

Expand Down
37 changes: 31 additions & 6 deletions src/string.php
Expand Up @@ -269,19 +269,44 @@ function uuid($prefix = null, $length = 36) {
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);

$uuid = vsprintf(
'%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)
);
return uuid_format(bin2hex($data), $prefix, $length);
}
}

if ( ! function_exists('uuid_format')) {
/**
* Format a string as a v4 uuid.
*
* @param string $string
* @param string $prefix
* @param int $length
*
* @return string
*/
function uuid_format($string, $prefix = null, $length = null) {
$uuid = implode('-', array_filter([
substr($string, 0, 8),
substr($string, 8, 4),
substr($string, 12, 4),
substr($string, 16, 4),
substr($string, 20)
]));

if ($prefix) {
$uuid = s('%s-%s', $prefix, $uuid);
}

while (strlen($uuid) < $length) {
$uuid .= '-' . uuid();
if ($length !== null) {
while (strlen($uuid) < $length) {
$uuid .= '-' . uuid();
}

$uuid = substr($uuid, 0, $length);
}

$uuid = substr($uuid, 0, $length);
while (strpos($uuid, '--') !== false) {
$uuid = str_replace('--', '-', $uuid);
}

while (str_ends_with($uuid, '-')) {
$uuid = substr($uuid, 0, -1) . str_random(1);
Expand Down
26 changes: 26 additions & 0 deletions tests/StringTest.php
Expand Up @@ -215,6 +215,32 @@ public function test_uuid_takes_custom_length() {
$this->assertEquals(55, strlen(uuid('abcdefghijk', 55)));
}

public function test_uuid_format() {
$id = uuid_format('1234567812341234123412345678');
$this->assertEquals('12345678-1234-1234-1234-12345678', $id);
}

public function test_uuid_format_with_prefix() {
$uuid = uuid_format('123', 'foo');
$this->assertStringStartsWith('foo', $uuid);
}

public function test_uuid_format_does_not_end_with_a_dash() {
$this->assertFalse(str_ends_with(uuid_format('123'), '-'));
}

public function test_uuid_format_removes_double_dashes() {
$this->assertFalse(
strpos(uuid_format('12345678-1234-1234--1234--12345678'), '--')
);
}

public function test_uuid_format_takes_custom_length() {
$this->assertEquals(15, strlen(uuid_format('1234567890', null, 15)));
$this->assertEquals(55, strlen(uuid_format('1', null, 55)));
$this->assertEquals(55, strlen(uuid_format('12345678901', null, 55)));
}

public function test_uuid_simple() {
$id1 = uuid_simple();
$id2 = uuid_simple();
Expand Down

0 comments on commit 1b726f7

Please sign in to comment.