Skip to content

Commit

Permalink
Merge pull request #183 from dvrtech-us/main
Browse files Browse the repository at this point in the history
Working ITF14 support with test and updated readme
  • Loading branch information
casperbakker authored Jul 24, 2023
2 parents 77da173 + b9ac477 commit 8e01532
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner
- TYPE_EAN_5
- TYPE_EAN_8
- TYPE_EAN_13
- TYPE_ITF14 (Also known as GTIN-14)
- TYPE_UPC_A
- TYPE_UPC_E
- TYPE_MSI
Expand Down
5 changes: 5 additions & 0 deletions src/BarcodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use Picqer\Barcode\Types\TypeIntelligentMailBarcode;
use Picqer\Barcode\Types\TypeInterleaved25;
use Picqer\Barcode\Types\TypeInterleaved25Checksum;
use Picqer\Barcode\Types\TypeITF14;
use Picqer\Barcode\Types\TypeKix;
use Picqer\Barcode\Types\TypeMsi;
use Picqer\Barcode\Types\TypeMsiChecksum;
Expand Down Expand Up @@ -74,6 +75,7 @@ abstract class BarcodeGenerator
const TYPE_STANDARD_2_5_CHECKSUM = 'S25+';
const TYPE_INTERLEAVED_2_5 = 'I25';
const TYPE_INTERLEAVED_2_5_CHECKSUM = 'I25+';
const TYPE_ITF_14 = 'ITF14';
const TYPE_CODE_128 = 'C128';
const TYPE_CODE_128_A = 'C128A';
const TYPE_CODE_128_B = 'C128B';
Expand Down Expand Up @@ -136,6 +138,9 @@ protected function createDataBuilderForType(string $type)
case self::TYPE_INTERLEAVED_2_5_CHECKSUM:
return new TypeInterleaved25Checksum();

case self::TYPE_ITF_14:
return new TypeITF14();

case self::TYPE_CODE_128:
return new TypeCode128();

Expand Down
112 changes: 112 additions & 0 deletions src/Types/TypeITF14.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Picqer\Barcode\Types;

use Picqer\Barcode\Barcode;
use Picqer\Barcode\BarcodeBar;
use Picqer\Barcode\Exceptions\InvalidCharacterException;
use Picqer\Barcode\Exceptions\InvalidLengthException;
use Picqer\Barcode\Types\TypeInterface;

class TypeITF14 implements TypeInterface
{
/**
* @throws InvalidLengthException
* @throws InvalidCharacterException
*/
public function getBarcodeData(string $code): Barcode
{
$barcode = new Barcode($code);

$chr = array();
$chr['0'] = '11221';
$chr['1'] = '21112';
$chr['2'] = '12112';
$chr['3'] = '22111';
$chr['4'] = '11212';
$chr['5'] = '21211';
$chr['6'] = '12211';
$chr['7'] = '11122';
$chr['8'] = '21121';
$chr['9'] = '12121';

if (strlen($code) === 13) {
$total = 0;

for ($i = 0; $i <= strlen($code) - 1; $i++) {
$temp = intval($code . substr($i, 1));
$total += $temp * (($i === 0 || $i % 2 === 0) ? 3 : 1);
}

$cs = $total % 10;
$cs = 10 - $cs;
if ($cs === 10) {
$cs = 0;
}

$code .= (string) $cs;
}

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

$k = 0;
$pbegin = "1010";
$pbeginarr = str_split($pbegin);

foreach ($pbeginarr as $x) {
$t = $x === '1';
$w = 1;

$barcode->addBar(new BarcodeBar($w, 1, $t));
++$k;
}

for ($i = 0; $i < strlen($code); $i += 2) {
if (!isset($chr[$code[$i]]) || !isset($chr[$code[$i + 1]])) {
throw new InvalidCharacterException();
}

$bars = true;
$pbars = $chr[$code[$i]];
$pspaces = $chr[$code[$i + 1]];
$pmixed = "";


while (strlen($pbars) > 0) {
$pmixed .= $pbars[0] . $pspaces[0];
$pbars = substr($pbars, 1);
$pspaces = substr($pspaces, 1);
}

$pmixedarr = str_split($pmixed);

foreach ($pmixedarr as $x) {
if ($bars) {
$t = true;
} else {
$t = false;
}
$w = ($x === '1') ? '1' : '2';

$barcode->addBar(new BarcodeBar($w, 1, $t));
$bars = !$bars;
++$k;
}
}

$pend = "1101";
$pendarr = str_split($pend);

foreach ($pendarr as $x) {
$t = $x == '1';
$w = 1;

$barcode->addBar(new BarcodeBar($w, 1, $t));
++$k;
}

return $barcode;
}
}
4 changes: 3 additions & 1 deletion tests/VerifiedBarcodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class VerifiedBarcodeTest extends TestCase
['type' => BarcodeGenerator::TYPE_INTERLEAVED_2_5, 'barcodes' => ['1234567890']],
['type' => BarcodeGenerator::TYPE_INTERLEAVED_2_5_CHECKSUM, 'barcodes' => ['1234567890']],
['type' => BarcodeGenerator::TYPE_EAN_13, 'barcodes' => ['081231723897', '0049000004632', '004900000463']],
['type' => BarcodeGenerator::TYPE_ITF_14, 'barcodes' => ['00012345600012']],
['type' => BarcodeGenerator::TYPE_CODE_128, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']],
['type' => BarcodeGenerator::TYPE_CODE_128_A, 'barcodes' => ['1234567890']],
['type' => BarcodeGenerator::TYPE_CODE_128_B, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']],
Expand Down Expand Up @@ -62,7 +63,8 @@ public function testAllSupportedBarcodeTypes()
}
}

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

0 comments on commit 8e01532

Please sign in to comment.