File tree Expand file tree Collapse file tree 1 file changed +6
-9
lines changed Expand file tree Collapse file tree 1 file changed +6
-9
lines changed Original file line number Diff line number Diff line change 22
22
* @link http://en.wikipedia.org/wiki/Luhn_algorithm Go to Wikipedia for more
23
23
* info on the Luhn Alorithm
24
24
*
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
27
27
*/
28
28
class LuhnAlgorithm {
29
29
@@ -152,15 +152,12 @@ public function getCheckDigit() {
152
152
* @throws InvalidArgumentException If the string is less than 2 numbers
153
153
*/
154
154
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 ) {
156
157
throw new InvalidArgumentException ('Number must be numeric ' );
157
158
}
158
159
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 ));
164
161
165
162
// Pretty safe to say that we need more then 1 number to be able to do
166
163
// the Luhn Algorithm
@@ -184,4 +181,4 @@ public function setNumber($number, $withCheckDigit = true) {
184
181
$ this ->nDigits = $ length ;
185
182
}
186
183
187
- }
184
+ }
You can’t perform that action at this time.
0 commit comments