Skip to content

Commit b229756

Browse files
committed
Added regular expression test in setNumber()
1 parent d84a24b commit b229756

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

LuhnAlgorithm.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
* @link http://en.wikipedia.org/wiki/Luhn_algorithm Go to Wikipedia for more
2323
* info on the Luhn Alorithm
2424
*
25-
* @author Niklas Ekman<nikl.ekman@gmail.com>
26-
* @version 2013-09-15
25+
* @author Niklas Ekman <nikl.ekman@gmail.com>
26+
* @version 2014-02-04
2727
*/
2828
class LuhnAlgorithm {
2929

@@ -152,15 +152,12 @@ public function getCheckDigit() {
152152
* @throws InvalidArgumentException If the string is less than 2 numbers
153153
*/
154154
public function setNumber($number, $withCheckDigit = true) {
155-
if (!is_numeric($number)) {
155+
// Validate the number
156+
if (preg_match("/\d{6}\s?-?\s?\d{4}/", $number) !== 0) {
156157
throw new InvalidArgumentException('Number must be numeric');
157158
}
158159

159-
// Store number as a string since we are going to use allot of
160-
// digits at position X
161-
if (!is_string($number)) {
162-
$number = strval($number);
163-
}
160+
$number = strval(intval($number));
164161

165162
// Pretty safe to say that we need more then 1 number to be able to do
166163
// the Luhn Algorithm
@@ -184,4 +181,4 @@ public function setNumber($number, $withCheckDigit = true) {
184181
$this->nDigits = $length;
185182
}
186183

187-
}
184+
}

0 commit comments

Comments
 (0)