Skip to content

Commit 3aed3ae

Browse files
authored
Merge pull request #30 from terox/uuid-generator
Uuid generator
2 parents caa509f + 6929207 commit 3aed3ae

6 files changed

Lines changed: 205 additions & 0 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ install:
2929
- if [ "$DEPENDENCIES" == "dev" ]; then perl -pi -e 's/^}$/,"minimum-stability":"dev"}/' composer.json; fi;
3030
- if [ "$DEPENDENCIES" != "low" ]; then composer update; fi;
3131
- if [ "$DEPENDENCIES" == "low" ]; then composer update --prefer-lowest; fi;
32+
- composer require ramsey/uuid
3233

3334
before_script:
3435
- echo "<?php if (PHP_VERSION_ID >= 50400) echo ',@php5.4';" > php_version_tags.php

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ Generates a secure random byte string using the `Symfony\Component\Security\Core
4949
@Generate(generator="secure_bytes", options={"length"=8})
5050
```
5151

52+
### `UuidStringGenerator`
53+
***For use this generator you should require the package ```ramsey/uuid``` in your application.***
54+
55+
For generating a UUID:
56+
57+
```php
58+
@Generate(generator="uuid_string", options={"version"=4})
59+
```
60+
61+
You can use also namespaced versions (v3 and v5). For example with the v5:
62+
```php
63+
@Generate(generator="uuid_string", options={"version"=5}, namespace="my_namespace")
64+
```
65+
5266
## Usage
5367
Add the `@Generate(generator="generator_name")` annotation to an entity property
5468
(where `generator_name` is the name of a generator defined in the configuration).

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"symfony/dependency-injection": "^2.7|^3.3",
2222
"symfony/config": "^2.7|^3.3"
2323
},
24+
"suggest": {
25+
"ramsey/uuid": "To use the UUID generator you should require this package"
26+
},
2427
"require-dev": {
2528
"phpspec/phpspec": "~2.0"
2629
},
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 it_should_throw_an_exception_when_no_namespace_on_version_3()
15+
{
16+
$this->setVersion(3);
17+
$this->setNamespace(null);
18+
$this->shouldThrow('\RuntimeException')->duringGenerate();
19+
}
20+
21+
function it_should_throw_an_exception_when_no_namespace_on_version_5()
22+
{
23+
$this->setVersion(5);
24+
$this->setNamespace(null);
25+
$this->shouldThrow('\RuntimeException')->duringGenerate();
26+
}
27+
28+
function it_generates_a_uuid_based_on_version_1()
29+
{
30+
$this->setVersion(1);
31+
$this->generate()->shouldBeString();
32+
$this->generate()->shouldHaveStrlen(36);
33+
}
34+
35+
function it_generates_a_uuid_based_on_version_3()
36+
{
37+
$this->setVersion(3);
38+
$this->setNamespace('testing');
39+
$this->generate()->shouldBeString();
40+
$this->generate()->shouldHaveStrlen(36);
41+
}
42+
43+
function it_generates_a_uuid_based_on_version_4()
44+
{
45+
$this->setVersion(4);
46+
$this->generate()->shouldBeString();
47+
$this->generate()->shouldHaveStrlen(36);
48+
}
49+
50+
function it_generates_a_uuid_based_on_version_5()
51+
{
52+
$this->setVersion(5);
53+
$this->setNamespace('testing');
54+
$this->generate()->shouldBeString();
55+
$this->generate()->shouldHaveStrlen(36);
56+
}
57+
58+
function getMatchers()
59+
{
60+
return [
61+
'haveStrlen' => function($string, $length){
62+
return strlen($string) == $length;
63+
}
64+
];
65+
}
66+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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 Uuid
13+
*/
14+
private $uuid;
15+
16+
/**
17+
* @var integer
18+
*/
19+
private $version;
20+
21+
/**
22+
* @var string
23+
*/
24+
private $namespace;
25+
26+
/**
27+
* Creates a random string based on a length and alphabet
28+
*
29+
* @return string
30+
*/
31+
public function generate()
32+
{
33+
if(!class_exists('Ramsey\Uuid\Uuid')) {
34+
throw new \RuntimeException('For use the UUID generator you should setup the ramsey/uuid package');
35+
}
36+
37+
switch($this->version) {
38+
case 1:
39+
return Uuid::uuid1()->toString();
40+
41+
case 3:
42+
$this->checkNamespace($this->version);
43+
return Uuid::uuid3(Uuid::NAMESPACE_DNS, $this->namespace)->toString();
44+
45+
case 4:
46+
return Uuid::uuid4()->toString();
47+
48+
case 5:
49+
$this->checkNamespace($this->version);
50+
return Uuid::uuid5(Uuid::NAMESPACE_DNS, $this->namespace)->toString();
51+
52+
default:
53+
throw new \RuntimeException(sprintf('The version %s of UUID does not exists',$this->version));
54+
}
55+
}
56+
57+
/**
58+
* @param integer $version
59+
*/
60+
private function checkNamespace($version)
61+
{
62+
if(null === $this->namespace) {
63+
throw new \RuntimeException(sprintf('The version %s of UUID needs a namespace', $version));
64+
}
65+
}
66+
67+
/**
68+
* {@inheritdoc}
69+
*/
70+
public function setOptions(array $options)
71+
{
72+
$this->version = $options['version'];
73+
$this->namespace = $options['namespace'];
74+
}
75+
76+
/**
77+
* {@inheritdoc}
78+
*/
79+
public function getDefaultOptions(OptionsResolver $resolver)
80+
{
81+
$resolver->setDefaults([
82+
'version' => 4,
83+
'namespace' => null
84+
]);
85+
}
86+
87+
/**
88+
* {@inheritdoc}
89+
*/
90+
public function setLength($length)
91+
{
92+
return null;
93+
}
94+
95+
/**
96+
* @param $version
97+
* @return $this
98+
*/
99+
public function setVersion($version)
100+
{
101+
$this->version = $version;
102+
103+
return $this;
104+
}
105+
106+
/**
107+
* @param $namespace
108+
* @return $this
109+
*/
110+
public function setNamespace($namespace)
111+
{
112+
$this->namespace = $namespace;
113+
114+
return $this;
115+
}
116+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,10 @@ services:
2828
tags:
2929
- { name: vivait_generator.generator, alias: 'secure_string' }
3030

31+
vivait_generator.generator.uuid_string:
32+
class: Vivait\StringGeneratorBundle\Generator\UuidGenerator
33+
tags:
34+
- { name: vivait_generator.generator, alias: 'uuid_string' }
35+
3136
vivait_generator.randomlib:
3237
class: RandomLib\Factory

0 commit comments

Comments
 (0)