Skip to content

Commit

Permalink
Merge pull request #37 from whikloj/updates-thinCollection
Browse files Browse the repository at this point in the history
Updates for thin collection controller
  • Loading branch information
DiegoPino committed Mar 23, 2016
2 parents e1e55a6 + a626748 commit 3202850
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Uuid/IUuidGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* This file is part of Islandora.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* PHP Version 5.5.9
*
* @category Islandora
* @package Islandora
* @author Daniel Lamb <daniel@discoverygarden.ca>
* @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 <daniel@discoverygarden.ca>
* @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);
}
72 changes: 72 additions & 0 deletions src/Uuid/UuidGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Islandora\Chullo\Uuid;

use Ramsey\Uuid\Uuid;

/**
* 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";
}

// 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);
}
}

/**
* 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();
}
}

}

0 comments on commit 3202850

Please sign in to comment.