File tree Expand file tree Collapse file tree 4 files changed +28
-25
lines changed Expand file tree Collapse file tree 4 files changed +28
-25
lines changed Original file line number Diff line number Diff line change 1
1
composer.phar
2
2
composer.lock
3
3
tests /phpunit.xml
4
- vendor
4
+ vendor
5
+ .idea
Original file line number Diff line number Diff line change 1
- # Luhn
1
+ # Xi Algorithm
2
+
3
+ ## Luhn
4
+
5
+ Usage:
2
6
3
7
```
4
- $luhn = new Luhn(1234);
5
- $luhn->generate();
8
+ use Xi\Algorithm\Luhn;
9
+
10
+ $luhn = new Luhn();
11
+ $luhn->generate(123); // 1230
6
12
```
7
13
8
- # Tests
14
+ ## Running the tests
15
+
16
+ No dependencies to other libraries exist, but in order to generate an autoloader
17
+ first run
9
18
10
- No dependencies to other libraries exist, but in order to generate an autoloader and run tests, first run
11
19
```
12
20
composer.phar install --dev
13
21
```
22
+
23
+ and then run the tests with
24
+
25
+ ```
26
+ phpunit -c tests
27
+ ```
Original file line number Diff line number Diff line change 4
4
5
5
class Luhn
6
6
{
7
- /**
8
- * @var integer
9
- */
10
- private $ number ;
11
-
12
- /**
13
- * @param integer $number
14
- */
15
- public function __construct ($ number )
16
- {
17
- $ this ->number = $ number ;
18
- }
19
-
20
7
/**
21
8
* Returns the given number with luhn algorithm applied.
22
9
*
23
10
* For example 456 becomes 4564.
24
11
*
12
+ * @param integer $number
25
13
* @return integer
26
14
*/
27
- public function generate ()
15
+ public function generate ($ number )
28
16
{
29
17
$ stack = 0 ;
30
- $ number = str_split (strrev ($ this -> number ), 1 );
18
+ $ digits = str_split (strrev ($ number ), 1 );
31
19
32
- foreach ($ number as $ key => $ value ) {
20
+ foreach ($ digits as $ key => $ value ) {
33
21
if ($ key % 2 === 0 ) {
34
22
$ value = array_sum (str_split ($ value * 2 , 1 ));
35
23
}
@@ -43,6 +31,6 @@ public function generate()
43
31
$ stack -= 10 ;
44
32
}
45
33
46
- return (int ) (implode ('' , array_reverse ($ number )) . abs ($ stack ));
34
+ return (int ) (implode ('' , array_reverse ($ digits )) . abs ($ stack ));
47
35
}
48
36
}
Original file line number Diff line number Diff line change @@ -18,8 +18,8 @@ class LuhnTest extends \PHPUnit_Framework_TestCase
18
18
*/
19
19
public function generatesLuhnChecksum ($ number , $ expected )
20
20
{
21
- $ luhn = new Luhn ($ number );
22
- $ luhnedNumber = $ luhn ->generate ();
21
+ $ luhn = new Luhn ();
22
+ $ luhnedNumber = $ luhn ->generate ($ number );
23
23
24
24
$ this ->assertInternalType ('integer ' , $ luhnedNumber );
25
25
$ this ->assertEquals ($ expected , $ luhnedNumber );
You can’t perform that action at this time.
0 commit comments