From 1c2b704d7b5e9c91fc5f2db60ffe434d3f5d4789 Mon Sep 17 00:00:00 2001 From: Jared Whiklo Date: Wed, 24 Feb 2016 23:08:01 -0600 Subject: [PATCH 1/4] Changes for the Ramsey Uuid v5 generator --- src/Uuid/IUuidGenerator.php | 2 +- src/Uuid/UuidGenerator.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Uuid/IUuidGenerator.php b/src/Uuid/IUuidGenerator.php index 70a2229..eb30b9a 100644 --- a/src/Uuid/IUuidGenerator.php +++ b/src/Uuid/IUuidGenerator.php @@ -40,5 +40,5 @@ public function generateV4(); * * @return String Valid v5 UUID. */ - public function generateV5($name, $namespace = NULL); + public function generateV5($namespace = NULL); } diff --git a/src/Uuid/UuidGenerator.php b/src/Uuid/UuidGenerator.php index 821019f..d115210 100644 --- a/src/Uuid/UuidGenerator.php +++ b/src/Uuid/UuidGenerator.php @@ -33,12 +33,12 @@ public function generateV4() { * * @return String Valid v5 UUID. */ - public function generateV5($str, $namespace = NULL) { + public function generateV5($namespace = NULL) { // Use default namespace if none is provided. if (empty($namespace)) { $namespace = $this->namespace; } - return Uuid::uuid5($namespace, $str)->toString(); + return Uuid::uuid5(Uuid::NAMESPACE_DNS, $namespace)->toString(); } } From 4d76008dce1f8acb410cb7e14ed57d0ade3815f4 Mon Sep 17 00:00:00 2001 From: Jared Whiklo Date: Thu, 25 Feb 2016 09:42:08 -0600 Subject: [PATCH 2/4] Maybe this to ensure the base hash is persisted --- src/Uuid/IUuidGenerator.php | 2 +- src/Uuid/UuidGenerator.php | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Uuid/IUuidGenerator.php b/src/Uuid/IUuidGenerator.php index eb30b9a..bcf58e7 100644 --- a/src/Uuid/IUuidGenerator.php +++ b/src/Uuid/IUuidGenerator.php @@ -40,5 +40,5 @@ public function generateV4(); * * @return String Valid v5 UUID. */ - public function generateV5($namespace = NULL); + public function generateV5($str, $namespace = NULL); } diff --git a/src/Uuid/UuidGenerator.php b/src/Uuid/UuidGenerator.php index d115210..4b0b773 100644 --- a/src/Uuid/UuidGenerator.php +++ b/src/Uuid/UuidGenerator.php @@ -10,6 +10,7 @@ class UuidGenerator implements IUuidGenerator { protected $namespace; + protected $namespace_uuid; public function __construct($namespace = NULL) { // Give sensible default namespace if none is provided. @@ -17,6 +18,7 @@ public function __construct($namespace = NULL) { $namespace = "islandora.ca"; } $this->namespace = $namespace; + $this->namespace_uuid = Uuid::uuid(Uuid::NAMESPACE_DNS, $namespace); } /** @@ -33,12 +35,14 @@ public function generateV4() { * * @return String Valid v5 UUID. */ - public function generateV5($namespace = NULL) { + public function generateV5($str, $namespace = NULL) { // Use default namespace if none is provided. - if (empty($namespace)) { - $namespace = $this->namespace; + if (!empty($namespace)) { + return Uuid::uuid5(Uuid::uuid5(Uuid::NAMESPACE_DNS, $namespace), $str)->toString(); + } + else { + return Uuid::uuid5($namespace_uuid, $str)->toString(); } - - return Uuid::uuid5(Uuid::NAMESPACE_DNS, $namespace)->toString(); } + } From f684eaf12b0fb433e9e98c81182c7b1e561fcc37 Mon Sep 17 00:00:00 2001 From: Jared Whiklo Date: Thu, 25 Feb 2016 09:44:56 -0600 Subject: [PATCH 3/4] Why am I changing the variable name in the interface? --- src/Uuid/IUuidGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Uuid/IUuidGenerator.php b/src/Uuid/IUuidGenerator.php index bcf58e7..70a2229 100644 --- a/src/Uuid/IUuidGenerator.php +++ b/src/Uuid/IUuidGenerator.php @@ -40,5 +40,5 @@ public function generateV4(); * * @return String Valid v5 UUID. */ - public function generateV5($str, $namespace = NULL); + public function generateV5($name, $namespace = NULL); } From a6267481977a77c667918b968208329a955cd573 Mon Sep 17 00:00:00 2001 From: Jared Whiklo Date: Thu, 25 Feb 2016 11:03:56 -0600 Subject: [PATCH 4/4] Allow for string or UUID namespaces --- src/Uuid/UuidGenerator.php | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/Uuid/UuidGenerator.php b/src/Uuid/UuidGenerator.php index 4b0b773..bf9428a 100644 --- a/src/Uuid/UuidGenerator.php +++ b/src/Uuid/UuidGenerator.php @@ -5,20 +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; - protected $namespace_uuid; + /** + * @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; - $this->namespace_uuid = Uuid::uuid(Uuid::NAMESPACE_DNS, $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); + } } /** @@ -33,15 +47,25 @@ 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)) { - return Uuid::uuid5(Uuid::uuid5(Uuid::NAMESPACE_DNS, $namespace), $str)->toString(); + // 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($namespace_uuid, $str)->toString(); + return Uuid::uuid5($this->namespace, $str)->toString(); } }