Skip to content

Commit affa227

Browse files
committed
Improve ITF-14 code
1 parent 7567f6d commit affa227

File tree

5 files changed

+60
-22
lines changed

5 files changed

+60
-22
lines changed

src/Types/TypeITF14.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,26 @@ public function getBarcodeData(string $code): Barcode
2929
$chr['A'] = '11';
3030
$chr['Z'] = '21';
3131

32-
if (strlen($code) === 13) {
33-
$code .= $this->getChecksum($code);
32+
if (strlen($code) < 13 || strlen($code) > 14) {
33+
throw new InvalidLengthException();
3434
}
3535

36-
if (strlen($code) > 14 || strlen($code) < 13) {
37-
throw new InvalidLengthException();
36+
if (strlen($code) === 13) {
37+
$code .= $this->getChecksum($code);
3838
}
3939

4040
$barcode = new Barcode($code);
4141

4242
// Add start and stop codes
4343
$code = 'AA' . strtolower($code) . 'ZA';
4444

45+
// Loop through 2 chars at once
4546
for ($charIndex = 0; $charIndex < strlen($code); $charIndex += 2) {
4647
if (! isset($chr[$code[$charIndex]]) || ! isset($chr[$code[$charIndex + 1]])) {
4748
throw new InvalidCharacterException();
4849
}
4950

50-
$bars = true;
51+
$drawBar = true;
5152
$pbars = $chr[$code[$charIndex]];
5253
$pspaces = $chr[$code[$charIndex + 1]];
5354
$pmixed = '';
@@ -58,18 +59,9 @@ public function getBarcodeData(string $code): Barcode
5859
$pspaces = substr($pspaces, 1);
5960
}
6061

61-
$pmixedarr = str_split($pmixed);
62-
63-
foreach ($pmixedarr as $x) {
64-
if ($bars) {
65-
$t = true;
66-
} else {
67-
$t = false;
68-
}
69-
$width = ($x === '1') ? '1' : '2';
70-
71-
$barcode->addBar(new BarcodeBar($width, 1, $t));
72-
$bars = ! $bars;
62+
foreach (str_split($pmixed) as $width) {
63+
$barcode->addBar(new BarcodeBar($width, 1, $drawBar));
64+
$drawBar = ! $drawBar;
7365
}
7466
}
7567

src/Types/TypeInterleaved25Checksum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function getBarcodeData(string $code): Barcode
3737
// add leading zero if code-length is odd
3838
$code = '0' . $code;
3939
}
40+
4041
// add start and stop codes
4142
$code = 'AA' . strtolower($code) . 'ZA';
4243

tests/VerifiedBarcodeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
/*
77
* Test all supported barcodes types, with as much different but supported input strings.
8-
* Verified files can be build with generate-verified-files.php file.
8+
* Verified files can be built with generate-verified-files.php file.
99
* Only run that file if you added new types or new strings to test.
1010
*
1111
* We use SVG because that output is vector and should be the same on every host system.
@@ -24,7 +24,7 @@ class VerifiedBarcodeTest extends TestCase
2424
['type' => BarcodeGenerator::TYPE_INTERLEAVED_2_5, 'barcodes' => ['1234567890']],
2525
['type' => BarcodeGenerator::TYPE_INTERLEAVED_2_5_CHECKSUM, 'barcodes' => ['1234567890']],
2626
['type' => BarcodeGenerator::TYPE_EAN_13, 'barcodes' => ['081231723897', '0049000004632', '004900000463']],
27-
['type' => BarcodeGenerator::TYPE_ITF_14, 'barcodes' => ['00012345600012']],
27+
['type' => BarcodeGenerator::TYPE_ITF_14, 'barcodes' => ['00012345600012', '05400141288766']],
2828
['type' => BarcodeGenerator::TYPE_CODE_128, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']],
2929
['type' => BarcodeGenerator::TYPE_CODE_128_A, 'barcodes' => ['1234567890']],
3030
['type' => BarcodeGenerator::TYPE_CODE_128_B, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']],

tests/verified-files/ITF14-00012345600012.svg

Lines changed: 2 additions & 3 deletions
Loading
Lines changed: 46 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)