Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Minimal, zero-dependency implementation of the Luhn Algorithm for PHP.

License

NotificationsYou must be signed in to change notification settings

Ekman/luhn-algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build StatusCoverage Status

This is a zero dependency implementation of the Luhn Algorithm for PHP 7.4 and above. The Luhn Algorithm is used to validate things like credit cards and national identification numbers. More information on the algorithm can be found atWikipedia.

Installation

Install withComposer:

composer require nekman/luhn-algorithm

Usage

In order to instantiate a new instance of the library, use the factory:

useNekman\LuhnAlgorithm\LuhnAlgorithmFactory;$luhn = LuhnAlgorithmFactory::create();

You can findthe library facade in theLuhnAlgorithmInterface.php file.

TheNumber class is a container class that holds the actual number and the check digit. It does no validation nor does it calculate the check digit. It exists to clearly separate the number from the check digit and to define when the check digit exists or not. To simplify the process of validating a number you can use the named constructorNumber::fromString() like this:

useNekman\LuhnAlgorithm\Number;// Assume $creditCard is from a form.$number = Number::fromString($creditCard);if ($luhn->isValid($number)) {// Number is valid.}

Alternatively, if you want to calculate the checksum or check digit for a number:

useNekman\LuhnAlgorithm\Number;$number =newNumber(12345);$checksum =$luhn->calcChecksum($number);$checkDigit =$luhn->calcCheckDigit($number);

Versioning

This project complies withSemantic Versioning.

Changelog

For a complete list of changes, and how to migrate between major versions, seereleases page.


[8]ページ先頭

©2009-2025 Movatter.jp