Skip to content

Commit 3ec9f77

Browse files
committed
Merge pull request #6 from mayeco/optional_unique
Add new unique option in Annotation
2 parents f236024 + b6ac981 commit 3ec9f77

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

Annotation/StringGenerator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Doctrine\Common\Annotations\Annotation;
66

7-
87
/**
98
* @Annotation
109
*
@@ -28,6 +27,11 @@ class StringGenerator extends Annotation
2827
*/
2928
public $length = 8;
3029

30+
/**
31+
* @var boolean
32+
*/
33+
public $unique = true;
34+
3135
/**
3236
* @var string
3337
*/
@@ -37,4 +41,4 @@ class StringGenerator extends Annotation
3741
* @var string
3842
*/
3943
public $prefix_callback;
40-
}
44+
}

EventListener/StringGeneratorListener.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ private function generateId($property, Vivait\StringGenerator $annotation)
7272
$str = sprintf("%s%s%s", $annotation->prefix, $annotation->separator, $str);
7373
}
7474

75+
if (!$annotation->unique) {
76+
return $str;
77+
}
78+
7579
if ($this->repo->findOneBy([$property => $str])) {
7680
return $this->generateId($property, $annotation);
7781
} else {

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ Setting `alphabet` limits the characters the generator can choose from. Defaults
8686

8787
@Vivait\StringGenerator(alphabet="abcdefghkmnpqrstuwxyz")
8888

89+
### Unique
90+
91+
Setting `unique` is boolean and tell if the string must be unique or not, by default `true`
92+
93+
@Vivait\StringGenerator(unique="false")
94+
8995
## Custom generator
9096

9197
If you want to use a different generator to create your string, create a class that implements namespace

0 commit comments

Comments
 (0)