diff --git a/src/Uuid/IUuidGenerator.php b/src/Uuid/IUuidGenerator.php new file mode 100644 index 0000000..70a2229 --- /dev/null +++ b/src/Uuid/IUuidGenerator.php @@ -0,0 +1,44 @@ + + * @license http://www.gnu.org/licenses/gpl-3.0.en.html GPL + * @link http://www.islandora.ca + */ + +namespace Islandora\Chullo\Uuid; + +/** + * Interface for generating UUIDs. + * + * @category Islandora + * @package Islandora + * @author Daniel Lamb + * @license http://www.gnu.org/licenses/gpl-3.0.en.html GPL + * @link http://www.islandora.ca + */ +interface IUuidGenerator { + + /** + * Generates a v4 UUID. + * + * @return String Valid v4 UUID. + */ + public function generateV4(); + + /** + * Generates a v5 UUID. + * + * @return String Valid v5 UUID. + */ + public function generateV5($name, $namespace = NULL); +} diff --git a/src/Uuid/UuidGenerator.php b/src/Uuid/UuidGenerator.php new file mode 100644 index 0000000..bf9428a --- /dev/null +++ b/src/Uuid/UuidGenerator.php @@ -0,0 +1,72 @@ +namespace = $namespace; + } + // Otherwise generate a namespace UUID from the passed in namespace. + else { + $this->namespace = Uuid::uuid5(Uuid::NAMESPACE_DNS, $namespace); + } + } + + /** + * Generates a v4 UUID. + * + * @return String Valid v4 UUID. + */ + public function generateV4() { + return Uuid::uuid4()->toString(); + } + + /** + * 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)) { + // 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(); + } + } + +}