1
1
<?php
2
2
3
3
/*
4
- Copyright 2013 Niklas Ekman, nikl.ekman@gmail.com
5
-
6
- Licensed under the Apache License, Version 2.0 (the "License");
7
- you may not use this file except in compliance with the License.
8
- You may obtain a copy of the License at
9
-
10
- http://www.apache.org/licenses/LICENSE-2.0
11
-
12
- Unless required by applicable law or agreed to in writing, software
13
- distributed under the License is distributed on an "AS IS" BASIS,
14
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- See the License for the specific language governing permissions and
16
- limitations under the License.
4
+ * The MIT License (MIT)
5
+ *
6
+ * Copyright (c) 2014 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.
17
24
*/
18
25
19
26
/**
20
27
* Apply the Luhn Algorithm to a number.
21
28
*
22
- * @link http://en.wikipedia.org/wiki/Luhn_algorithm Go to Wikipedia for more
23
- * info on the Luhn Alorithm
24
- *
29
+ * @link http://en.wikipedia.org/wiki/Luhn_algorithm
25
30
* @author Niklas Ekman <nikl.ekman@gmail.com>
26
- * @version 2014-02-04
31
+ * @version 2014-02-07
27
32
*/
28
33
class LuhnAlgorithm {
29
34
@@ -32,11 +37,10 @@ class LuhnAlgorithm {
32
37
private $ checkDigit ;
33
38
34
39
/**
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
40
+ * Set number that the instance should handle.
41
+ * @param sting|int $number Number in string or int format
42
+ * @param bool $withCheckDigit [Defaults to true] <br> Is the check digit
43
+ * included in $number?
40
44
*/
41
45
function __construct ($ number , $ withCheckDigit = true ) {
42
46
$ this ->setNumber ($ number , $ withCheckDigit );
@@ -55,18 +59,15 @@ public function isValid() {
55
59
56
60
/**
57
61
* Calculate the checksum from a number
58
- * @param string $number
62
+ * @param string $number Number to calculate checksum of
63
+ * @param int $length [Defaults to 0] <br> Length of $number. Function
64
+ * will calculate it if not supplied
59
65
* @return int Checksum
60
66
*/
61
- public static function calculateChecksum ($ number , $ length = null ) {
62
- // Validate the number
63
- if (preg_match (self ::numberRegex (), $ number ) !== 1 ) {
64
- throw new \InvalidArgumentException ("{$ number } is an invalid format " );
65
- }
66
-
67
+ public static function calculateChecksum ($ number , $ length = 0 ) {
67
68
$ number = strval (self ::stringToInteger ($ number ));
68
69
69
- if ($ length === null ) {
70
+ if ($ length === 0 ) {
70
71
$ length = strlen ($ number );
71
72
}
72
73
@@ -145,15 +146,6 @@ public function getCheckDigit() {
145
146
return $ this ->checkDigit ;
146
147
}
147
148
148
- /**
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
149
/**
158
150
* Fix a string so that it only contains numbers
159
151
* @param string $integer String to convert to integer
@@ -164,18 +156,12 @@ public static function stringToInteger($integer) {
164
156
}
165
157
166
158
/**
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
159
+ * Set number that the instance should handle.
160
+ * @param sting|int $number Number in string or int format
161
+ * @param bool $withCheckDigit [Defaults to true] <br> Is the check digit
162
+ * included in $number?
172
163
*/
173
164
public function setNumber ($ number , $ withCheckDigit = true ) {
174
- // Validate the number
175
- if (preg_match (self ::numberRegex (), $ number ) !== 1 ) {
176
- throw new \InvalidArgumentException ("{$ number } is an invalid format " );
177
- }
178
-
179
165
$ number = strval (self ::stringToInteger ($ number ));
180
166
$ length = strlen ($ number );
181
167
0 commit comments