Skip to content

Commit b581607

Browse files
committed
Throwing exceptions instead of giving false if failed to generate barcode picqer#2
1 parent 4e087c8 commit b581607

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

src/BarcodeGenerator.php

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929

3030
namespace Picqer\Barcode;
3131

32+
use Picqer\Barcode\Exceptions\BarcodeException;
3233
use Picqer\Barcode\Exceptions\InvalidCharacterException;
34+
use Picqer\Barcode\Exceptions\InvalidCheckDigitException;
35+
use Picqer\Barcode\Exceptions\InvalidFormatException;
36+
use Picqer\Barcode\Exceptions\InvalidLengthException;
3337

3438
abstract class BarcodeGenerator
3539
{
@@ -271,10 +275,6 @@ protected function barcode_code39($code, $extended = false, $checksum = false)
271275
$code = $this->encode_code39_ext($code);
272276
}
273277

274-
if ($code === false) {
275-
return false;
276-
}
277-
278278
if ($checksum) {
279279
// checksum
280280
$code .= $this->checksum_code39($code);
@@ -289,8 +289,7 @@ protected function barcode_code39($code, $extended = false, $checksum = false)
289289
for ($i = 0; $i < $clen; ++$i) {
290290
$char = $code{$i};
291291
if ( ! isset($chr[$char])) {
292-
// invalid character
293-
return false;
292+
throw new InvalidCharacterException('Char ' . $char . ' is unsupported');
294293
}
295294
for ($j = 0; $j < 9; ++$j) {
296295
if (($j % 2) == 0) {
@@ -455,7 +454,7 @@ protected function encode_code39_ext($code)
455454
$clen = strlen($code);
456455
for ($i = 0; $i < $clen; ++$i) {
457456
if (ord($code{$i}) > 127) {
458-
return false;
457+
throw new InvalidCharacterException('Only supports till char 127');
459458
}
460459
$code_ext .= $encode[$code{$i}];
461460
}
@@ -722,7 +721,7 @@ protected function barcode_code93($code)
722721
$clen = strlen($code);
723722
for ($i = 0; $i < $clen; ++$i) {
724723
if (ord($code{$i}) > 127) {
725-
return false;
724+
throw new InvalidCharacterException('Only supports till char 127');
726725
}
727726
$code_ext .= $encode[$code{$i}];
728727
}
@@ -736,8 +735,7 @@ protected function barcode_code93($code)
736735
for ($i = 0; $i < $clen; ++$i) {
737736
$char = ord($code{$i});
738737
if ( ! isset($chr[$char])) {
739-
// invalid character
740-
return false;
738+
throw new InvalidCharacterException('Char ' . $char . ' is unsupported');
741739
}
742740
for ($j = 0; $j < 6; ++$j) {
743741
if (($j % 2) == 0) {
@@ -929,8 +927,7 @@ protected function barcode_msi($code, $checksum = false)
929927
for ($i = 0; $i < $clen; ++$i) {
930928
$digit = $code{$i};
931929
if ( ! isset($chr[$digit])) {
932-
// invalid character
933-
return false;
930+
throw new InvalidCharacterException('Char ' . $digit . ' is unsupported');
934931
}
935932
$seq .= $chr[$digit];
936933
}
@@ -975,8 +972,7 @@ protected function barcode_s25($code, $checksum = false)
975972
for ($i = 0; $i < $clen; ++$i) {
976973
$digit = $code{$i};
977974
if ( ! isset($chr[$digit])) {
978-
// invalid character
979-
return false;
975+
throw new InvalidCharacterException('Char ' . $digit . ' is unsupported');
980976
}
981977
$seq .= $chr[$digit];
982978
}
@@ -1059,9 +1055,8 @@ protected function barcode_i25($code, $checksum = false)
10591055
for ($i = 0; $i < $clen; $i = ($i + 2)) {
10601056
$char_bar = $code{$i};
10611057
$char_space = $code{$i + 1};
1062-
if (( ! isset($chr[$char_bar])) OR ( ! isset($chr[$char_space]))) {
1063-
// invalid character
1064-
return false;
1058+
if ( ! isset($chr[$char_bar]) || ! isset($chr[$char_space])) {
1059+
throw new InvalidCharacterException();
10651060
}
10661061
// create a bar-space sequence
10671062
$seq = '';
@@ -1233,7 +1228,7 @@ protected function barcode_c128($code, $type = '')
12331228
} elseif (($char_id >= 0) AND ($char_id <= 95)) {
12341229
$code_data[] = strpos($keys_a, $char);
12351230
} else {
1236-
return false;
1231+
throw new InvalidCharacterException('Char ' . $char . ' is unsupported');
12371232
}
12381233
}
12391234
break;
@@ -1248,7 +1243,7 @@ protected function barcode_c128($code, $type = '')
12481243
} elseif (($char_id >= 32) AND ($char_id <= 127)) {
12491244
$code_data[] = strpos($keys_b, $char);
12501245
} else {
1251-
return false;
1246+
throw new InvalidCharacterException('Char ' . $char . ' is unsupported');
12521247
}
12531248
}
12541249
break;
@@ -1261,15 +1256,14 @@ protected function barcode_c128($code, $type = '')
12611256
--$len;
12621257
}
12631258
if (($len % 2) != 0) {
1264-
// the length must be even
1265-
return false;
1259+
throw new InvalidLengthException('Length must be even');
12661260
}
12671261
for ($i = 0; $i < $len; $i += 2) {
12681262
$chrnum = $code{$i} . $code{$i + 1};
12691263
if (preg_match('/([0-9]{2})/', $chrnum) > 0) {
12701264
$code_data[] = intval($chrnum);
12711265
} else {
1272-
return false;
1266+
throw new InvalidCharacterException();
12731267
}
12741268
}
12751269
break;
@@ -1509,8 +1503,7 @@ protected function barcode_eanupc($code, $len = 13)
15091503
// add check digit
15101504
$code .= $r;
15111505
} elseif ($r !== intval($code{$data_len})) {
1512-
// wrong checkdigit
1513-
return false;
1506+
throw new InvalidCheckDigitException();
15141507
}
15151508
if ($len == 12) {
15161509
// UPC-A
@@ -1688,7 +1681,7 @@ protected function barcode_eanext($code, $len = 5)
16881681
$r = (3 * ($code[0] + $code[2] + $code[4])) + (9 * ($code[1] + $code[3]));
16891682
$r %= 10;
16901683
} else {
1691-
return false;
1684+
throw new InvalidCheckDigitException();
16921685
}
16931686
//Convert digits to bars
16941687
$codes = array(
@@ -2021,7 +2014,7 @@ protected function barcode_codabar($code)
20212014
$len = strlen($code);
20222015
for ($i = 0; $i < $len; ++$i) {
20232016
if ( ! isset($chr[$code{$i}])) {
2024-
return false;
2017+
throw new InvalidCharacterException('Char ' . $code{$i} . ' is unsupported');
20252018
}
20262019
$seq = $chr[$code{$i}];
20272020
for ($j = 0; $j < 8; ++$j) {
@@ -2115,7 +2108,7 @@ protected function barcode_code11($code)
21152108
$len += 3;
21162109
for ($i = 0; $i < $len; ++$i) {
21172110
if ( ! isset($chr[$code{$i}])) {
2118-
return false;
2111+
throw new InvalidCharacterException('Char ' . $code{$i} . ' is unsupported');
21192112
}
21202113
$seq = $chr[$code{$i}];
21212114
for ($j = 0; $j < 6; ++$j) {
@@ -2549,7 +2542,7 @@ protected function barcode_imb($code)
25492542
break;
25502543
}
25512544
default: {
2552-
return false;
2545+
throw new BarcodeException('Routing code unknown');
25532546
break;
25542547
}
25552548
}
@@ -2647,7 +2640,7 @@ protected function barcode_imb($code)
26472640
protected function barcode_imb_pre($code)
26482641
{
26492642
if ( ! preg_match('/^[fadtFADT]{65}$/', $code) == 1) {
2650-
return false;
2643+
throw new InvalidFormatException();
26512644
}
26522645
$characters = str_split(strtolower($code), 1);
26532646
// build bars
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 InvalidCheckDigitException extends BarcodeException {}
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 InvalidFormatException extends BarcodeException {}
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 InvalidLengthException extends BarcodeException {}

0 commit comments

Comments
 (0)