diff --git a/composer.json b/composer.json index 7d384d1..b9be08b 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,6 @@ "type": "library", "description": "Implementation of the Luhn algorithm in PHP. Used in validation of credit card numbers and some national identification numbers.", "prefer-stable": true, - "prefer-lowest": true, "minimum-stability": "stable", "keywords": [ "luhn", diff --git a/src/ArgumentIsNotNumericException.php b/src/ArgumentIsNotNumericException.php new file mode 100644 index 0000000..e79d21a --- /dev/null +++ b/src/ArgumentIsNotNumericException.php @@ -0,0 +1,40 @@ +calcChecksum($number); - + // Get the last digit of the checksum. $checkDigit = $checksum % 10; diff --git a/src/Number.php b/src/Number.php index a343e33..4228862 100644 --- a/src/Number.php +++ b/src/Number.php @@ -52,7 +52,7 @@ class Number implements NumberInterface public function __construct(string $number, int $checkDigit = null) { if (!is_numeric($number)) { - throw new \InvalidArgumentException("Expects \$number to be a number, \"{$number}\" given."); + throw new ArgumentIsNotNumericException($number); } $this->number = $number; @@ -72,14 +72,16 @@ public static function fromString(string $input): self $input = preg_replace('/[^\d]/', '', $input); if (!is_numeric($input)) { - throw new \InvalidArgumentException("Expects \$input to be a number, \"{$input}\" given."); + throw new ArgumentIsNotNumericException($input); } + $lastIndex = strlen($input) - 1; + // Get the last digit. - $checkDigit = (int) $input[strlen($input) - 1]; + $checkDigit = (int) $input[$lastIndex]; // Remove the last digit. - $number = substr($input, 0, strlen($input) - 1); + $number = substr($input, 0, $lastIndex); return new self($number, $checkDigit); } @@ -100,11 +102,12 @@ public function getCheckDigit() return $this->checkDigit; } + /** * {@inheritdoc} */ public function __toString() { - return (string) $this->number . (string) $this->checkDigit; + return $this->number . $this->checkDigit; } }