Skip to content

Commit

Permalink
patch uuid
Browse files Browse the repository at this point in the history
add uuid test case
  • Loading branch information
AxiosLeo committed Jul 21, 2020
1 parent 3e389f2 commit c19ecdf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/UUID.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ public function __construct($salt = '')

public function v0()
{
return uniqid(microtime(true));
return uniqid((string) (microtime(true)));
}

public function v1()
{
return uniqid(md5(microtime(true)));
return uniqid(md5((string) (microtime(true))));
}

public function v2()
{
return md5($this->salt . uniqid(md5(microtime(true)), true));
return md5($this->salt . uniqid(md5((string) (microtime(true))), true));
}

public function v3($cut = 8, $flavour = '-')
{
$str = self::v2();
$str = $this->v2();
$length = 32;
$tmp = [];
while ($length > 0) {
Expand All @@ -44,17 +44,14 @@ public function v3($cut = 8, $flavour = '-')

public function v4($cut = [6, 7, 9, 10], $flavour = '-')
{
array_sum($cut);
if (array_sum($cut) < 32) {
throw new \InvalidArgumentException('Invalid cut part length');
}
$str = self::v2();
$str = $this->v2();
$length = 32;
$tmp = [];
while ($length > 0) {
$part = substr($str, 32 - $length, array_rand($cut));
$cut_val = array_rand($cut);
$part = substr($str, 32 - $length, $cut_val);
array_push($tmp, $part);
$length -= $cut;
$length = $length - $cut_val;
}

return implode($flavour, $tmp);
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/UUIDTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace axios\tools\tests\unit;

use axios\tools\UUID;
use PHPUnit\Framework\TestCase;

/**
* @internal
* @coversNothing
*/
class UUIDTest extends TestCase
{
public function testUUID()
{
$uuid = new UUID();
$uuid->v0();
$uuid->v1();
$uuid->v2();
$uuid->v3();
$uuid->v4();
$this->assertTrue(true);
}
}

0 comments on commit c19ecdf

Please sign in to comment.