Skip to content

Commit 12b8c62

Browse files
authored
Rewrite the public interface of the library and add Travis CI. (#6)
* Rewrite for new version. * Add tracis. * Code review.
1 parent bf1f72d commit 12b8c62

10 files changed

+274
-323
lines changed

.travis.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: php
2+
php:
3+
- '7.0'
4+
- '7.1'
5+
- '7.2'
6+
before_script: composer install
7+
script: vendor/bin/phpunit

README.md

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
1-
# Luhn Algorithm in PHP
1+
# Luhn Algorithm
2+
3+
[![Build Status](https://travis-ci.org/Ekman/Luhn-Algorithm.svg?branch=master)](https://travis-ci.org/Ekman/Luhn-Algorithm)
4+
25
This is an implementation of the Luhn Algorithm in PHP. The Luhn Algorithm is
36
used to validate things like credit cards and national identifcation numbers.
47
More information on the algorithm can be found at [Wikipedia](http://en.wikipedia.org/wiki/Luhn_algorithm)
58

69
## Installation
7-
Can be installed using composer:
810

9-
"nekman/luhn-algorithm": "2.*"
11+
Can be installed using composer:
12+
```bash
13+
composer require nekman/luhn-algorithm
14+
```
1015

1116
## Usage
17+
1218
Use the class like this:
1319

14-
$luhn = new \Nekman\LuhnAlgorithm\LuhnAlgorithm('123456789');
15-
$luhn->isCompletelyValid();
20+
```php
21+
use Nekman\LuhnAlgorithm\LuhnAlgorithmFactory;
1622

23+
$luhn = LuhnAlgorithmFactory::create();
1724

18-
The class contains some static functions as well. This will return the Luhn
19-
checksum of a number:
25+
if ($luhn->isValid(123456789)) {
26+
// Number is valid.
27+
}
2028

21-
$number = '123456789';
22-
$luhnCheckSum = \Nekman\LuhnAlgorithm\LuhnAlgorithm::calculateChecksum($number);
29+
$checkSum = $luhn->calcCheckSum(123456789);
2330

24-
### Personnummer
25-
If you'd like to validate the input to the class, extend it and do a regex check.
26-
In the file **Personnummer.php**, I have extended the class to make sure that the
27-
input is a valid Swedish national security id.
31+
$checkDigit = $luhn->calcCheckDigit(123456789);
32+
```

composer.json

+14-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
"description": "Implementation of the Luhn algorithm in PHP. Used in validation of credit card numbers and some national identification numbers.",
55
"keywords": [
66
"luhn",
7-
"credit card",
8-
"identification number",
7+
"credit",
8+
"card",
9+
"identification",
910
"validation"
1011
],
11-
"require-dev": {
12-
"phpunit/phpunit": "5.1.*"
13-
},
1412
"license": "MIT",
1513
"authors": [
1614
{
@@ -27,5 +25,16 @@
2725
"psr-4": {
2826
"Nekman\\LuhnAlgorithm\\": "src"
2927
}
28+
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"Nekman\\LuhnAlgorithm\\Test\\": "tests"
32+
}
33+
},
34+
"require": {
35+
"php": "^7.0"
36+
},
37+
"require-dev": {
38+
"phpunit/phpunit": "^6.5"
3039
}
3140
}

example/Personnummer.example.php

-82
This file was deleted.

phpunit.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd"
2+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
33
bootstrap="vendor/autoload.php">
44

55
<testsuites>
66
<testsuite name="Luhn Algorithm tests">
77
<directory>tests/</directory>
88
</testsuite>
99
</testsuites>
10-
</phpunit>
10+
</phpunit>
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
namespace Nekman\LuhnAlgorithm\Contract;
27+
28+
/**
29+
* Handles the Luhn Algorithm.
30+
*
31+
* @link http://en.wikipedia.org/wiki/Luhn_algorithm
32+
*/
33+
interface LuhnAlgorithmInterface {
34+
/**
35+
* Determine if a number is valid according to the Luhn Algorithm.
36+
*
37+
* @param string $input The number to validate.
38+
*
39+
* @return bool true if number is valid, false otherwise.
40+
*
41+
* @throws \InvalidArgumentException If the input is invalid.
42+
*/
43+
public function isValid(string $input): bool;
44+
45+
/**
46+
* Calculate the check digit for an input.
47+
*
48+
* @param int $input The input to calculate the check digit for.
49+
*
50+
* @return int The check digit.
51+
*
52+
* @throws \InvalidArgumentException If the input is invalid.
53+
*/
54+
public function calcCheckDigit(int $input): int;
55+
56+
/**
57+
* Calulates the checksum for number.
58+
*
59+
* @param int $input The number to calculate the checksum for.
60+
*
61+
* @return int The checksum.
62+
*
63+
* @throws \InvalidArgumentException If the input is invalid.
64+
*/
65+
public function calcChecksum(int $input): int;
66+
}

0 commit comments

Comments
 (0)