Skip to content

Commit

Permalink
fixes and tests on C128
Browse files Browse the repository at this point in the history
  • Loading branch information
griffithben committed Sep 16, 2019
1 parent 745ad56 commit 326b886
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.DS_Store
vendor
composer.lock
composer.lock
tmp/
46 changes: 46 additions & 0 deletions src/Barcode/Types/C128.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Brewerwall\Barcode\Types;

use Brewerwall\Barcode\Exceptions\InvalidLengthException;
use Brewerwall\Barcode\Exceptions\InvalidCharacterException;

class C128 extends BarcodeTypeAbstract implements BarcodeTypeInterface
{
/** @var string */
Expand Down Expand Up @@ -357,4 +360,47 @@ protected function barcode_c128(string $code, string $type = ''): array

return $bararray;
}

/**
* Split text code in A/B sequence for 128 code.
*
* @param $code (string) code to split
*
* @return array sequence
* @protected
*/
protected function get128ABsequence($code)
{
$len = strlen($code);
$sequence = array();
// get A sequences (if any)
$numseq = array();
preg_match_all('/([\x00-\x1f])/', $code, $numseq, PREG_OFFSET_CAPTURE);
if (isset($numseq[1]) and !empty($numseq[1])) {
$end_offset = 0;
foreach ($numseq[1] as $val) {
$offset = $val[1];
if ($offset > $end_offset) {
// B sequence
$sequence[] = array(
'B',
substr($code, $end_offset, ($offset - $end_offset)),
($offset - $end_offset),
);
}
// A sequence
$slen = strlen($val[0]);
$sequence[] = array('A', substr($code, $offset, $slen), $slen);
$end_offset = $offset + $slen;
}
if ($end_offset < $len) {
$sequence[] = array('B', substr($code, $end_offset), ($len - $end_offset));
}
} else {
// only B sequence
$sequence[] = array('B', $code, $len);
}

return $sequence;
}
}
28 changes: 27 additions & 1 deletion tests/Unit/C128Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Brewerwall\Barcode\BarcodeGenerator;
use Brewerwall\Barcode\Constants\BarcodeRender;
use Brewerwall\Barcode\Constants\BarcodeType;
use Brewerwall\Barcode\Exceptions\InvalidCharacterException;
use Brewerwall\Barcode\Exceptions\InvalidLengthException;
use Test\BaseTestCase;

class C128Test extends BaseTestCase
Expand All @@ -19,6 +21,14 @@ public function test_C128GeneratesJPGStructure()
$this->assertContains('JPEG', $generated);
}

public function test_C128GeneratesJPGStructureWithNonNumericCode()
{
$generator = new BarcodeGenerator('0812|31723|897', BarcodeType::TYPE_CODE_128, BarcodeRender::RENDER_JPG);
$generated = $generator->generate();

$this->assertContains('JPEG', $generated);
}

public function test_C128GeneratesPNGStructure()
{
$generator = new BarcodeGenerator(self::VALID_CODE, BarcodeType::TYPE_CODE_128, BarcodeRender::RENDER_PNG);
Expand Down Expand Up @@ -75,6 +85,14 @@ public function test_C128AGeneratesSVGStructure()
$this->assertContains('<svg', $generated);
}

public function test_C128AThrowExceptionWithInvalidCharacters()
{
$this->expectException(InvalidCharacterException::class);

$generator = new BarcodeGenerator('0812|31723|897', BarcodeType::TYPE_CODE_128_A, BarcodeRender::RENDER_JPG);
$generator->generate();
}

public function test_C128BGeneratesJPGStructure()
{
$generator = new BarcodeGenerator(self::VALID_CODE, BarcodeType::TYPE_CODE_128_B, BarcodeRender::RENDER_JPG);
Expand Down Expand Up @@ -106,7 +124,7 @@ public function test_C128BGeneratesSVGStructure()

$this->assertContains('<svg', $generated);
}

public function test_C128CGeneratesJPGStructure()
{
$generator = new BarcodeGenerator(self::VALID_CODE, BarcodeType::TYPE_CODE_128_C, BarcodeRender::RENDER_JPG);
Expand Down Expand Up @@ -138,4 +156,12 @@ public function test_C128CGeneratesSVGStructure()

$this->assertContains('<svg', $generated);
}

public function test_C128CThrowExceptionWithUnevenCharacterLength()
{
$this->expectException(InvalidLengthException::class);

$generator = new BarcodeGenerator('81231723897', BarcodeType::TYPE_CODE_128_C, BarcodeRender::RENDER_JPG);
$generator->generate();
}
}

0 comments on commit 326b886

Please sign in to comment.