@@ -32,12 +32,11 @@ class LuhnAlgorithm {
32
32
private $ checkDigit ;
33
33
34
34
/**
35
- * The number can contain other signs then numbers, for instance
36
- * whitespace; <b>123 456 789</b>, these will be stripped
37
- * away.
38
- * @param string|int $number String or int
39
- * @param bool $withCheckDigit If the number contains a checkdigit already
40
- * @throws InvalidArgumentException If the string is less than 2 numbers
35
+ * The number must be in the format <b>XXXXXX-XXX(D)</b> or
36
+ * <b>XXXXXX - XXX(x)</b> or any permutations of those two. If the
37
+ * checkdigit (D) is not supplied, it will be calculated
38
+ * @param string|int $number The personnumer or organizational number
39
+ * @throws InvalidArgumentException
41
40
*/
42
41
function __construct ($ number , $ withCheckDigit = true ) {
43
42
$ this ->setNumber ($ number , $ withCheckDigit );
@@ -60,10 +59,13 @@ public function isValid() {
60
59
* @return int Checksum
61
60
*/
62
61
public static function calculateChecksum ($ number , $ length = null ) {
63
- if (!is_string ($ number )) {
64
- $ number = strval ($ number );
62
+ // Validate the number
63
+ if (preg_match (self ::numberRegex (), $ number ) !== 1 ) {
64
+ throw new \InvalidArgumentException ("{$ number } is an invalid format " );
65
65
}
66
66
67
+ $ number = strval (self ::stringToInteger ($ number ));
68
+
67
69
if ($ length === null ) {
68
70
$ length = strlen ($ number );
69
71
}
@@ -100,7 +102,7 @@ public static function calculateChecksum($number, $length = null) {
100
102
*/
101
103
public static function calculcateCheckDigit ($ number ) {
102
104
// Get the checksum
103
- $ checkSum = strval (self ::calculateChecksum ($ number ));
105
+ $ checkSum = strval (self ::calculateChecksum ($ number . 0 ));
104
106
// Get the last digit of the checksum
105
107
$ checkDigit = intval ($ checkSum [strlen ($ checkSum ) - 1 ]);
106
108
@@ -113,7 +115,7 @@ public static function calculcateCheckDigit($number) {
113
115
* @return bool true if checkdigit is correct
114
116
*/
115
117
public function isValidCheckDigit () {
116
- $ checkDigit = self ::calculcateCheckDigit ($ this ->number . 0 , $ this -> nDigits + 1 );
118
+ $ checkDigit = self ::calculcateCheckDigit ($ this ->number );
117
119
// Validate
118
120
return $ checkDigit === $ this ->checkDigit ;
119
121
}
@@ -144,30 +146,41 @@ public function getCheckDigit() {
144
146
}
145
147
146
148
/**
147
- * The number can contain other signs then numbers, for instance
148
- * whitespace; <b>123 456 789</b>, these will be stripped
149
- * away.
150
- * @param string|int $number String or int
151
- * @param bool $withCheckDigit If the number contains a checkdigit already
152
- * @throws InvalidArgumentException If the string is less than 2 numbers
149
+ * What regex to use when validating numbers? Override this to provide
150
+ * something else
151
+ * @return string
152
+ */
153
+ protected static function numberRegex () {
154
+ return "/\d{6}\s?-?\s?\d{3}\d?/ " ;
155
+ }
156
+
157
+ /**
158
+ * Fix a string so that it only contains numbers
159
+ * @param string $integer String to convert to integer
160
+ * @return int An integer
161
+ */
162
+ public static function stringToInteger ($ integer ) {
163
+ return intval (preg_replace ("/[^0-9]/ " , "" , $ integer ));
164
+ }
165
+
166
+ /**
167
+ * The number must be in the format <b>XXXXXX-XXX(D)</b> or
168
+ * <b>XXXXXX - XXX(x)</b> or any permutations of those two. If the
169
+ * checkdigit (D) is not supplied, it will be calculated
170
+ * @param string|int $number The personnumer or organizational number
171
+ * @throws InvalidArgumentException
153
172
*/
154
173
public function setNumber ($ number , $ withCheckDigit = true ) {
155
174
// Validate the number
156
- if (preg_match (" /\d{6}\s?-?\s?\d{4}/ " , $ number ) !== 0 ) {
157
- throw new InvalidArgumentException (' Number must be numeric ' );
175
+ if (preg_match (self :: numberRegex () , $ number ) !== 1 ) {
176
+ throw new \ InvalidArgumentException ("{ $ number } is an invalid format " );
158
177
}
159
178
160
- $ number = strval (intval ($ number ));
161
-
162
- // Pretty safe to say that we need more then 1 number to be able to do
163
- // the Luhn Algorithm
179
+ $ number = strval (self ::stringToInteger ($ number ));
164
180
$ length = strlen ($ number );
165
- if ($ length <= 1 ) {
166
- throw new InvalidArgumentException ('Length must be longer then 1 ' );
167
- }
168
181
169
182
// If number does not include checkdigit, calculate it!
170
- if ($ withCheckDigit === false ) {
183
+ if (! $ withCheckDigit ) {
171
184
$ this ->checkDigit = self ::calculcateCheckDigit ($ number );
172
185
} else {
173
186
// Extract check digit from the number
0 commit comments