Skip to content

Commit 629ffd1

Browse files
committed
prevent Prefix collision
1 parent a462609 commit 629ffd1

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/SerialNumberGenerator.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@ public function generate(int $count, ?int $serie = null, ?Carbon $date = null):
1818
{
1919
return preg_replace_callback_array(
2020
[
21-
'/P+/' => function ($matches) {
22-
if (! $matches[0]) {
23-
return '';
24-
}
25-
$slotLength = strlen($matches[0]);
26-
$prefixLength = strlen($this->prefix);
27-
28-
throw_if(
29-
$prefixLength < $slotLength,
30-
"The serial Number can't be formatted, the prefix provided is $prefixLength letters long ({$this->prefix}), while the format require at minimum a $slotLength letters long prefix"
31-
);
32-
33-
return substr($this->prefix, 0, strlen($matches[0]));
34-
},
3521
'/S+/' => function ($matches) use ($serie) {
3622
if (! $matches[0]) {
3723
return '';
@@ -70,6 +56,20 @@ public function generate(int $count, ?int $serie = null, ?Carbon $date = null):
7056
STR_PAD_LEFT
7157
);
7258
},
59+
'/P+/' => function ($matches) {
60+
if (! $matches[0]) {
61+
return '';
62+
}
63+
$slotLength = strlen($matches[0]);
64+
$prefixLength = strlen($this->prefix);
65+
66+
throw_if(
67+
$prefixLength < $slotLength,
68+
"The serial Number can't be formatted, the prefix provided is $prefixLength letters long ({$this->prefix}), while the format require at minimum a $slotLength letters long prefix"
69+
);
70+
71+
return substr($this->prefix, 0, strlen($matches[0]));
72+
},
7373
],
7474
$this->format
7575
);

0 commit comments

Comments
 (0)