From 9cdd724019353f07bef91fc651a38fd34fe94152 Mon Sep 17 00:00:00 2001 From: Vano Devium Date: Wed, 22 Apr 2020 21:48:27 +0300 Subject: [PATCH 1/4] Added ArgumentIsNotNumericException as wrapper for InvalidArgumentException --- composer.json | 1 - src/ArgumentIsNotNumericException.php | 40 +++++++++++++++++++++++++++ src/LuhnAlgorithm.php | 4 +-- src/Number.php | 17 ++++++------ 4 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 src/ArgumentIsNotNumericException.php 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 @@ +getCheckDigit() === null) { - throw new \InvalidArgumentException("Check digit is null."); + throw new \InvalidArgumentException('Check digit is null.'); } $checksum = $this->calcChecksum($number) + $number->getCheckDigit(); @@ -55,7 +55,7 @@ public function isValid(NumberInterface $number): bool public function calcCheckDigit(NumberInterface $number): int { $checksum = $this->calcChecksum($number); - + // Get the last digit of the checksum. $checkDigit = $checksum % 10; diff --git a/src/Number.php b/src/Number.php index a343e33..b52f75e 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; @@ -69,17 +69,19 @@ public function __construct(string $number, int $checkDigit = null) */ public static function fromString(string $input): self { - $input = preg_replace('/[^\d]/', '', $input); + $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,8 @@ public function getCheckDigit() return $this->checkDigit; } - /** - * {@inheritdoc} - */ public function __toString() { - return (string) $this->number . (string) $this->checkDigit; + return $this->number . $this->checkDigit; } } From fa51eb84cc57dfb52c57992448af6abcc795cee5 Mon Sep 17 00:00:00 2001 From: Niklas Ekman Date: Thu, 23 Apr 2020 20:27:08 +0200 Subject: [PATCH 2/4] Update Number.php --- src/Number.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Number.php b/src/Number.php index b52f75e..efa2dce 100644 --- a/src/Number.php +++ b/src/Number.php @@ -102,6 +102,10 @@ public function getCheckDigit() return $this->checkDigit; } + + /** + * {@inheritdoc} + */ public function __toString() { return $this->number . $this->checkDigit; From a15c7ffd237b976868bbe22f6d50c5f432448e4b Mon Sep 17 00:00:00 2001 From: Niklas Ekman Date: Thu, 23 Apr 2020 20:29:30 +0200 Subject: [PATCH 3/4] Update LuhnAlgorithm.php --- src/LuhnAlgorithm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LuhnAlgorithm.php b/src/LuhnAlgorithm.php index 7e18a4e..444d57a 100644 --- a/src/LuhnAlgorithm.php +++ b/src/LuhnAlgorithm.php @@ -41,7 +41,7 @@ class LuhnAlgorithm implements LuhnAlgorithmInterface public function isValid(NumberInterface $number): bool { if ($number->getCheckDigit() === null) { - throw new \InvalidArgumentException('Check digit is null.'); + throw new \InvalidArgumentException("Check digit is null."); } $checksum = $this->calcChecksum($number) + $number->getCheckDigit(); From 4a0a13f5b4b3a38298cf46c09885b4b4d42c3da8 Mon Sep 17 00:00:00 2001 From: Niklas Ekman Date: Thu, 23 Apr 2020 20:30:41 +0200 Subject: [PATCH 4/4] Update Number.php --- src/Number.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Number.php b/src/Number.php index efa2dce..4228862 100644 --- a/src/Number.php +++ b/src/Number.php @@ -69,7 +69,7 @@ public function __construct(string $number, int $checkDigit = null) */ public static function fromString(string $input): self { - $input = preg_replace('/\D/', '', $input); + $input = preg_replace('/[^\d]/', '', $input); if (!is_numeric($input)) { throw new ArgumentIsNotNumericException($input);