1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace App\Services\LabelSystem\Barcodes; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
final class BarcodeNormalizer |
25
|
|
|
{ |
26
|
|
|
private const PREFIX_TYPE_MAP = [ |
27
|
|
|
'L' => 'lot', |
28
|
|
|
'P' => 'part', |
29
|
|
|
'S' => 'location', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Parses barcode content and normalizes it. |
34
|
|
|
* Returns an array in the format ['part', 1]: First entry contains element type, second the ID of the element |
35
|
|
|
* @param string $input |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
public function normalizeBarcodeContent(string $input): array |
39
|
|
|
{ |
40
|
|
|
$input = trim($input); |
41
|
|
|
$matches = []; |
42
|
|
|
|
43
|
|
|
//Some scanner output '-' as ß, so replace it (ß is never used, so we can replace it safely) |
44
|
|
|
$input = str_replace('ß', '-', $input); |
45
|
|
|
|
46
|
|
|
//Extract parts from QR code's URL |
47
|
|
|
if (preg_match('#^https?://.*/scan/(\w+)/(\d+)/?$#', $input, $matches)) { |
48
|
|
|
return [$matches[1], (int) $matches[2]]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
//New Code39 barcode use L0001 format |
52
|
|
|
if (preg_match('#^([A-Z])(\d{4,})$#', $input, $matches)) { |
53
|
|
|
$prefix = $matches[1]; |
54
|
|
|
$id = (int) $matches[2]; |
55
|
|
|
|
56
|
|
|
if (!isset(self::PREFIX_TYPE_MAP[$prefix])) { |
57
|
|
|
throw new \InvalidArgumentException('Unknown prefix ' . $prefix); |
58
|
|
|
} |
59
|
|
|
return [self::PREFIX_TYPE_MAP[$prefix], $id]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
//During development the L-000001 format was used |
63
|
|
|
if (preg_match('#^(\w)-(\d{6,})$#', $input, $matches)) { |
64
|
|
|
$prefix = $matches[1]; |
65
|
|
|
$id = (int) $matches[2]; |
66
|
|
|
|
67
|
|
|
if (!isset(self::PREFIX_TYPE_MAP[$prefix])) { |
68
|
|
|
throw new \InvalidArgumentException('Unknown prefix ' . $prefix); |
69
|
|
|
} |
70
|
|
|
return [self::PREFIX_TYPE_MAP[$prefix], $id]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
//Legacy Part-DB location labels used $L00336 format |
74
|
|
|
if (preg_match('#^\$L(\d{5,})$#', $input, $matches)) { |
75
|
|
|
return ['location', (int) $matches[1]]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
//Legacy Part-DB used EAN8 barcodes for part labels. Format 0000001(2) (note the optional 8th digit => checksum) |
79
|
|
|
if (preg_match('#^(\d{7})\d?$#', $input, $matches)) { |
80
|
|
|
return ['part', (int) $matches[1]]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
throw new \InvalidArgumentException('Unknown barcode format!'); |
85
|
|
|
} |
86
|
|
|
} |