Skip to content

Commit 6ed877f

Browse files
authored
Merge pull request #29 from teodino93/patch-1
Fix special characters in parsing serial numbers
2 parents 74ca7e6 + 43254e3 commit 6ed877f

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/SerialNumberGenerator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ public function parse(string $serialNumber): array
163163

164164
protected function formatToRegex(): string
165165
{
166+
$format = preg_replace('/([^\w\s])/i', '\\\\$1', $this->format);
167+
166168
$value = preg_replace_callback_array(
167169
[
168170
'/P+/' => fn ($matches) => ($matches[0] && $length = mb_strlen($matches[0])) ? "(?<prefix>[a-zA-Z]{{$length}})" : '',
@@ -171,7 +173,7 @@ protected function formatToRegex(): string
171173
'/Y+/' => fn ($matches) => ($matches[0] && $length = mb_strlen($matches[0])) ? "(?<year>\d{{$length}})" : '',
172174
'/C+/' => fn ($matches) => ($matches[0] && $length = mb_strlen($matches[0])) ? "(?<count>\d{{$length}})" : '',
173175
],
174-
$this->format
176+
$format
175177
);
176178

177179
return is_string($value) ? $value : '';

tests/Unit/SerialNumberTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,6 @@
5252
['YYCCCC', '220002', null, null, 22, 2],
5353
['YYPPCCCSSSS', '22IN0020001', 'IN', 1, 22, 2],
5454
['YYPPCCCSSSS', '22CS0020001', 'CS', 1, 22, 2],
55+
['YYPPCCC/SSSS', '22CS002/0001', 'CS', 1, 22, 2],
56+
['CCCC/YY', '0002/25', null, null, 25, 2],
5557
]);

0 commit comments

Comments
 (0)