Skip to content

Commit 922940b

Browse files
authored
Merge pull request #186 from picqer/itf14-improvements
ITF-14 improvements
2 parents cc09761 + affa227 commit 922940b

File tree

6 files changed

+100
-88
lines changed

6 files changed

+100
-88
lines changed

phpunit.xml

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
colors="true"
5-
convertErrorsToExceptions="true"
6-
convertNoticesToExceptions="true"
7-
convertWarningsToExceptions="true"
8-
processIsolation="false"
9-
stopOnError="false"
10-
stopOnFailure="false"
11-
verbose="true"
12-
>
13-
<testsuites>
14-
<testsuite name="Barcode Test Suite">
15-
<directory>./tests/</directory>
16-
</testsuite>
17-
</testsuites>
18-
<filter>
19-
<whitelist>
20-
<directory>./src</directory>
21-
</whitelist>
22-
</filter>
23-
</phpunit>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnError="false" stopOnFailure="false" verbose="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory>./src</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Barcode Test Suite">
10+
<directory>./tests/</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

src/Types/TypeITF14.php

Lines changed: 36 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Picqer\Barcode\BarcodeBar;
77
use Picqer\Barcode\Exceptions\InvalidCharacterException;
88
use Picqer\Barcode\Exceptions\InvalidLengthException;
9-
use Picqer\Barcode\Types\TypeInterface;
109

1110
class TypeITF14 implements TypeInterface
1211
{
@@ -16,9 +15,7 @@ class TypeITF14 implements TypeInterface
1615
*/
1716
public function getBarcodeData(string $code): Barcode
1817
{
19-
$barcode = new Barcode($code);
20-
21-
$chr = array();
18+
$chr = [];
2219
$chr['0'] = '11221';
2320
$chr['1'] = '21112';
2421
$chr['2'] = '12112';
@@ -29,84 +26,62 @@ public function getBarcodeData(string $code): Barcode
2926
$chr['7'] = '11122';
3027
$chr['8'] = '21121';
3128
$chr['9'] = '12121';
29+
$chr['A'] = '11';
30+
$chr['Z'] = '21';
3231

33-
if (strlen($code) === 13) {
34-
$total = 0;
35-
36-
for ($i = 0; $i <= strlen($code) - 1; $i++) {
37-
$temp = intval($code . substr($i, 1));
38-
$total += $temp * (($i === 0 || $i % 2 === 0) ? 3 : 1);
39-
}
40-
41-
$cs = $total % 10;
42-
$cs = 10 - $cs;
43-
if ($cs === 10) {
44-
$cs = 0;
45-
}
46-
47-
$code .= (string) $cs;
48-
}
49-
50-
if (strlen($code) > 14 || strlen($code) < 13) {
32+
if (strlen($code) < 13 || strlen($code) > 14) {
5133
throw new InvalidLengthException();
5234
}
5335

54-
$k = 0;
55-
$pbegin = "1010";
56-
$pbeginarr = str_split($pbegin);
36+
if (strlen($code) === 13) {
37+
$code .= $this->getChecksum($code);
38+
}
5739

58-
foreach ($pbeginarr as $x) {
59-
$t = $x === '1';
60-
$w = 1;
40+
$barcode = new Barcode($code);
6141

62-
$barcode->addBar(new BarcodeBar($w, 1, $t));
63-
++$k;
64-
}
42+
// Add start and stop codes
43+
$code = 'AA' . strtolower($code) . 'ZA';
6544

66-
for ($i = 0; $i < strlen($code); $i += 2) {
67-
if (!isset($chr[$code[$i]]) || !isset($chr[$code[$i + 1]])) {
45+
// Loop through 2 chars at once
46+
for ($charIndex = 0; $charIndex < strlen($code); $charIndex += 2) {
47+
if (! isset($chr[$code[$charIndex]]) || ! isset($chr[$code[$charIndex + 1]])) {
6848
throw new InvalidCharacterException();
6949
}
7050

71-
$bars = true;
72-
$pbars = $chr[$code[$i]];
73-
$pspaces = $chr[$code[$i + 1]];
74-
$pmixed = "";
75-
51+
$drawBar = true;
52+
$pbars = $chr[$code[$charIndex]];
53+
$pspaces = $chr[$code[$charIndex + 1]];
54+
$pmixed = '';
7655

7756
while (strlen($pbars) > 0) {
78-
$pmixed .= $pbars[0] . $pspaces[0];
79-
$pbars = substr($pbars, 1);
57+
$pmixed .= $pbars[0] . $pspaces[0];
58+
$pbars = substr($pbars, 1);
8059
$pspaces = substr($pspaces, 1);
8160
}
8261

83-
$pmixedarr = str_split($pmixed);
84-
85-
foreach ($pmixedarr as $x) {
86-
if ($bars) {
87-
$t = true;
88-
} else {
89-
$t = false;
90-
}
91-
$w = ($x === '1') ? '1' : '2';
92-
93-
$barcode->addBar(new BarcodeBar($w, 1, $t));
94-
$bars = !$bars;
95-
++$k;
62+
foreach (str_split($pmixed) as $width) {
63+
$barcode->addBar(new BarcodeBar($width, 1, $drawBar));
64+
$drawBar = ! $drawBar;
9665
}
9766
}
9867

99-
$pend = "1101";
100-
$pendarr = str_split($pend);
68+
return $barcode;
69+
}
10170

102-
foreach ($pendarr as $x) {
103-
$t = $x == '1';
104-
$w = 1;
71+
private function getChecksum(string $code): string
72+
{
73+
$total = 0;
10574

106-
$barcode->addBar(new BarcodeBar($w, 1, $t));
107-
++$k;
75+
for ($charIndex = 0; $charIndex <= (strlen($code) - 1); $charIndex++) {
76+
$integerOfChar = intval($code . substr($charIndex, 1));
77+
$total += $integerOfChar * (($charIndex === 0 || $charIndex % 2 === 0) ? 3 : 1);
10878
}
10979

110-
return $barcode;
80+
$checksum = 10 - ($total % 10);
81+
if ($checksum === 10) {
82+
$checksum = 0;
83+
}
84+
85+
return (string)$checksum;
11186
}
11287
}

src/Types/TypeInterleaved25Checksum.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class TypeInterleaved25Checksum implements TypeInterface
1616
{
1717
public function getBarcodeData(string $code): Barcode
1818
{
19+
$chr = [];
1920
$chr['0'] = '11221';
2021
$chr['1'] = '21112';
2122
$chr['2'] = '12112';
@@ -36,6 +37,7 @@ public function getBarcodeData(string $code): Barcode
3637
// add leading zero if code-length is odd
3738
$code = '0' . $code;
3839
}
40+
3941
// add start and stop codes
4042
$code = 'AA' . strtolower($code) . 'ZA';
4143

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)