Skip to content

Commit

Permalink
Merge pull request #1 from bernardofm/fix/numbers-started-with-zeroes…
Browse files Browse the repository at this point in the history
…-validation-error

Fixed the  parameter typefrom int to string: validation error when  s…
  • Loading branch information
lloricode authored Nov 24, 2022
2 parents 65f4ae8 + aef62a8 commit de1f1df
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
32 changes: 3 additions & 29 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,20 @@ class Generator
private int $value;
private int $checkDigit;

/**
* Generator constructor.
*
* @param int $numbers
* @param string $format
*
* @throws \Lloricode\CheckDigit\Exceptions\ValidationException
*/
public function __construct(int $numbers, string $format = self::GTIN_13)
public function __construct(string $numbers, string $format = self::GTIN_13)
{
$this->checkDigit = self::execute($numbers, $format);
$this->value = ($numbers * 10) + $this->checkDigit;
}

/**
* @param int $number
* @param string $format
*
* @throws \Lloricode\CheckDigit\Exceptions\ValidationException
*/
private static function validateLength(int $number, string $format): void
private static function validateLength(string $number, string $format): void
{
$actualLength = strlen((string)$number);
if (self::ID_KEY_FORMATS_LENGTH[$format] != ($actualLength + 1)) {
throw ValidationException::length($number, $actualLength, $format);
}
}

/**
* @param string $format
*
* @throws \Lloricode\CheckDigit\Exceptions\ValidationException
*/
private static function validateFormat(string $format): void
{
if (! in_array($format, self::ID_KEY_FORMATS)) {
Expand Down Expand Up @@ -95,14 +76,7 @@ private static function nearestEqualOrHigherThenMultiplyByTen(int $number): int
return (int)ceil($number / 10) * 10;
}

/**
* @param int $numbers
* @param string $format
*
* @return int
* @throws \Lloricode\CheckDigit\Exceptions\ValidationException
*/
private static function execute(int $numbers, string $format = self::GTIN_13): int
private static function execute(string $numbers, string $format = self::GTIN_13): int
{
self::validateFormat($format);
self::validateLength($numbers, $format);
Expand Down
10 changes: 10 additions & 0 deletions tests/Feature/GenerateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public function generator()
$this->assertEquals(7, (new Generator(87643802105978513, Generator::SSCC))->getCheckDigit());
}

public function testWithStringAndStartWithZero()
{
$this->assertEquals(7, (new Generator("0012345", Generator::GTIN_8))->getCheckDigit());
$this->assertEquals(5, (new Generator("00123456789", Generator::GTIN_12))->getCheckDigit());
$this->assertEquals(5, (new Generator("001234567890", Generator::GTIN_13))->getCheckDigit());
$this->assertEquals(2, (new Generator("0012345678901", Generator::GTIN_14))->getCheckDigit());
$this->assertEquals(3, (new Generator("0012345678901234", Generator::GSIN))->getCheckDigit());
$this->assertEquals(2, (new Generator("00123456789012345", Generator::SSCC))->getCheckDigit());
}

/**
* @throws \Lloricode\CheckDigit\Exceptions\ValidationException
* @test
Expand Down

0 comments on commit de1f1df

Please sign in to comment.