Skip to content

Commit 8e01532

Browse files
authored
Merge pull request #183 from dvrtech-us/main
Working ITF14 support with test and updated readme
2 parents 77da173 + b9ac477 commit 8e01532

File tree

4 files changed

+121
-1
lines changed

4 files changed

+121
-1
lines changed

Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner
8484
- TYPE_EAN_5
8585
- TYPE_EAN_8
8686
- TYPE_EAN_13
87+
- TYPE_ITF14 (Also known as GTIN-14)
8788
- TYPE_UPC_A
8889
- TYPE_UPC_E
8990
- TYPE_MSI

src/BarcodeGenerator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
use Picqer\Barcode\Types\TypeIntelligentMailBarcode;
4848
use Picqer\Barcode\Types\TypeInterleaved25;
4949
use Picqer\Barcode\Types\TypeInterleaved25Checksum;
50+
use Picqer\Barcode\Types\TypeITF14;
5051
use Picqer\Barcode\Types\TypeKix;
5152
use Picqer\Barcode\Types\TypeMsi;
5253
use Picqer\Barcode\Types\TypeMsiChecksum;
@@ -74,6 +75,7 @@ abstract class BarcodeGenerator
7475
const TYPE_STANDARD_2_5_CHECKSUM = 'S25+';
7576
const TYPE_INTERLEAVED_2_5 = 'I25';
7677
const TYPE_INTERLEAVED_2_5_CHECKSUM = 'I25+';
78+
const TYPE_ITF_14 = 'ITF14';
7779
const TYPE_CODE_128 = 'C128';
7880
const TYPE_CODE_128_A = 'C128A';
7981
const TYPE_CODE_128_B = 'C128B';
@@ -136,6 +138,9 @@ protected function createDataBuilderForType(string $type)
136138
case self::TYPE_INTERLEAVED_2_5_CHECKSUM:
137139
return new TypeInterleaved25Checksum();
138140

141+
case self::TYPE_ITF_14:
142+
return new TypeITF14();
143+
139144
case self::TYPE_CODE_128:
140145
return new TypeCode128();
141146

src/Types/TypeITF14.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace Picqer\Barcode\Types;
4+
5+
use Picqer\Barcode\Barcode;
6+
use Picqer\Barcode\BarcodeBar;
7+
use Picqer\Barcode\Exceptions\InvalidCharacterException;
8+
use Picqer\Barcode\Exceptions\InvalidLengthException;
9+
use Picqer\Barcode\Types\TypeInterface;
10+
11+
class TypeITF14 implements TypeInterface
12+
{
13+
/**
14+
* @throws InvalidLengthException
15+
* @throws InvalidCharacterException
16+
*/
17+
public function getBarcodeData(string $code): Barcode
18+
{
19+
$barcode = new Barcode($code);
20+
21+
$chr = array();
22+
$chr['0'] = '11221';
23+
$chr['1'] = '21112';
24+
$chr['2'] = '12112';
25+
$chr['3'] = '22111';
26+
$chr['4'] = '11212';
27+
$chr['5'] = '21211';
28+
$chr['6'] = '12211';
29+
$chr['7'] = '11122';
30+
$chr['8'] = '21121';
31+
$chr['9'] = '12121';
32+
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) {
51+
throw new InvalidLengthException();
52+
}
53+
54+
$k = 0;
55+
$pbegin = "1010";
56+
$pbeginarr = str_split($pbegin);
57+
58+
foreach ($pbeginarr as $x) {
59+
$t = $x === '1';
60+
$w = 1;
61+
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]])) {
68+
throw new InvalidCharacterException();
69+
}
70+
71+
$bars = true;
72+
$pbars = $chr[$code[$i]];
73+
$pspaces = $chr[$code[$i + 1]];
74+
$pmixed = "";
75+
76+
77+
while (strlen($pbars) > 0) {
78+
$pmixed .= $pbars[0] . $pspaces[0];
79+
$pbars = substr($pbars, 1);
80+
$pspaces = substr($pspaces, 1);
81+
}
82+
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;
96+
}
97+
}
98+
99+
$pend = "1101";
100+
$pendarr = str_split($pend);
101+
102+
foreach ($pendarr as $x) {
103+
$t = $x == '1';
104+
$w = 1;
105+
106+
$barcode->addBar(new BarcodeBar($w, 1, $t));
107+
++$k;
108+
}
109+
110+
return $barcode;
111+
}
112+
}

tests/VerifiedBarcodeTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +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']],
2728
['type' => BarcodeGenerator::TYPE_CODE_128, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']],
2829
['type' => BarcodeGenerator::TYPE_CODE_128_A, 'barcodes' => ['1234567890']],
2930
['type' => BarcodeGenerator::TYPE_CODE_128_B, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']],
@@ -62,7 +63,8 @@ public function testAllSupportedBarcodeTypes()
6263
}
6364
}
6465

65-
protected function getSaveFilename($value) {
66+
protected function getSaveFilename($value)
67+
{
6668
return preg_replace('/[^a-zA-Z0-9_ \-+]/s', '-', $value);
6769
}
6870
}

0 commit comments

Comments
 (0)