Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for the Ramsey Uuid v5 generator #1

Merged
Merged
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
40 changes: 34 additions & 6 deletions src/Uuid/UuidGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,34 @@
use Ramsey\Uuid\Uuid;

/**
* Generator for v4 UUIDs.
* Generator for v4 & v5 UUIDs.
*/
class UuidGenerator implements IUuidGenerator
{
/**
* @var string $namespace
* The UUID for this namespace.
*/
protected $namespace;

/**
* @param string $namespace
* The initial namespace for the Uuid Generator.
*/
public function __construct($namespace = NULL) {
// Give sensible default namespace if none is provided.
if (empty($namespace)) {
$namespace = "islandora.ca";
}
$this->namespace = $namespace;

// If we are passed a namespace UUID don't generate it.
if (Uuid::isValid($namespace)) {
$this->namespace = $namespace;
}
// Otherwise generate a namespace UUID from the passed in namespace.
else {
$this->namespace = Uuid::uuid5(Uuid::NAMESPACE_DNS, $namespace);
}
}

/**
Expand All @@ -31,14 +47,26 @@ public function generateV4() {
/**
* Generates a v5 UUID.
*
* @param string $str
* The word to generate the UUID with.
* @param string $namespace
* A namespace
* @return String Valid v5 UUID.
*/
public function generateV5($str, $namespace = NULL) {
// Use default namespace if none is provided.
if (empty($namespace)) {
$namespace = $this->namespace;
if (!empty($namespace)) {
// Is this a UUID already?
if (Uuid::isValid($namespace)) {
return Uuid::uuid5($namespace, $str)->toString();
}
else {
return Uuid::uuid5(Uuid::uuid5(Uuid::NAMESPACE_DNS, $namespace), $str)->toString();
}
}
else {
return Uuid::uuid5($this->namespace, $str)->toString();
}

return Uuid::uuid5($namespace, $str)->toString();
}

}