Skip to content

Commit 059dea7

Browse files
committed
UUID Generator
1 parent 90d8645 commit 059dea7

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace spec\Vivait\StringGeneratorBundle\Generator;
4+
5+
use PhpSpec\ObjectBehavior;
6+
7+
class UuidGeneratorSpec extends ObjectBehavior
8+
{
9+
function it_is_initializable()
10+
{
11+
$this->shouldHaveType('Vivait\StringGeneratorBundle\Generator\UuidGenerator');
12+
}
13+
14+
function let()
15+
{
16+
17+
}
18+
19+
function it_should_throw_an_exception_when_no_namespace_on_version_3()
20+
{
21+
$this->setVersion(3);
22+
$this->setNamespace(null);
23+
$this->shouldThrow('\RuntimeException')->duringGenerate();
24+
}
25+
26+
function it_should_throw_an_exception_when_no_namespace_on_version_5()
27+
{
28+
$this->setVersion(5);
29+
$this->setNamespace(null);
30+
$this->shouldThrow('\RuntimeException')->duringGenerate();
31+
}
32+
33+
function it_generates_a_uuid_based_on_version_1()
34+
{
35+
$this->setVersion(1);
36+
$this->generate()->shouldBeString();
37+
$this->generate()->shouldHaveStrlen(36);
38+
}
39+
40+
function it_generates_a_uuid_based_on_version_3()
41+
{
42+
$this->setVersion(3);
43+
$this->setNamespace('testing');
44+
$this->generate()->shouldBeString();
45+
$this->generate()->shouldHaveStrlen(36);
46+
}
47+
48+
function it_generates_a_uuid_based_on_version_4()
49+
{
50+
$this->setVersion(4);
51+
$this->generate()->shouldBeString();
52+
$this->generate()->shouldHaveStrlen(36);
53+
}
54+
55+
function it_generates_a_uuid_based_on_version_5()
56+
{
57+
$this->setVersion(5);
58+
$this->setNamespace('testing');
59+
$this->generate()->shouldBeString();
60+
$this->generate()->shouldHaveStrlen(36);
61+
}
62+
63+
function getMatchers()
64+
{
65+
return [
66+
'haveStrlen' => function($string, $length){
67+
return strlen($string) == $length;
68+
}
69+
];
70+
}
71+
}

src/Vivait/StringGeneratorBundle/Generator/UuidGenerator.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
class UuidGenerator implements ConfigurableGeneratorInterface
1010
{
11+
/**
12+
* @var Uuid
13+
*/
14+
private $uuid;
15+
1116
/**
1217
* @var integer
1318
*/
@@ -18,6 +23,14 @@ class UuidGenerator implements ConfigurableGeneratorInterface
1823
*/
1924
private $namespace;
2025

26+
/**
27+
* Constructor.
28+
*/
29+
public function __construct()
30+
{
31+
32+
}
33+
2134
/**
2235
* Creates a random string based on a length and alphabet
2336
*
@@ -37,6 +50,7 @@ public function generate()
3750
return Uuid::uuid4()->toString();
3851

3952
case 5:
53+
$this->checkNamespace($this->version);
4054
return Uuid::uuid5(Uuid::NAMESPACE_DNS, $this->namespace)->toString();
4155

4256
default:
@@ -81,4 +95,26 @@ public function setLength($length)
8195
{
8296
return null;
8397
}
98+
99+
/**
100+
* @param $version
101+
* @return $this
102+
*/
103+
public function setVersion($version)
104+
{
105+
$this->version = $version;
106+
107+
return $this;
108+
}
109+
110+
/**
111+
* @param $namespace
112+
* @return $this
113+
*/
114+
public function setNamespace($namespace)
115+
{
116+
$this->namespace = $namespace;
117+
118+
return $this;
119+
}
84120
}

0 commit comments

Comments
 (0)