Skip to content

Commit 405a7e5

Browse files
committed
Add UuidGenerator
1 parent 5b1ff3b commit 405a7e5

4 files changed

Lines changed: 90 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ vivait_string_generator:
3232
string: vivait_generator.generator.string
3333
secure_bytes: vivait_generator.generator.secure_bytes
3434
secure_string: vivait_generator.generator.secure_string
35+
uuid_string: vivait_generator.generator.uuid_string
3536
```
3637
3738
## Bundled generators

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"symfony/polyfill-php70": "^1.3",
1818
"symfony/security": "^2.8|^3.0",
1919
"symfony/options-resolver": "^2.8|^3.0",
20-
"ircmaxell/random-lib": "~1.0"
20+
"ircmaxell/random-lib": "~1.0",
21+
"ramsey/uuid": "^3.6"
2122
},
2223
"require-dev": {
2324
"phpspec/phpspec": "~2.0"
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Vivait\StringGeneratorBundle\Generator;
4+
5+
use Ramsey\Uuid\Uuid;
6+
use Symfony\Component\OptionsResolver\OptionsResolver;
7+
use Vivait\StringGeneratorBundle\Model\ConfigurableGeneratorInterface;
8+
9+
class UuidGenerator implements ConfigurableGeneratorInterface
10+
{
11+
/**
12+
* @var integer
13+
*/
14+
private $version;
15+
16+
/**
17+
* @var string
18+
*/
19+
private $namespace;
20+
21+
/**
22+
* Creates a random string based on a length and alphabet
23+
*
24+
* @return string
25+
*/
26+
public function generate()
27+
{
28+
switch($this->version) {
29+
case 1:
30+
return Uuid::uuid1()->toString();
31+
32+
case 3:
33+
$this->checkNamespace($this->version);
34+
return Uuid::uuid3(Uuid::NAMESPACE_DNS, $this->namespace)->toString();
35+
36+
case 4:
37+
return Uuid::uuid4()->toString();
38+
39+
case 5:
40+
return Uuid::uuid5(Uuid::NAMESPACE_DNS, $this->namespace)->toString();
41+
42+
default:
43+
throw new \RuntimeException(sprintf('The version %s of UUID does not exists',$this->version));
44+
}
45+
}
46+
47+
/**
48+
* @param integer $version
49+
*/
50+
private function checkNamespace($version)
51+
{
52+
if(null === $this->namespace) {
53+
throw new \RuntimeException(sprintf('The version %s of UUID needs a namespace', $version));
54+
}
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
public function setOptions(array $options)
61+
{
62+
$this->version = $options['version'];
63+
$this->namespace = $options['namespace'];
64+
}
65+
66+
/**
67+
* {@inheritdoc}
68+
*/
69+
public function getDefaultOptions(OptionsResolver $resolver)
70+
{
71+
$resolver->setDefaults([
72+
'version' => 4,
73+
'namespace' => null
74+
]);
75+
}
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
public function setLength($length)
81+
{
82+
return null;
83+
}
84+
}

src/Vivait/StringGeneratorBundle/Resources/config/services.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ services:
2222
class: Vivait\StringGeneratorBundle\Generator\SecureStringGenerator
2323
arguments: ["@vivait_generator.randomlib"]
2424

25+
vivait_generator.generator.uuid_string:
26+
class: Vivait\StringGeneratorBundle\Generator\UuidGenerator
27+
2528
vivait_generator.randomlib:
2629
class: RandomLib\Factory

0 commit comments

Comments
 (0)