diff --git a/README.md b/README.md index de9d424..d025a7b 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Generate CBU and CUIT/CUIL ## Installation -Add the CompanyNameGeneratorâ„¢ library to your `composer.json` file: +Add the ArgentinaDataGenerator library to your `composer.json` file: composer require pablorsk/argentina-data-generator --dev @@ -34,3 +34,7 @@ This snippet generates 5 awesome CUIT/CUIL valid numbers. Here is an example out cuit // 33-37145386-0 cuitNumber // 33371453860 dni // 37145386 + +### \ArgentinaDataGenerator\CbuFakerProvider + + cbu // 6999444785661157353820 diff --git a/composer.json b/composer.json index 0a48fae..28e734f 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,7 @@ "squizlabs/php_codesniffer": "3.3.0", "phpunit/phpunit": "7.2.6", "codedungeon/phpunit-result-printer": "0.19.10", + "pablorsk/cbu-validator-php": "^1.0", "sebastian/phpcpd": "4.0.0" }, "autoload": { diff --git a/src/CbuFakerProvider.php b/src/CbuFakerProvider.php new file mode 100644 index 0000000..8270cae --- /dev/null +++ b/src/CbuFakerProvider.php @@ -0,0 +1,40 @@ +. + * + * This file is part of JsonApiPlayground. JsonApiPlayground can not be copied and/or + * distributed without the express permission of Reyesoft + */ + +declare(strict_types=1); + +namespace ArgentinaDataGenerator; + +use Faker\Provider\Base; + +class CbuFakerProvider extends Base +{ + public static function cbu(): string + { + $A = random_int(1000000, 9999999); // 7 + $Ax = self::getChecksum($A); + $B = random_int(1000000000000, 9999999999999); // 13 + $Bx = self::getChecksum($B); + + return $A . $Ax . $B . $Bx; + } + + private static function getChecksum(int $number): int + { + $value = (string) $number; + $ponderador = [3, 1, 7, 9]; + $sum = 0; + $j = 0; + for ($i = strlen($value) - 1; $i >= 0; --$i) { + $sum += ($value[$i] * $ponderador[$j % 4]); + ++$j; + } + + return (10 - $sum % 10) % 10; + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php deleted file mode 100644 index f278437..0000000 --- a/src/ServiceProvider.php +++ /dev/null @@ -1,29 +0,0 @@ -. - * - * This file is part of JsonApiPlayground. JsonApiPlayground can not be copied and/or - * distributed without the express permission of Reyesoft - */ - -declare(strict_types=1); - -namespace Reyesoft\JsonApi; - -use Illuminate\Support\ServiceProvider as BaseServiceProvider; - -/** - * @codeCoverageIgnore - */ -class ServiceProvider extends BaseServiceProvider -{ - public function register(): void - { - $this->app->singleton('Faker', function($app) { - $faker = \Faker\Factory::create(); - $faker->addProvider(\ArgentinaDataGenerator\CuitFakerProvider::class); - return $faker; - }); - } -} diff --git a/tests/CbuFakerProviderTest.php b/tests/CbuFakerProviderTest.php new file mode 100644 index 0000000..0f41bbf --- /dev/null +++ b/tests/CbuFakerProviderTest.php @@ -0,0 +1,26 @@ +. + * + * This file is part of JsonApiPlayground. JsonApiPlayground can not be copied and/or + * distributed without the express permission of Reyesoft + */ + +declare(strict_types=1); + +namespace Tests; + +use ArgentinaDataGenerator\CbuFakerProvider; +use PHPUnit\Framework\TestCase; + +class CbuFakerProviderTest extends TestCase +{ + public function testCbu(): void + { + $generator = new CbuFakerProvider(\Faker\Factory::create()); + for ($i = 0; $i < 100; ++$i) { + $cbu = $generator::cbu(); + $this->assertTrue(\Cbu::isValid($cbu), $cbu . ' not valid.'); + } + } +}