Created
June 7, 2024 06:02
-
-
Save robfrancken/ea16dbafb2413adb6d158faff9fceb21 to your computer and use it in GitHub Desktop.
ABN Validation Rule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Rules; | |
use Illuminate\Contracts\Validation\Rule; | |
class IsValidABN implements Rule | |
{ | |
/** | |
* Determine if the validation rule passes. | |
* | |
* @param string $attribute | |
* @param mixed $value | |
* @return bool | |
*/ | |
public function passes($attribute, $value) | |
{ | |
$weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]; | |
// strip anything other than digits | |
$abn = preg_replace("/[^\d]/", '', $value); | |
// check length is 11 digits | |
if (strlen($abn) == 11) { | |
// apply ATO check method | |
$sum = 0; | |
foreach ($weights as $position => $weight) { | |
$digit = $abn[$position] - ($position ? 0 : 1); | |
$sum += $weight * $digit; | |
} | |
return ($sum % 89) == 0; | |
} | |
return false; | |
} | |
/** | |
* Get the validation error message. | |
* | |
* @return string | |
*/ | |
public function message() | |
{ | |
return 'The provided ABN number is not valid.'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment