Skip to content

Commit 01b6148

Browse files
author
Robin Cawser
committed
Separate generator into its own class
1 parent 6e8a691 commit 01b6148

3 files changed

Lines changed: 59 additions & 20 deletions

File tree

EventListener/IdGeneratorListener.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Doctrine\Common\Annotations\Reader;
66
use Doctrine\ORM\Event\LifecycleEventArgs;
77
use Vivait\StringGeneratorBundle\Annotation as Vivait;
8+
use Vivait\StringGeneratorBundle\Generator\StringGenerator;
9+
use Vivait\StringGeneratorBundle\Model\StringGeneratorInterface;
810

911
class IdGeneratorListener
1012
{
@@ -56,7 +58,12 @@ public function prePersist(LifecycleEventArgs $args)
5658
*/
5759
private function generateId($property, Vivait\StringGenerator $annotation)
5860
{
59-
$id = $this->createId($annotation->alphabet, $annotation->length);
61+
$generator = new StringGenerator();
62+
$generator
63+
->setAlphabet($annotation->alphabet)
64+
->setLength($annotation->length);
65+
66+
$id = $$this->generator->generate();
6067

6168
if ($annotation->prefix) {
6269
$id = sprintf("%s%s%s", $annotation->prefix, $annotation->separator, $id);
@@ -69,20 +76,5 @@ private function generateId($property, Vivait\StringGenerator $annotation)
6976
}
7077
}
7178

72-
/**
73-
* @param $alphabet
74-
* @param $length
75-
* @return string
76-
*/
77-
private function createId($alphabet, $length)
78-
{
79-
$id = [];
80-
$alphaLength = strlen($alphabet) - 1;
81-
for ($i = 0; $i < $length; $i++) {
82-
$n = rand(0, $alphaLength);
83-
$id[] = $alphabet[$n];
84-
}
8579

86-
return implode($id);
87-
}
8880
}

Generator/StringGenerator.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Vivait\StringGeneratorBundle\Generator;
4+
5+
use Vivait\StringGeneratorBundle\Model\StringGeneratorInterface;
6+
7+
class StringGenerator implements StringGeneratorInterface
8+
{
9+
10+
private $alphabet;
11+
private $length;
12+
13+
public function setAlphabet($alphabet)
14+
{
15+
$this->alphabet = $alphabet;
16+
return $this;
17+
}
18+
19+
public function setLength($length)
20+
{
21+
$this->length = $length;
22+
return $this;
23+
}
24+
25+
public function generate()
26+
{
27+
$str = [];
28+
$alphaLength = strlen($this->alphabet) - 1;
29+
for ($i = 0; $i < $$this->length; $i++) {
30+
$n = rand(0, $alphaLength);
31+
$str[] = $this->alphabet[$n];
32+
}
33+
34+
return implode($str);
35+
}
36+
}

Model/StringGeneratorInterface.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@
22

33
namespace Vivait\StringGeneratorBundle\Model;
44

5-
interface StringGeneratorInterface {
5+
interface StringGeneratorInterface
6+
{
7+
8+
/**
9+
* @param $alphabet
10+
* @return $this
11+
*/
12+
public function setAlphabet($alphabet);
13+
14+
/**
15+
* @param $length
16+
* @return $this
17+
*/
18+
public function setLength($length);
619

720
/**
8-
* Creates a random string
9-
*
1021
* @return string
1122
*/
12-
public static function generate();
23+
public function generate();
1324
}

0 commit comments

Comments
 (0)