Skip to content

Commit 334bb27

Browse files
committed
Fixed structure of project. Changed autoloading in composer
1 parent 79ac121 commit 334bb27

File tree

8 files changed

+35
-24
lines changed

8 files changed

+35
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
index.php
2+
nbproject/
23

34
# Created by http://www.gitignore.io
45

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ More information on the algorithm can be found at [Wikipedia](http://en.wikipedi
66
## Usage
77
Use the class like this:
88

9-
$luhn = new LuhnAlgorithm('123456789');
9+
$luhn = new \Nekman\LuhnAlgorithm\LuhnAlgorithm('123456789');
1010
$luhn->isCompletelyValid();
1111

1212

1313
The class contains some static functions as well. This will return the Luhn
1414
checksum of a number:
1515

1616
$number = '123456789';
17-
$luhnCheckSum = LuhnAlgorithm::calculateChecksum($number);
17+
$luhnCheckSum = \Nekman\LuhnAlgorithm\LuhnAlgorithm::calculateChecksum($number);
1818

1919
### Personnummer
2020
If you'd like to validate the input to the class, extend it and do a regex check.
2121
In the file **Personnummer.php**, I have extended the class to make sure that the
22-
input is a valid Swedish national security id.
22+
input is a valid Swedish national security id.

composer.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"identification number",
99
"validation"
1010
],
11+
"require-dev": {
12+
"phpunit/phpunit": "4.1.*"
13+
},
1114
"license": "MIT",
1215
"authors": [
1316
{
@@ -20,13 +23,12 @@
2023
"issues": "https://github.com/Ekman/Luhn-Algorithm/issues",
2124
"source": "https://github.com/Ekman/Luhn-Algorithm"
2225
},
23-
"require": {
24-
"php": ">=4.0.0"
25-
},
2626
"require-dev": {
2727
"phpunit/phpunit": "3.7.*"
2828
},
2929
"autoload": {
30-
"files": ["LuhnAlgorithm.php"]
30+
"psr-4": {
31+
"Nekman\\LuhnAlgorithm\\": "src"
32+
}
3133
}
3234
}

Personnummer.example.php renamed to example/Personnummer.example.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2424
*/
2525

26-
require_once(dirname(__FILE__) . "/LuhnAlgorithm.php");
26+
require_once(dirname(__FILE__) . '../vendor/autoload.php');
27+
28+
use Nekman\LuhnAlgorithm\LuhnAlgorithm as LuhnAlgorithm;
2729

2830
/**
2931
* Extends LuhnAlgorithm and makes sure that the number supplied is a valid
@@ -32,15 +34,15 @@
3234
* @author Niklas Ekman <nikl.ekman@gmail.com>
3335
* @version 2014-02-07
3436
*/
35-
class Personnumer extends \LuhnAlgorithm {
37+
class Personnumer extends LuhnAlgorithm {
3638

3739
/**
3840
* Validate an input and see if it can be a valid personnumer
3941
* @param type $input
4042
* @return type
4143
*/
4244
public static function isValid($input) {
43-
return preg_match("/^\d{2}[0-1]\d[0-3]\d\s?-?\s?\d{4}$/", trim($input)) !== false;
45+
return preg_match("/^\d{2}[0-1]\d[0-3]\d\s?-?\s?\d{4}$/", trim($input)) === 1;
4446
}
4547

4648
/**

phpunit.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd"
3+
bootstrap="vendor/autoload.php">
4+
5+
<testsuites>
6+
<testsuite name="Luhn Algorithm tests">
7+
<directory>tests/</directory>
8+
</testsuite>
9+
</testsuites>
10+
</phpunit>

LuhnAlgorithm.php renamed to src/LuhnAlgorithm.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2424
*/
2525

26+
namespace Nekman\LuhnAlgorithm;
27+
2628
/**
2729
* Apply the Luhn Algorithm to a number
2830
*

tests/LuhnAlgorithmTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use \Nekman\LuhnAlgorithm\LuhnAlgorithm as LuhnAlgorithm;
4+
35
/**
46
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-02-04 at 21:57:34.
57
*/
@@ -48,8 +50,8 @@ public function testIsValid($number) {
4850
* @dataProvider provider
4951
*/
5052
public function testCalculateChecksum($number) {
51-
$number = \LuhnAlgorithm::toInteger($number);
52-
$checkSum = \LuhnAlgorithm::calculateChecksum($number);
53+
$number = LuhnAlgorithm::toInteger($number);
54+
$checkSum = LuhnAlgorithm::calculateChecksum($number);
5355
$this->assertEquals(0, $checkSum % 10);
5456
}
5557

@@ -58,14 +60,14 @@ public function testCalculateChecksum($number) {
5860
* @dataProvider provider
5961
*/
6062
public function testCalculcateCheckDigit($number) {
61-
$number = strval(\LuhnAlgorithm::toInteger($number));
63+
$number = strval(LuhnAlgorithm::toInteger($number));
6264
$last = strlen($number) - 1;
6365

6466
// Check digit is the last number
6567
$checkDigit = $number[$last];
6668
$number = substr($number, 0, $last);
6769

68-
$calcCheckDigit = \LuhnAlgorithm::calculcateCheckDigit($number);
70+
$calcCheckDigit = LuhnAlgorithm::calculcateCheckDigit($number);
6971
$this->assertEquals($checkDigit, $calcCheckDigit);
7072
}
7173

@@ -93,7 +95,7 @@ public function testIsCompletelyValid($number) {
9395
*/
9496
public function testGetNumber($number) {
9597
$this->object->setNumber($number, true);
96-
$number = \LuhnAlgorithm::toInteger($number);
98+
$number = LuhnAlgorithm::toInteger($number);
9799
$this->assertEquals($number, $this->object->getNumber());
98100
}
99101

@@ -113,7 +115,7 @@ public function testGetCheckDigit($number) {
113115
* @dataProvider provider
114116
*/
115117
public function testStringToInteger($number) {
116-
$int = \LuhnAlgorithm::toInteger($number);
118+
$int = LuhnAlgorithm::toInteger($number);
117119
$this->assertTrue(is_numeric($int));
118120
}
119121

tests/bootstrap.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)