Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
    Generate iban code and make a initial test
  • Loading branch information
xafardero committed Nov 15, 2016
0 parents commit ed4d47f
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
composer.lock
*.swp
*.swo
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "xafardero/generate-iban",
"description": "Generate the IBAN letter with a CCC",
"type": "library",
"require": {
"php": ">=5.5.0"
},
"require-dev": {
"phpunit/phpunit": "^5.5"
},
"autoload": {
"psr-4": {
"IbanGenerator\\": "src/"
}
},
"authors": [
{
"name": "xaf",
"email": "[email protected]"
}
]
}
17 changes: 17 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="Demo Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
72 changes: 72 additions & 0 deletions src/Iban.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace IbanGenerator;

/**
* Class Iban
*
* Description
*
* @link
*/
class Iban
{
/**
* Iban validation.
*
* @param string $paisCode
* @param mixed $control
*
* @return bool
*/
public function generate($countryCode, $ccc)
{

$number = $ccc
. $this->code(substr($countryCode, 0, 1))
. $this->code(substr($countryCode, 1, 1))
. '00';

$checksum = 98 - bcmod($number, '97');

if (strlen($checksum) === 1) {
$checksum = '0' . $checksum;
}

return $countryCode . $checksum;
}

private function code($letter)
{
$tabla = [
'A' => '10',
'B' => '11',
'C' => '12',
'D' => '13',
'E' => '14',
'F' => '15',
'G' => '16',
'H' => '17',
'I' => '18',
'J' => '19',
'K' => '20',
'L' => '21',
'M' => '22',
'N' => '23',
'O' => '24',
'P' => '25',
'Q' => '26',
'R' => '27',
'S' => '28',
'T' => '29',
'U' => '30',
'V' => '31',
'W' => '32',
'X' => '33',
'Y' => '34',
'Z' => '35'
];

return $tabla[$letter];
}
}
27 changes: 27 additions & 0 deletions tests/CccTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use IbanGenerator\Iban;

/**
* Ccc validator test.
*
* @link
*
* Pattern:
* EEEE OOOO DC CCCCCCCCCC
*/
class IbanTest extends PHPUnit_Framework_TestCase
{
public function testCorrectCcc()
{
$entidad = '0000';
$oficina = '0000';
$cuenta = '0000000000';
$dc = '00';

$ibanGenerator = new Iban();

$code = $ibanGenerator->generate('ES', $entidad . $oficina . $dc . $cuenta);
$this->assertEquals($code, 'ES82');
}
}

0 comments on commit ed4d47f

Please sign in to comment.