Skip to content

Commit

Permalink
Merge branch 'release/1.0.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
vpratfr committed Sep 19, 2019
2 parents d0ccc3d + 5e7c20d commit a40bda9
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 4 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"require": {
"php": ">=7.1",
"illuminate/support": "^5.5"
"illuminate/support": "^5.5|^6.0"
},
"require-dev": {
"roave/security-advisories": "dev-master",
Expand All @@ -43,7 +43,7 @@
"MarvinLabs\\Luhn\\LuhnServiceProvider"
],
"aliases": {
"Menu": "MarvinLabs\\Luhn\\Facades\\Luhn"
"Luhn": "MarvinLabs\\Luhn\\Facades\\Luhn"
}
}
},
Expand Down
12 changes: 11 additions & 1 deletion src/Luhn/Algorithm/LuhnAlgorithm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MarvinLabs\Luhn\Algorithm;

use InvalidArgumentException;
use MarvinLabs\Luhn\Contracts\LuhnAlgorithm as LuhnAlgorithmContract;

/**
Expand All @@ -12,7 +13,11 @@ class LuhnAlgorithm implements LuhnAlgorithmContract

public function isValid(string $input): bool
{
[$number, $lastDigit] = $this->cleanAndSplitInput($input);
try {
[$number, $lastDigit] = $this->cleanAndSplitInput($input);
} catch (InvalidArgumentException $e) {
return false;
}

$checksum = $this->computeCheckSum($number);
$sum = $checksum + $lastDigit;
Expand Down Expand Up @@ -59,6 +64,11 @@ protected function cleanAndSplitInput(string $input): array
// Remove everything not a digit, then extract check digit
$input = \preg_replace('/\D/', '', $input);
$inputLength = \strlen($input);

if ($inputLength === 0) {
throw new InvalidArgumentException;
}

return [
\substr($input, 0, $inputLength - 1),
(int)$input[$inputLength - 1],
Expand Down
6 changes: 6 additions & 0 deletions src/Luhn/LuhnServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class LuhnServiceProvider extends ServiceProvider
public function boot()
{
$this->registerValidationRules();
$this->registerTranslationsPath();
}

public function register()
Expand All @@ -36,4 +37,9 @@ protected function registerBindings(): void
$this->app->singleton(LuhnAlgorithmContract::class, LuhnAlgorithm::class);
$this->app->singleton('luhn', LuhnAlgorithmContract::class);
}

protected function registerTranslationsPath(): void
{
$this->loadTranslationsFrom(__DIR__ . '/../../translations', 'luhn');
}
}
4 changes: 3 additions & 1 deletion src/Luhn/Rules/LuhnRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public function passes($attribute, $value)

public function message()
{
return trans('validation.luhn');
return trans('validation.luhn') != 'validation.luhn'
? trans('validation.luhn')
: trans('luhn::validation.luhn');
}
}
2 changes: 2 additions & 0 deletions tests/AlgorithmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ public function provideIsValidTestInput(): array
{
return [
'default' => ['837668185', true],
'zero' => ['0', true],
'with_spaces' => ['837 668 185', true],
'123455' => ['123455', true],
'4103219202' => ['4103219202', true],
'31997233700020' => ['31997233700020', true],
'large' => ['89148000003974165685', true],
'empty' => ['', false],
'invalid' => ['123456789', false],
'123' => ['123', false],
];
Expand Down
7 changes: 7 additions & 0 deletions tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ public function can_use_shorthand_notation()
$validator = Validator::make(['number' => TestCase::INVALID_LUHN_NUMBER], ['number' => 'luhn']);
$this->assertTrue($validator->fails());
}

/** @test */
public function does_not_throw_on_edge_cases()
{
$validator = Validator::make(['number' => 'notanumber'], ['number' => 'luhn']);
$this->assertTrue($validator->fails());
}
}
5 changes: 5 additions & 0 deletions translations/en/validation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'luhn' => 'The :attribute checksum is invalid.',
];

0 comments on commit a40bda9

Please sign in to comment.