Skip to content

Commit 77157d2

Browse files
committed
Small cleanups
1 parent 7abe5e7 commit 77157d2

12 files changed

+477
-513
lines changed

generate-examples.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
require('vendor/autoload.php');
4-
require('tests/VerifiedBarcodeTest.php');
4+
require(__DIR__ . '/tests/VerifiedBarcodeTest.php');
55
$verifiedFiles = VerifiedBarcodeTest::$supportedBarcodes;
66

77
$result = [];

generate-verified-files.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
// New style of verified files
16-
require('tests/VerifiedBarcodeTest.php');
16+
require(__DIR__ . '/tests/VerifiedBarcodeTest.php');
1717
$verifiedFiles = VerifiedBarcodeTest::$supportedBarcodes;
1818

1919
$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG();

src/Types/TypeCodabar.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,41 @@
1313

1414
class TypeCodabar implements TypeInterface
1515
{
16+
protected $conversionTable = [
17+
'0' => '11111221',
18+
'1' => '11112211',
19+
'2' => '11121121',
20+
'3' => '22111111',
21+
'4' => '11211211',
22+
'5' => '21111211',
23+
'6' => '12111121',
24+
'7' => '12112111',
25+
'8' => '12211111',
26+
'9' => '21121111',
27+
'-' => '11122111',
28+
'$' => '11221111',
29+
':' => '21112121',
30+
'/' => '21211121',
31+
'.' => '21212111',
32+
'+' => '11222221',
33+
'A' => '11221211',
34+
'B' => '12121121',
35+
'C' => '11121221',
36+
'D' => '11122211'
37+
];
38+
1639
public function getBarcodeData(string $code): Barcode
1740
{
18-
$chr = array(
19-
'0' => '11111221',
20-
'1' => '11112211',
21-
'2' => '11121121',
22-
'3' => '22111111',
23-
'4' => '11211211',
24-
'5' => '21111211',
25-
'6' => '12111121',
26-
'7' => '12112111',
27-
'8' => '12211111',
28-
'9' => '21121111',
29-
'-' => '11122111',
30-
'$' => '11221111',
31-
':' => '21112121',
32-
'/' => '21211121',
33-
'.' => '21212111',
34-
'+' => '11222221',
35-
'A' => '11221211',
36-
'B' => '12121121',
37-
'C' => '11121221',
38-
'D' => '11122211'
39-
);
40-
4141
$barcode = new Barcode($code);
4242

4343
$code = 'A' . strtoupper($code) . 'A';
44-
$len = strlen($code);
45-
for ($i = 0; $i < $len; ++$i) {
46-
if (! isset($chr[$code[$i]])) {
44+
45+
for ($i = 0; $i < strlen($code); ++$i) {
46+
if (! isset($this->conversionTable[$code[$i]])) {
4747
throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported');
4848
}
4949

50-
$seq = $chr[$code[$i]];
50+
$seq = $this->conversionTable[$code[$i]];
5151
for ($j = 0; $j < 8; ++$j) {
5252
if (($j % 2) == 0) {
5353
$t = true; // bar

src/Types/TypeCode11.php

Lines changed: 66 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,56 @@
1313

1414
class TypeCode11 implements TypeInterface
1515
{
16+
protected $conversionTable = [
17+
'0' => '111121',
18+
'1' => '211121',
19+
'2' => '121121',
20+
'3' => '221111',
21+
'4' => '112121',
22+
'5' => '212111',
23+
'6' => '122111',
24+
'7' => '111221',
25+
'8' => '211211',
26+
'9' => '211111',
27+
'-' => '112111',
28+
'S' => '112211',
29+
];
30+
1631
public function getBarcodeData(string $code): Barcode
1732
{
18-
$chr = array(
19-
'0' => '111121',
20-
'1' => '211121',
21-
'2' => '121121',
22-
'3' => '221111',
23-
'4' => '112121',
24-
'5' => '212111',
25-
'6' => '122111',
26-
'7' => '111221',
27-
'8' => '211211',
28-
'9' => '211111',
29-
'-' => '112111',
30-
'S' => '112211'
31-
);
32-
3333
$barcode = new Barcode($code);
3434

35-
$len = strlen($code);
36-
// calculate check digit C
35+
$code .= $this->getCheckDigitC($code);
36+
$code .= $this->getCheckDigitK($code);
37+
38+
$code = 'S' . $code . 'S';
39+
40+
for ($i = 0; $i < strlen($code); ++$i) {
41+
if (! isset($this->conversionTable[$code[$i]])) {
42+
throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported');
43+
}
44+
45+
$seq = $this->conversionTable[$code[$i]];
46+
for ($j = 0; $j < strlen($seq); ++$j) {
47+
if (($j % 2) == 0) {
48+
$t = true; // bar
49+
} else {
50+
$t = false; // space
51+
}
52+
$w = $seq[$j];
53+
54+
$barcode->addBar(new BarcodeBar($w, 1, $t));
55+
}
56+
}
57+
58+
return $barcode;
59+
}
60+
61+
private function getCheckDigitC(string $code): string
62+
{
3763
$p = 1;
3864
$check = 0;
39-
for ($i = ($len - 1); $i >= 0; --$i) {
65+
for ($i = (strlen($code) - 1); $i >= 0; --$i) {
4066
$digit = $code[$i];
4167
if ($digit == '-') {
4268
$dval = 10;
@@ -53,51 +79,33 @@ public function getBarcodeData(string $code): Barcode
5379
if ($check == 10) {
5480
$check = '-';
5581
}
56-
$code .= $check;
57-
58-
if ($len > 10) {
59-
// calculate check digit K
60-
$p = 1;
61-
$check = 0;
62-
for ($i = $len; $i >= 0; --$i) {
63-
$digit = $code[$i];
64-
if ($digit == '-') {
65-
$dval = 10;
66-
} else {
67-
$dval = intval($digit);
68-
}
69-
$check += ($dval * $p);
70-
++$p;
71-
if ($p > 9) {
72-
$p = 1;
73-
}
74-
}
75-
$check %= 11;
76-
$code .= $check;
77-
++$len;
78-
}
7982

80-
$code = 'S' . $code . 'S';
81-
$len += 3;
82-
83-
for ($i = 0; $i < $len; ++$i) {
84-
if (! isset($chr[$code[$i]])) {
85-
throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported');
86-
}
83+
return $check;
84+
}
8785

88-
$seq = $chr[$code[$i]];
89-
for ($j = 0; $j < 6; ++$j) {
90-
if (($j % 2) == 0) {
91-
$t = true; // bar
92-
} else {
93-
$t = false; // space
94-
}
95-
$w = $seq[$j];
86+
private function getCheckDigitK(string $code): string
87+
{
88+
if (strlen($code) <= 10) {
89+
return '';
90+
}
9691

97-
$barcode->addBar(new BarcodeBar($w, 1, $t));
92+
$p = 1;
93+
$check = 0;
94+
for ($i = strlen($code); $i >= 0; --$i) {
95+
$digit = $code[$i];
96+
if ($digit == '-') {
97+
$dval = 10;
98+
} else {
99+
$dval = intval($digit);
100+
}
101+
$check += ($dval * $p);
102+
++$p;
103+
if ($p > 9) {
104+
$p = 1;
98105
}
99106
}
107+
$check %= 11;
100108

101-
return $barcode;
109+
return $check;
102110
}
103111
}

0 commit comments

Comments
 (0)