Skip to content

Commit

Permalink
cbu generator (#4)
Browse files Browse the repository at this point in the history
* readme updated
* cbu generator done
  • Loading branch information
pablorsk authored Sep 13, 2018
1 parent a7b2917 commit 6f2615e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 30 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
40 changes: 40 additions & 0 deletions src/CbuFakerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright (C) 1997-2018 Reyesoft <[email protected]>.
*
* 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;
}
}
29 changes: 0 additions & 29 deletions src/ServiceProvider.php

This file was deleted.

26 changes: 26 additions & 0 deletions tests/CbuFakerProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Copyright (C) 1997-2018 Reyesoft <[email protected]>.
*
* 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.');
}
}
}

0 comments on commit 6f2615e

Please sign in to comment.