Skip to content

Commit 93a47dc

Browse files
vanodeviumEkman
andauthored
Added ArgumentIsNotNumericException as wrapper for InvalidArgumentExсeption (#22)
* Added ArgumentIsNotNumericException as wrapper for InvalidArgumentException * Update Number.php * Update LuhnAlgorithm.php * Update Number.php Co-authored-by: Niklas Ekman <nikl.ekman@gmail.com>
1 parent 8ea6c3e commit 93a47dc

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"type": "library",
44
"description": "Implementation of the Luhn algorithm in PHP. Used in validation of credit card numbers and some national identification numbers.",
55
"prefer-stable": true,
6-
"prefer-lowest": true,
76
"minimum-stability": "stable",
87
"keywords": [
98
"luhn",

src/ArgumentIsNotNumericException.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Niklas Ekman
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
9+
* this software and associated documentation files (the "Software"), to deal in
10+
* the Software without restriction, including without limitation the rights to
11+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12+
* the Software, and to permit persons to whom the Software is furnished to do so,
13+
* subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
declare(strict_types=1);
27+
28+
namespace Nekman\LuhnAlgorithm;
29+
30+
class ArgumentIsNotNumericException extends \InvalidArgumentException
31+
{
32+
/**
33+
* ArgumentIsNotNumericException constructor.
34+
* @param string $number
35+
*/
36+
public function __construct(string $number)
37+
{
38+
parent::__construct("Expects \$number to be a number, \"{$number}\" given.", 0, null);
39+
}
40+
}

src/LuhnAlgorithm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function isValid(NumberInterface $number): bool
5555
public function calcCheckDigit(NumberInterface $number): int
5656
{
5757
$checksum = $this->calcChecksum($number);
58-
58+
5959
// Get the last digit of the checksum.
6060
$checkDigit = $checksum % 10;
6161

src/Number.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Number implements NumberInterface
5252
public function __construct(string $number, int $checkDigit = null)
5353
{
5454
if (!is_numeric($number)) {
55-
throw new \InvalidArgumentException("Expects \$number to be a number, \"{$number}\" given.");
55+
throw new ArgumentIsNotNumericException($number);
5656
}
5757

5858
$this->number = $number;
@@ -72,14 +72,16 @@ public static function fromString(string $input): self
7272
$input = preg_replace('/[^\d]/', '', $input);
7373

7474
if (!is_numeric($input)) {
75-
throw new \InvalidArgumentException("Expects \$input to be a number, \"{$input}\" given.");
75+
throw new ArgumentIsNotNumericException($input);
7676
}
7777

78+
$lastIndex = strlen($input) - 1;
79+
7880
// Get the last digit.
79-
$checkDigit = (int) $input[strlen($input) - 1];
81+
$checkDigit = (int) $input[$lastIndex];
8082

8183
// Remove the last digit.
82-
$number = substr($input, 0, strlen($input) - 1);
84+
$number = substr($input, 0, $lastIndex);
8385

8486
return new self($number, $checkDigit);
8587
}
@@ -100,11 +102,12 @@ public function getCheckDigit()
100102
return $this->checkDigit;
101103
}
102104

105+
103106
/**
104107
* {@inheritdoc}
105108
*/
106109
public function __toString()
107110
{
108-
return (string) $this->number . (string) $this->checkDigit;
111+
return $this->number . $this->checkDigit;
109112
}
110113
}

0 commit comments

Comments
 (0)