Skip to content

Commit 7567f6d

Browse files
committed
Fix codestyle of ITF-14
1 parent 28b3b8c commit 7567f6d

File tree

2 files changed

+33
-49
lines changed

2 files changed

+33
-49
lines changed

src/Types/TypeITF14.php

Lines changed: 32 additions & 49 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,54 +26,35 @@ 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

3332
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;
33+
$code .= $this->getChecksum($code);
4834
}
4935

5036
if (strlen($code) > 14 || strlen($code) < 13) {
5137
throw new InvalidLengthException();
5238
}
5339

54-
$k = 0;
55-
$pbegin = "1010";
56-
$pbeginarr = str_split($pbegin);
40+
$barcode = new Barcode($code);
5741

58-
foreach ($pbeginarr as $x) {
59-
$t = $x === '1';
60-
$w = 1;
42+
// Add start and stop codes
43+
$code = 'AA' . strtolower($code) . 'ZA';
6144

62-
$barcode->addBar(new BarcodeBar($w, 1, $t));
63-
++$k;
64-
}
65-
66-
for ($i = 0; $i < strlen($code); $i += 2) {
67-
if (!isset($chr[$code[$i]]) || !isset($chr[$code[$i + 1]])) {
45+
for ($charIndex = 0; $charIndex < strlen($code); $charIndex += 2) {
46+
if (! isset($chr[$code[$charIndex]]) || ! isset($chr[$code[$charIndex + 1]])) {
6847
throw new InvalidCharacterException();
6948
}
7049

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

7755
while (strlen($pbars) > 0) {
78-
$pmixed .= $pbars[0] . $pspaces[0];
79-
$pbars = substr($pbars, 1);
56+
$pmixed .= $pbars[0] . $pspaces[0];
57+
$pbars = substr($pbars, 1);
8058
$pspaces = substr($pspaces, 1);
8159
}
8260

@@ -88,25 +66,30 @@ public function getBarcodeData(string $code): Barcode
8866
} else {
8967
$t = false;
9068
}
91-
$w = ($x === '1') ? '1' : '2';
69+
$width = ($x === '1') ? '1' : '2';
9270

93-
$barcode->addBar(new BarcodeBar($w, 1, $t));
94-
$bars = !$bars;
95-
++$k;
71+
$barcode->addBar(new BarcodeBar($width, 1, $t));
72+
$bars = ! $bars;
9673
}
9774
}
9875

99-
$pend = "1101";
100-
$pendarr = str_split($pend);
76+
return $barcode;
77+
}
78+
79+
private function getChecksum(string $code): string
80+
{
81+
$total = 0;
10182

102-
foreach ($pendarr as $x) {
103-
$t = $x == '1';
104-
$w = 1;
83+
for ($charIndex = 0; $charIndex <= (strlen($code) - 1); $charIndex++) {
84+
$integerOfChar = intval($code . substr($charIndex, 1));
85+
$total += $integerOfChar * (($charIndex === 0 || $charIndex % 2 === 0) ? 3 : 1);
86+
}
10587

106-
$barcode->addBar(new BarcodeBar($w, 1, $t));
107-
++$k;
88+
$checksum = 10 - ($total % 10);
89+
if ($checksum === 10) {
90+
$checksum = 0;
10891
}
10992

110-
return $barcode;
93+
return (string)$checksum;
11194
}
11295
}

src/Types/TypeInterleaved25Checksum.php

Lines changed: 1 addition & 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';

0 commit comments

Comments
 (0)