Skip to content

Commit

Permalink
patch UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
AxiosLeo committed Jul 18, 2020
1 parent d91cec9 commit 95b29f3
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/UUID.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,35 @@ public function v2()
return md5($this->salt . uniqid(md5(microtime(true)), true));
}

public function v3($cut = 8, $flavour = '-', $isUpper = false)
public function v3($cut = 8, $flavour = '-')
{
$str = self::v2();
$len = \strlen($str);
$length = $len;
$uuid = '';
if (\is_array($cut)) {
while ($length > 0) {
$uuid .= substr($str, $len - $length, array_rand($cut)) . $flavour;
$length -= $cut;
}
} elseif (\is_int($cut)) {
$step = 0;
while ($length > 0) {
$temp = substr($str, $len - $length, $cut);
$uuid .= 0 != $step ? $flavour . $temp : $temp;
$length -= $cut;
++$step;
}
$length = 32;
$tmp = [];
while ($length > 0) {
$part = substr($str, 32 - $length, $cut);
array_push($tmp, $part);
$length -= $cut;
}

return $isUpper ? strtoupper($uuid) : $uuid;
return implode($flavour, $tmp);
}

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();
$length = 32;
$tmp = [];
while ($length > 0) {
$part = substr($str, 32 - $length, array_rand($cut));
array_push($tmp, $part);
$length -= $cut;
}

return implode($flavour, $tmp);
}
}

0 comments on commit 95b29f3

Please sign in to comment.