Skip to content

Commit 8259bec

Browse files
committed
License and code cleanup
1 parent 20a1bfc commit 8259bec

File tree

5 files changed

+190
-72
lines changed

5 files changed

+190
-72
lines changed

.gitignore

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,52 @@
1-
nbproject/
21
index.php
2+
3+
# Created by http://www.gitignore.io
4+
5+
### Windows ###
6+
# Windows image file caches
7+
Thumbs.db
8+
ehthumbs.db
9+
10+
# Folder config file
11+
Desktop.ini
12+
13+
# Recycle Bin used on file shares
14+
$RECYCLE.BIN/
15+
16+
# Windows Installer files
17+
*.cab
18+
*.msi
19+
*.msm
20+
*.msp
21+
22+
23+
### OSX ###
24+
.DS_Store
25+
.AppleDouble
26+
.LSOverride
27+
28+
# Icon must ends with two \r.
29+
Icon
30+
31+
32+
# Thumbnails
33+
._*
34+
35+
# Files that might appear on external disk
36+
.Spotlight-V100
37+
.Trashes
38+
39+
40+
### Linux ###
41+
*~
42+
43+
44+
### NetBeans ###
45+
nbproject/private/
46+
build/
47+
nbbuild/
48+
dist/
49+
nbdist/
50+
nbactions.xml
51+
nb-configuration.xml
52+
nbproject/

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Niklas Ekman
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

LuhnAlgorithm.php

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
<?php
22

33
/*
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.
1724
*/
1825

1926
/**
2027
* Apply the Luhn Algorithm to a number.
2128
*
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
2530
* @author Niklas Ekman <nikl.ekman@gmail.com>
26-
* @version 2014-02-04
31+
* @version 2014-02-07
2732
*/
2833
class LuhnAlgorithm {
2934

@@ -32,11 +37,10 @@ class LuhnAlgorithm {
3237
private $checkDigit;
3338

3439
/**
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?
4044
*/
4145
function __construct($number, $withCheckDigit = true) {
4246
$this->setNumber($number, $withCheckDigit);
@@ -55,18 +59,15 @@ public function isValid() {
5559

5660
/**
5761
* 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
5965
* @return int Checksum
6066
*/
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) {
6768
$number = strval(self::stringToInteger($number));
6869

69-
if ($length === null) {
70+
if ($length === 0) {
7071
$length = strlen($number);
7172
}
7273

@@ -145,15 +146,6 @@ public function getCheckDigit() {
145146
return $this->checkDigit;
146147
}
147148

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-
157149
/**
158150
* Fix a string so that it only contains numbers
159151
* @param string $integer String to convert to integer
@@ -164,18 +156,12 @@ public static function stringToInteger($integer) {
164156
}
165157

166158
/**
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?
172163
*/
173164
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-
179165
$number = strval(self::stringToInteger($number));
180166
$length = strlen($number);
181167

Personnummer.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
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.
24+
*/
25+
26+
require_once(dirname(__FILE__) . "/LuhnAlgorithm.php");
27+
28+
/**
29+
* Extends LuhnAlgorithm and makes sure that the number supplied is a valid
30+
* personnummer.
31+
*
32+
* @author Niklas Ekman <nikl.ekman@gmail.com>
33+
* @version 2014-02-07
34+
*/
35+
class Personnumer extends \LuhnAlgorithm {
36+
37+
/**
38+
* Validate an input and see if it can be a valid personnumer
39+
* @param type $input
40+
* @return type
41+
*/
42+
public static function isValid($input) {
43+
return preg_match("/^\d{2}[0-1]\d[0-3]\d\s?-?\s?\d{4}$/", trim($input)) !== false;
44+
}
45+
46+
/**
47+
* The number must be in the format <b>XXXXXX-XXX(D)</b> or
48+
* <b>XXXXXX - XXX(D)</b> or any permutations of those two. If the
49+
* checkdigit (D) is not supplied, it will be calculated
50+
*
51+
* @param string|int $number The personnumer or organizational number
52+
* @param bool $withCheckDigit [Defaults to true] <br> Is the check digit
53+
* included in $number?
54+
* @throws InvalidArgumentException
55+
*/
56+
public function setNumber($number, $withCheckDigit = true) {
57+
if (!static::isValid($number)) {
58+
throw new \InvalidArgumentException("{$number} is not a valid personnummer");
59+
}
60+
61+
parent::setNumber($number, $withCheckDigit);
62+
}
63+
64+
/**
65+
* Calculate the checksum from a number
66+
* @param string $number Number to calculate checksum of
67+
* @param int $length [Defaults to 0] <br> Length of $number. Function
68+
* will calculate it if not supplied
69+
* @return int Checksum
70+
* @throws InvalidArgumentException
71+
*/
72+
public static function calculateChecksum($number, $length = 0) {
73+
if (!static::isValid($number)) {
74+
throw new \InvalidArgumentException("{$number} is not a valid personnummer");
75+
}
76+
77+
return \LuhnAlgorithm::calculateChecksum($number, $length);
78+
}
79+
80+
}

README.md

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,7 @@ checksum of a number:
1616
$number = '123456789';
1717
$luhnCheckSum = LuhnAlgorithm::calculateChecksum($number);
1818

19-
## Regex
20-
There will be a regex check agains the number provided as argument
21-
to this class. Override the function below to provide your own regex.
22-
23-
protected static function numberRegex() {
24-
return "/\d{6}\s?-?\s?\d{3}\d?/";
25-
}
26-
27-
## License
28-
Copyright 2013 Niklas Ekman, nikl.ekman@gmail.com
29-
30-
Licensed under the Apache License, Version 2.0 (the "License");
31-
you may not use this file except in compliance with the License.
32-
You may obtain a copy of the License at
33-
34-
http://www.apache.org/licenses/LICENSE-2.0
35-
36-
Unless required by applicable law or agreed to in writing, software
37-
distributed under the License is distributed on an "AS IS" BASIS,
38-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39-
See the License for the specific language governing permissions and
40-
limitations under the License.
19+
### Personnummer
20+
If you'd like to validate the input to the class, extend it and do a regex check.
21+
In the file **Personnummer.php**, I have extended the class to make sure that the
22+
input is a valid Swedish national security id.

0 commit comments

Comments
 (0)