Skip to content

Commit b513c45

Browse files
committed
clean regex
1 parent 4fceba7 commit b513c45

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/SerialNumberGenerator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ protected function formatToRegex(): string
165165
{
166166
$value = preg_replace_callback_array(
167167
[
168-
'/[^\w\s]/i' => fn ($matches) => $matches[0] ? "\\{$matches[0]}" : '',
169-
'/P+/' => fn ($matches) => ($matches[0] && $length = mb_strlen($matches[0])) ? "(?<prefix>[a-zA-Z]{{$length}})" : '',
170-
'/S+/' => fn ($matches) => ($matches[0] && $length = mb_strlen($matches[0])) ? "(?<serie>\d{{$length}})" : '',
171-
'/M+/' => fn ($matches) => ($matches[0] && $length = mb_strlen($matches[0])) ? "(?<month>\d{{$length}})" : '',
172-
'/Y+/' => fn ($matches) => ($matches[0] && $length = mb_strlen($matches[0])) ? "(?<year>\d{{$length}})" : '',
173-
'/C+/' => fn ($matches) => ($matches[0] && $length = mb_strlen($matches[0])) ? "(?<count>\d{{$length}})" : '',
168+
'/[^\w\s]/i' => fn ($matches) => "\\{$matches[0]}",
169+
'/P+/' => fn ($matches) => ($length = mb_strlen($matches[0])) ? "(?<prefix>[a-zA-Z]{{$length}})" : '',
170+
'/S+/' => fn ($matches) => ($length = mb_strlen($matches[0])) ? "(?<serie>\d{{$length}})" : '',
171+
'/M+/' => fn ($matches) => ($length = mb_strlen($matches[0])) ? "(?<month>\d{{$length}})" : '',
172+
'/Y+/' => fn ($matches) => ($length = mb_strlen($matches[0])) ? "(?<year>\d{{$length}})" : '',
173+
'/C+/' => fn ($matches) => ($length = mb_strlen($matches[0])) ? "(?<count>\d{{$length}})" : '',
174174
],
175175
$this->format
176176
);

tests/Unit/SerialNumberTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
['CCCC\YY', '0002\25', null, null, 25, null, 2],
2222
['CCCC.YY', '0002.25', null, null, 25, null, 2],
2323
['CCCC|YY', '0002|25', null, null, 25, null, 2],
24+
['PPP-SSS..CCCC/YY', 'INV-999..0001/25', 'INV', 999, 25, null, 1],
2425
];
2526

2627
it('can generate serial number from format', function ($format, $serialNumber, $prefix, $serie, $year, $month, $count) {

0 commit comments

Comments
 (0)