3
3
/*
4
4
* The MIT License (MIT)
5
5
*
6
- * Copyright (c) 2014 Niklas Ekman
6
+ * Copyright (c) 2018 Niklas Ekman
7
7
*
8
8
* Permission is hereby granted, free of charge, to any person obtaining a copy of
9
9
* this software and associated documentation files (the "Software"), to deal in
@@ -44,11 +44,9 @@ public function isValid(NumberInterface $number): bool
44
44
throw new \InvalidArgumentException ("Check digit is null. " );
45
45
}
46
46
47
- $ checksum = $ this ->calcChecksum ($ number );
48
- $ sum = $ checksum + $ number ->getCheckDigit ();
47
+ $ checksum = $ this ->calcChecksum ($ number ) + $ number ->getCheckDigit ();
49
48
50
- // If the checksum is divisible by 10 it is valid.
51
- return ($ sum % 10 ) === 0 ;
49
+ return ($ checksum % 10 ) === 0 ;
52
50
}
53
51
54
52
/**
@@ -61,7 +59,6 @@ public function calcCheckDigit(NumberInterface $number): int
61
59
// Get the last digit of the checksum.
62
60
$ checkDigit = $ checksum % 10 ;
63
61
64
- // If the check digit is not 0, then subtract the value from 10.
65
62
return $ checkDigit === 0
66
63
? $ checkDigit
67
64
: 10 - $ checkDigit ;
@@ -72,14 +69,13 @@ public function calcCheckDigit(NumberInterface $number): int
72
69
*/
73
70
public function calcChecksum (NumberInterface $ number ): int
74
71
{
75
- $ number = (string ) $ number ->getNumber ();
76
- // Need to account for the check digit.
77
- $ nDigits = strlen ($ number ) + 1 ;
78
- $ parity = $ nDigits % 2 ;
72
+ $ nDigits = strlen ($ number ->getNumber ());
73
+ // Need to account for check digit
74
+ $ parity = ($ nDigits + 1 ) % 2 ;
79
75
$ checksum = 0 ;
80
76
81
- for ($ i = 0 ; $ i < $ nDigits - 1 ; $ i ++) {
82
- $ digit = (int ) $ number [$ i ];
77
+ for ($ i = 0 ; $ i < $ nDigits ; $ i ++) {
78
+ $ digit = (int ) $ number-> getNumber () [$ i ];
83
79
84
80
// Every other digit, starting from the rightmost,
85
81
// shall be doubled.
0 commit comments