Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,26 @@ A small PHP class for generating [RFC 4122](http://tools.ietf.org/html/rfc4122)

If all you want is a unique ID, you should call `uuid4()`.

## Minimal UUID v4 implementation

Credits go to [this answer](https://stackoverflow.com/a/15875555) on Stackoverflow for this minimal RFC 4122 compliant solution.
```php
<?php
function uuid4()
{
$data = random_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}

echo uuid4();
```

## Installation

If you need comparison tools or sortable identifiers like in version 6, you might find this small and fast package useful. It doesn't require any other dependencies.

```bash
composer require oittaa/uuid
```
Expand Down
1 change: 1 addition & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"runner.bootstrap": "vendor/autoload.php",
"runner.path": "tests/Benchmark",
"runner.retry_threshold": 5,
"runner.iterations": 5,
"runner.revs": 1000
}
52 changes: 52 additions & 0 deletions tests/Benchmark/UUIDComparisonBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace UUID\Benchmark;

use UUID\UUID;

class UUIDComparisonBench
{
public function benchComparisonWithEquals(): void
{
UUID::cmp(
'urn:uuid:c4a760a8-dbcf-5254-a0d9-6a4474bd1b62',
'{C4A760A8-DBCF-5254-A0D9-6A4474BD1B62}'
);
}

public function benchComparisonWithDifference(): void
{
UUID::cmp(
'c4a760a8-dbcf-5254-a0d9-6a4474bd1b62',
'2140a926-4a47-465c-b622-4571ad9bb378'
);
}

public function benchEquals(): void
{
UUID::equals(
'c4a760a8-dbcf-5254-a0d9-6a4474bd1b62',
'C4A760A8-DBCF-5254-A0D9-6A4474BD1B62'
);
}

public function benchNotEquals(): void
{
UUID::equals(
'c4a760a8-dbcf-5254-a0d9-6a4474bd1b62',
'c4a760a8-dbcf-5254-a0d9-6a4474bd1b63'
);
}

public function benchIsValid(): void
{
UUID::isValid('11a38b9a-b3da-360f-9353-a5a725514269');
}

public function benchIsNotValid(): void
{
UUID::isValid('11a38b9a-b3da-xxxx-9353-a5a725514269');
}
}
4 changes: 4 additions & 0 deletions tests/Benchmark/UUIDGenerationBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public function benchUUID6Generation(): void
{
UUID::uuid6();
}
public function benchUUIDToString(): void
{
UUID::toString('{C4A760A8-DBCF-5254-A0D9-6A4474BD1B62}');
}
}
27 changes: 17 additions & 10 deletions tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ public function testCanGenerateValidVersion3()
public function testCanGenerateValidVersion4()
{
$uuid1 = UUID::uuid4();
$this->assertMatchesRegularExpression(
'/^[0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12}$/',
$uuid1
);
$uuid2 = UUID::uuid4();
$this->assertNotEquals(
$uuid1,
$uuid2
);
for ($x = 0; $x < 10; $x++) {
$this->assertMatchesRegularExpression(
'/^[0-9a-f]{8}\-[0-9a-f]{4}\-4[0-9a-f]{3}\-[89ab][0-9a-f]{3}\-[0-9a-f]{12}$/',
$uuid1
);
$uuid2 = UUID::uuid4();
$this->assertNotEquals(
$uuid1,
$uuid2
);
$uuid1 = $uuid2;
}
}

public function testCanGenerateValidVersion5()
Expand All @@ -45,7 +48,11 @@ public function testCanGenerateValidVersion5()
public function testCanGenerateValidVersion6()
{
$uuid1 = UUID::uuid6();
for ($x = 0; $x <= 10; $x++) {
for ($x = 0; $x < 10; $x++) {
$this->assertMatchesRegularExpression(
'/^[0-9a-f]{8}\-[0-9a-f]{4}\-6[0-9a-f]{3}\-[89ab][0-9a-f]{3}\-[0-9a-f]{12}$/',
$uuid1
);
usleep(1);
$uuid2 = UUID::uuid6();
$this->assertGreaterThan(
Expand Down