@@ -16,25 +16,39 @@ composer require nekman/luhn-algorithm
16
16
17
17
## Usage
18
18
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
+
21
33
``` php
22
- use Nekman\LuhnAlgorithm\LuhnAlgorithmFactory;
23
34
use Nekman\LuhnAlgorithm\Number;
24
35
25
- $luhn = LuhnAlgorithmFactory::create();
36
+ // Assume $creditCard is from a form.
37
+ $number = Number::fromString($creditCard);
26
38
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.
31
41
}
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;
32
48
33
- // These methods are used internally by the library. You're free
34
- // to make use of them as well.
35
49
$number = new Number(12345);
36
50
37
51
$checksum = $luhn->calcChecksum($number);
38
52
39
53
$checkDigit = $luhn->calcCheckDigit($number);
40
- ```
54
+ ```
0 commit comments