@@ -16,25 +16,39 @@ composer require nekman/luhn-algorithm
1616
1717## Usage
1818
19- Use the class like this:
20-
19+ In order to instantiate a new instance of the library, use the factory:
20+
21+ ``` php
22+ use Nekman\LuhnAlgorithm\LuhnAlgorithmFactory;
23+
24+ $luhn = LuhnAlgorithmFactory::create();
25+ ```
26+
27+ You can find [ the public interface of the library in the ` LuhnAlgorithmInterface ` ] ( src/Contract/LuhnAlgorithmInterface.php ) .
28+
29+ [ The ` Number ` class] ( src/Number.php ) is a container class that holds the actual number and the check digit. It does no validation
30+ nor does it calculate the check digit. To simplify the process of validating a number you can use the
31+ named constructor ` Number::fromString() ` like this:
32+
2133``` php
22- use Nekman\LuhnAlgorithm\LuhnAlgorithmFactory;
2334use Nekman\LuhnAlgorithm\Number;
2435
25- $luhn = LuhnAlgorithmFactory::create();
36+ // Assume $creditCard is from a form.
37+ $number = Number::fromString($creditCard);
2638
27- // Validate a credit card number entered in a form.
28- $ccNumber = Number::fromString($creditCard);
29- if ($luhn->isValid($ccNumber)) {
30- // Credit card number is valid.
39+ if ($luhn->isValid($number)) {
40+ // Number is valid.
3141}
42+ ```
43+
44+ Alternatively, if you want to calculate the checksum or check digit for a number:
45+
46+ ``` php
47+ use Nekman\LuhnAlgorithm\Number;
3248
33- // These methods are used internally by the library. You're free
34- // to make use of them as well.
3549$number = new Number(12345);
3650
3751$checksum = $luhn->calcChecksum($number);
3852
3953$checkDigit = $luhn->calcCheckDigit($number);
40- ```
54+ ```
0 commit comments