Skip to content

Commit a139792

Browse files
committed
ITF14 throws InvalidCheckDigitException when checksum is invalid
1 parent 5e76e1c commit a139792

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/Types/TypeITF14.php

+6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
use Picqer\Barcode\Barcode;
66
use Picqer\Barcode\BarcodeBar;
77
use Picqer\Barcode\Exceptions\InvalidCharacterException;
8+
use Picqer\Barcode\Exceptions\InvalidCheckDigitException;
89
use Picqer\Barcode\Exceptions\InvalidLengthException;
910

1011
class TypeITF14 implements TypeInterface
1112
{
1213
/**
1314
* @throws InvalidLengthException
1415
* @throws InvalidCharacterException
16+
* @throws InvalidCheckDigitException
1517
*/
1618
public function getBarcodeData(string $code): Barcode
1719
{
@@ -35,6 +37,10 @@ public function getBarcodeData(string $code): Barcode
3537

3638
if (strlen($code) === 13) {
3739
$code .= $this->getChecksum($code);
40+
} elseif (substr($code, -1) !== $this->getChecksum(substr($code, 0, -1))) {
41+
// If length of given barcode is same as final length, barcode is including checksum
42+
// Make sure that checksum is the same as we calculated
43+
throw new InvalidCheckDigitException();
3844
}
3945

4046
$barcode = new Barcode($code);

0 commit comments

Comments
 (0)