Skip to content

Commit

Permalink
fix: Fixed MB validation
Browse files Browse the repository at this point in the history
  • Loading branch information
seebeen committed Jan 24, 2022
1 parent d911a49 commit 68f0061
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
41 changes: 22 additions & 19 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/serbian-validator-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Checks if number is valid using check digit 11 function (mod11)
*
* @param int $number Number to check
* @return null|number Check digit if valid, null otherwise
* @return number Check digit
*/
function mod11($number) {
$digits = array_reverse(str_split((string) $number));
Expand All @@ -23,7 +23,7 @@ function mod11($number) {
return $remainder;
break;
case 1:
return null;
return 0;
break;
default:
return 11 - $remainder;
Expand Down
7 changes: 6 additions & 1 deletion src/validate-mb.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
namespace Oblak;

function validateMB($mb) {
return strlen($mb) == 8 && substr($mb, -1) == mod11(substr($mb, 0, 7));
$control_number = mod11(substr($mb, 0, 7));
if ($control_number > 9) {
$control_number = 0;
}

return strlen($mb) == 8 && substr($mb, -1) == $control_number;
}
3 changes: 2 additions & 1 deletion tests/validate-mb.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
use function Oblak\validateMB;

final class MBValidationTest extends TestCase {
public function test_validatePIB() {
public function test_validateMB() {
$this->assertTrue(validateMB(66143627));
$this->assertFalse(validateMB(66143628));
$this->assertFalse(validateMB(6614362));
$this->assertFalse(validateMB(661436200));
$this->assertTrue(validateMB(61629190));
}
}

0 comments on commit 68f0061

Please sign in to comment.