Skip to content

Commit 4e087c8

Browse files
committed
Adds exceptions to fix picqer#7
1 parent c32c8ef commit 4e087c8

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

src/BarcodeGenerator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
namespace Picqer\Barcode;
3131

32+
use Picqer\Barcode\Exceptions\InvalidCharacterException;
33+
3234
abstract class BarcodeGenerator
3335
{
3436
const TYPE_CODE_39 = 'C39';
@@ -1638,6 +1640,9 @@ protected function barcode_eanupc($code, $len = 13)
16381640
}
16391641
$seq .= '01010'; // center guard bar
16401642
for ($i = $half_len; $i < $len; ++$i) {
1643+
if ( ! isset($codes['C'][$code{$i}])) {
1644+
throw new InvalidCharacterException('Char ' . $code{$i} . ' not allowed');
1645+
}
16411646
$seq .= $codes['C'][$code{$i}];
16421647
}
16431648
$seq .= '101'; // right guard bar
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Picqer\Barcode\Exceptions;
4+
5+
class BarcodeException extends \Exception {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Picqer\Barcode\Exceptions;
4+
5+
class InvalidCharacterException extends BarcodeException {}

tests/BarcodeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,14 @@ public function jpg_barcode_generator_can_generate_code_128_barcode()
4343
$generator = new Picqer\Barcode\BarcodeGeneratorJPG();
4444
$generator->getBarcode('081231723897', $generator::TYPE_CODE_128);
4545
}
46+
47+
/**
48+
* @test
49+
* @expectedException \Picqer\Barcode\Exceptions\InvalidCharacterException
50+
*/
51+
public function ean13_generator_throws_exception_if_invalid_chars_are_used()
52+
{
53+
$generator = new Picqer\Barcode\BarcodeGeneratorSVG();
54+
$generator->getBarcode('A123', $generator::TYPE_EAN_13);
55+
}
4656
}

0 commit comments

Comments
 (0)