33/*
44 * The MIT License (MIT)
55 *
6- * Copyright (c) 2014 Niklas Ekman
6+ * Copyright (c) 2018 Niklas Ekman
77 *
88 * Permission is hereby granted, free of charge, to any person obtaining a copy of
99 * this software and associated documentation files (the "Software"), to deal in
@@ -44,11 +44,9 @@ public function isValid(NumberInterface $number): bool
4444 throw new \InvalidArgumentException ("Check digit is null. " );
4545 }
4646
47- $ checksum = $ this ->calcChecksum ($ number );
48- $ sum = $ checksum + $ number ->getCheckDigit ();
47+ $ checksum = $ this ->calcChecksum ($ number ) + $ number ->getCheckDigit ();
4948
50- // If the checksum is divisible by 10 it is valid.
51- return ($ sum % 10 ) === 0 ;
49+ return ($ checksum % 10 ) === 0 ;
5250 }
5351
5452 /**
@@ -61,7 +59,6 @@ public function calcCheckDigit(NumberInterface $number): int
6159 // Get the last digit of the checksum.
6260 $ checkDigit = $ checksum % 10 ;
6361
64- // If the check digit is not 0, then subtract the value from 10.
6562 return $ checkDigit === 0
6663 ? $ checkDigit
6764 : 10 - $ checkDigit ;
@@ -72,14 +69,13 @@ public function calcCheckDigit(NumberInterface $number): int
7269 */
7370 public function calcChecksum (NumberInterface $ number ): int
7471 {
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 ;
7975 $ checksum = 0 ;
8076
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 ];
8379
8480 // Every other digit, starting from the rightmost,
8581 // shall be doubled.
0 commit comments