Skip to content

Commit

Permalink
ITF14 throws InvalidCheckDigitException when checksum is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
vitkutny committed Jul 17, 2024
1 parent 5e76e1c commit a139792
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Types/TypeITF14.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
use Picqer\Barcode\Barcode;
use Picqer\Barcode\BarcodeBar;
use Picqer\Barcode\Exceptions\InvalidCharacterException;
use Picqer\Barcode\Exceptions\InvalidCheckDigitException;
use Picqer\Barcode\Exceptions\InvalidLengthException;

class TypeITF14 implements TypeInterface
{
/**
* @throws InvalidLengthException
* @throws InvalidCharacterException
* @throws InvalidCheckDigitException
*/
public function getBarcodeData(string $code): Barcode
{
Expand All @@ -35,6 +37,10 @@ public function getBarcodeData(string $code): Barcode

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

$barcode = new Barcode($code);
Expand Down

0 comments on commit a139792

Please sign in to comment.