Skip to content

3.x #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: php
php:
- '7.0'
- '7.1'
- '7.2'
before_script: composer install
script: vendor/bin/phpunit
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
# Luhn Algorithm in PHP
# Luhn Algorithm

[![Build Status](https://travis-ci.org/Ekman/Luhn-Algorithm.svg?branch=master)](https://travis-ci.org/Ekman/Luhn-Algorithm)

This is an implementation of the Luhn Algorithm in PHP. The Luhn Algorithm is
used to validate things like credit cards and national identifcation numbers.
More information on the algorithm can be found at [Wikipedia](http://en.wikipedia.org/wiki/Luhn_algorithm)

## Installation
Can be installed using composer:

"nekman/luhn-algorithm": "2.*"
Can be installed using composer:
```bash
composer require nekman/luhn-algorithm
```

## Usage

Use the class like this:

$luhn = new \Nekman\LuhnAlgorithm\LuhnAlgorithm('123456789');
$luhn->isCompletelyValid();
```php
use Nekman\LuhnAlgorithm\LuhnAlgorithmFactory;

$luhn = LuhnAlgorithmFactory::create();

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

$number = '123456789';
$luhnCheckSum = \Nekman\LuhnAlgorithm\LuhnAlgorithm::calculateChecksum($number);
$checkSum = $luhn->calcCheckSum(123456789);

### Personnummer
If you'd like to validate the input to the class, extend it and do a regex check.
In the file **Personnummer.php**, I have extended the class to make sure that the
input is a valid Swedish national security id.
$checkDigit = $luhn->calcCheckDigit(123456789);
```
19 changes: 14 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
"description": "Implementation of the Luhn algorithm in PHP. Used in validation of credit card numbers and some national identification numbers.",
"keywords": [
"luhn",
"credit card",
"identification number",
"credit",
"card",
"identification",
"validation"
],
"require-dev": {
"phpunit/phpunit": "5.1.*"
},
"license": "MIT",
"authors": [
{
Expand All @@ -27,5 +25,16 @@
"psr-4": {
"Nekman\\LuhnAlgorithm\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Nekman\\LuhnAlgorithm\\Test\\": "tests"
}
},
"require": {
"php": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
}
}
82 changes: 0 additions & 82 deletions example/Personnummer.example.php

This file was deleted.

4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
bootstrap="vendor/autoload.php">

<testsuites>
<testsuite name="Luhn Algorithm tests">
<directory>tests/</directory>
</testsuite>
</testsuites>
</phpunit>
</phpunit>
66 changes: 66 additions & 0 deletions src/Contract/LuhnAlgorithmInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Niklas Ekman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace Nekman\LuhnAlgorithm\Contract;

/**
* Handles the Luhn Algorithm.
*
* @link http://en.wikipedia.org/wiki/Luhn_algorithm
*/
interface LuhnAlgorithmInterface {
/**
* Determine if a number is valid according to the Luhn Algorithm.
*
* @param string $input The number to validate.
*
* @return bool true if number is valid, false otherwise.
*
* @throws \InvalidArgumentException If the input is invalid.
*/
public function isValid(string $input): bool;

/**
* Calculate the check digit for an input.
*
* @param int $input The input to calculate the check digit for.
*
* @return int The check digit.
*
* @throws \InvalidArgumentException If the input is invalid.
*/
public function calcCheckDigit(int $input): int;

/**
* Calulates the checksum for number.
*
* @param int $input The number to calculate the checksum for.
*
* @return int The checksum.
*
* @throws \InvalidArgumentException If the input is invalid.
*/
public function calcChecksum(int $input): int;
}
Loading