Skip to content

Commit

Permalink
Merge pull request #186 from picqer/itf14-improvements
Browse files Browse the repository at this point in the history
ITF-14 improvements
  • Loading branch information
casperbakker authored Jul 24, 2023
2 parents cc09761 + affa227 commit 922940b
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 88 deletions.
34 changes: 12 additions & 22 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<testsuites>
<testsuite name="Barcode Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>
<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">
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Barcode Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
97 changes: 36 additions & 61 deletions src/Types/TypeITF14.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Picqer\Barcode\BarcodeBar;
use Picqer\Barcode\Exceptions\InvalidCharacterException;
use Picqer\Barcode\Exceptions\InvalidLengthException;
use Picqer\Barcode\Types\TypeInterface;

class TypeITF14 implements TypeInterface
{
Expand All @@ -16,9 +15,7 @@ class TypeITF14 implements TypeInterface
*/
public function getBarcodeData(string $code): Barcode
{
$barcode = new Barcode($code);

$chr = array();
$chr = [];
$chr['0'] = '11221';
$chr['1'] = '21112';
$chr['2'] = '12112';
Expand All @@ -29,84 +26,62 @@ public function getBarcodeData(string $code): Barcode
$chr['7'] = '11122';
$chr['8'] = '21121';
$chr['9'] = '12121';
$chr['A'] = '11';
$chr['Z'] = '21';

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) {
if (strlen($code) < 13 || strlen($code) > 14) {
throw new InvalidLengthException();
}

$k = 0;
$pbegin = "1010";
$pbeginarr = str_split($pbegin);
if (strlen($code) === 13) {
$code .= $this->getChecksum($code);
}

foreach ($pbeginarr as $x) {
$t = $x === '1';
$w = 1;
$barcode = new Barcode($code);

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

for ($i = 0; $i < strlen($code); $i += 2) {
if (!isset($chr[$code[$i]]) || !isset($chr[$code[$i + 1]])) {
// Loop through 2 chars at once
for ($charIndex = 0; $charIndex < strlen($code); $charIndex += 2) {
if (! isset($chr[$code[$charIndex]]) || ! isset($chr[$code[$charIndex + 1]])) {
throw new InvalidCharacterException();
}

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

$drawBar = true;
$pbars = $chr[$code[$charIndex]];
$pspaces = $chr[$code[$charIndex + 1]];
$pmixed = '';

while (strlen($pbars) > 0) {
$pmixed .= $pbars[0] . $pspaces[0];
$pbars = substr($pbars, 1);
$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;
foreach (str_split($pmixed) as $width) {
$barcode->addBar(new BarcodeBar($width, 1, $drawBar));
$drawBar = ! $drawBar;
}
}

$pend = "1101";
$pendarr = str_split($pend);
return $barcode;
}

foreach ($pendarr as $x) {
$t = $x == '1';
$w = 1;
private function getChecksum(string $code): string
{
$total = 0;

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

return $barcode;
$checksum = 10 - ($total % 10);
if ($checksum === 10) {
$checksum = 0;
}

return (string)$checksum;
}
}
2 changes: 2 additions & 0 deletions src/Types/TypeInterleaved25Checksum.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class TypeInterleaved25Checksum implements TypeInterface
{
public function getBarcodeData(string $code): Barcode
{
$chr = [];
$chr['0'] = '11221';
$chr['1'] = '21112';
$chr['2'] = '12112';
Expand All @@ -36,6 +37,7 @@ public function getBarcodeData(string $code): Barcode
// add leading zero if code-length is odd
$code = '0' . $code;
}

// add start and stop codes
$code = 'AA' . strtolower($code) . 'ZA';

Expand Down
4 changes: 2 additions & 2 deletions tests/VerifiedBarcodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/*
* Test all supported barcodes types, with as much different but supported input strings.
* Verified files can be build with generate-verified-files.php file.
* Verified files can be built with generate-verified-files.php file.
* Only run that file if you added new types or new strings to test.
*
* We use SVG because that output is vector and should be the same on every host system.
Expand All @@ -24,7 +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_ITF_14, 'barcodes' => ['00012345600012', '05400141288766']],
['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
5 changes: 2 additions & 3 deletions tests/verified-files/ITF14-00012345600012.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions tests/verified-files/ITF14-05400141288766.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 922940b

Please sign in to comment.