Skip to content

Commit

Permalink
Merge pull request #188 from bav-php/bav
Browse files Browse the repository at this point in the history
Add (German) bank account validation with malkusch/bav
  • Loading branch information
henriquemoody committed Jan 14, 2015
2 parents fc48c1a + 623a262 commit 15dc825
Show file tree
Hide file tree
Showing 12 changed files with 480 additions and 0 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ Reference
* [v::uploaded()](#vuploaded)
* [v::writable()](#vwritable)

### Banking

* [v::bank()](#vbankstring-countrycode)
* [v::bankAccount()](#vbankaccountstring-countrycode-string-bank)
* [v::bic()](#vbicstring-countrycode)

### Other

* [v::cnh()](#vcnh)
Expand Down Expand Up @@ -493,6 +499,42 @@ See also:

* [v::key()](#vkeyname) - Validates a specific key of an array

#### v::bank(string $countryCode)

Validates a bank.

```php
v::bank("de")->validate("70169464"); //true
v::bank("de")->validate("12345"); //false
```

These country codes are supported:

* "de" (Germany) - This validator needs `malkusch/bav` as dependency.

See also

* [v::bankAccount()](#vbankaccountstring-countrycode-string-bank)
* [v::bic()](#vbicstring-countrycode)

#### v::bankAccount(string $countryCode, string $bank)

Validates a bank account for a given bank.

```php
v::bankAccount("de", "70169464")->validate("1112"); //true
v::bankAccount("de", "70169464")->validate("1234"); //false
```

These country codes are supported:

* "de" (Germany) - This validator needs `malkusch/bav` as dependency.

See also

* [v::bank()](#vbankstring-countrycode)
* [v::bic()](#vbicstring-countrycode)

#### v::between($start, $end)
#### v::between($start, $end, boolean $inclusive=false)

Expand Down Expand Up @@ -535,6 +577,24 @@ See also:
* [v::min()](#vminmin)
* [v::max()](#vmaxmax)

#### v::bic(string $countryCode)

Validates a BIC (Bank Identifier Code) for a given country.

```php
v::bic("de")->validate("VZVDDED1XXX"); //true
v::bic("de")->validate("VZVDDED1"); //true
```

Theses country codes are supported:

* "de" (Germany) - This validator needs `malkusch/bav` as dependency.

See also

* [v::bank()](#vbankstring-countrycode)
* [v::bankAccount()](#vbankaccountstring-countrycode-string-bank)

#### v::bool()

Validates if the input is a boolean value:
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
"require-dev": {
"phpunit/phpunit": "~4.0",
"symfony/validator": "~2.6",
"malkusch/bav": "~1.0",
"zendframework/zend-validator": "~2.3"
},
"suggest": {
"ext-bcmath": "Arbitrary Precision Mathematics",
"malkusch/bav": "German bank account validation",
"ext-mbstring": "Multibyte String Functions"
},
"autoload": {
Expand Down
14 changes: 14 additions & 0 deletions library/Exceptions/BICException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Respect\Validation\Exceptions;

class BICException extends ValidationException
{
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be a BIC',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be a BIC',
)
);
}
14 changes: 14 additions & 0 deletions library/Exceptions/BankAccountException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Respect\Validation\Exceptions;

class BankAccountException extends ValidationException
{
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be a bank account',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be a bank account',
)
);
}
14 changes: 14 additions & 0 deletions library/Exceptions/BankException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Respect\Validation\Exceptions;

class BankException extends ValidationException
{
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be a bank.',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be a bank.',
)
);
}
58 changes: 58 additions & 0 deletions library/Rules/BIC.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace Respect\Validation\Rules;

use malkusch\bav\BAV;
use Respect\Validation\Exceptions\ComponentException;

/**
* Validates a BIC (Bank Identifier Code).
*
* Currently only German BIC validation is supported (country code "de").
* The German validator depends on the composer package malkusch/bav.
* Note: It is not recommended to use this validator with BAV's default
* configuration. Use a configuration with one of the following
* DataBackendContainer implementations:
* PDODataBackendContainer or DoctrineBackendContainer.
*
* @author Markus Malkusch <[email protected]>
* @see BAV::isValidBIC()
* @see \malkusch\bav\Configuration
* @see \malkusch\bav\ConfigurationRegistry::setConfiguration()
*/
class BIC extends Callback
{
/**
* @var String $countryCode The ISO 639-1 country code.
*/
private $countryCode;

/**
* Sets the country code.
*
* The country code is not case sensitive.
*
* @param string $countryCode The ISO 639-1 country code.
*/
public function __construct($countryCode)
{
$callback = null;

switch (strtolower($countryCode)) {
case "de":
$bav = new BAV();
$callback = function ($bic) use ($bav) {
return $bav->isValidBIC($bic);
};
break;

default:
$message = sprintf(
"Cannot validate BIC for country '%s'.",
$countryCode
);
throw new ComponentException($message);
}

parent::__construct($callback);
}
}
48 changes: 48 additions & 0 deletions library/Rules/Bank.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace Respect\Validation\Rules;

use malkusch\bav\BAV;
use Respect\Validation\Exceptions\ComponentException;

/**
* Validates a bank.
*
* Currently only German validation is supported (country code "de").
* This validator depends on the composer package malkusch/bav.
*
* @author Markus Malkusch <[email protected]>
* @see BAV::isValidBank()
*/
class Bank extends Callback
{

/**
* Sets the country code.
*
* The country code is not case sensitive.
*
* @param string $countryCode The ISO 639-1 country code.
*/
public function __construct($countryCode)
{
$callback = null;

switch (strtolower($countryCode)) {
case "de":
$bav = new BAV();
$callback = function ($bank) use ($bav) {
return $bav->isValidBank($bank);
};
break;

default:
$message = sprintf(
"Cannot validate bank for country '%s'.",
$countryCode
);
throw new ComponentException($message);
}

parent::__construct($callback);
}
}
49 changes: 49 additions & 0 deletions library/Rules/BankAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace Respect\Validation\Rules;

use malkusch\bav\BAV;
use Respect\Validation\Exceptions\ComponentException;

/**
* Validates a bank account for a given bank.
*
* Currently only German validation is supported (country code "de").
* This validator depends on the composer package malkusch/bav.
*
* @author Markus Malkusch <[email protected]>
* @see BAV::isValidBankAccount()
*/
class BankAccount extends Callback
{

/**
* Sets the country code and bank.
*
* The country code is not case sensitive.
*
* @param string $countryCode The ISO 639-1 country code.
* @param string $bank The bank.
*/
public function __construct($countryCode, $bank)
{
$callback = null;

switch (strtolower($countryCode)) {
case "de":
$bav = new BAV();
$callback = function ($account) use ($bank, $bav) {
return $bav->isValidBankAccount($bank, $account);
};
break;

default:
$message = sprintf(
"Cannot validate BIC for country '%s'.",
$countryCode
);
throw new ComponentException($message);
}

parent::__construct($callback);
}
}
3 changes: 3 additions & 0 deletions library/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
* @method static Validator alwaysValid()
* @method static Validator arr()
* @method static Validator attribute(string $reference, Validatable $validator = null, bool $mandatory = true)
* @method static Validator bank(string $countryCode)
* @method static Validator bankAccount(string $countryCode)
* @method static Validator base()
* @method static Validator between(int $min = null, int $max = null, bool $inclusive = false)
* @method static Validator bic(string $countryCode)
* @method static Validator bool()
* @method static Validator call()
* @method static Validator callback(mixed $callback)
Expand Down
88 changes: 88 additions & 0 deletions tests/library/Respect/Validation/Rules/BICTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
namespace Respect\Validation\Rules;

use malkusch\bav\ConfigurationRegistry;
use malkusch\bav\DefaultConfiguration;
use malkusch\bav\PDODataBackendContainer;

/**
* @large
*/
class BICTest extends \PHPUnit_Framework_TestCase
{

public static function setUpBeforeClass()
{
$configuration = new DefaultConfiguration();

$pdo = new \PDO('sqlite::memory:');
$configuration->setDataBackendContainer(new PDODataBackendContainer($pdo));

ConfigurationRegistry::setConfiguration($configuration);
}

public static function tearDownAfterClass()
{
ConfigurationRegistry::setConfiguration(new DefaultConfiguration());
}

/**
* @expectedException Respect\Validation\Exceptions\ComponentException
*/
public function testUnsupportedCountryCodeRaisesException()
{
$validator = new BIC("xx");
}

public function testCountryCodeIsCaseUnsensitive()
{
$validator1 = new BIC("de");
$validator1->validate("foo");

$validator2 = new BIC("DE");
$validator2->validate("foo");
}

/**
* @dataProvider providerForValidBIC
*/
public function testValidBICShouldReturnTrue(BIC $validator, $bic)
{
$this->assertTrue($validator->__invoke($bic));
$this->assertTrue($validator->assert($bic));
$this->assertTrue($validator->check($bic));
}

/**
* @dataProvider providerForNotBIC
* @expectedException Respect\Validation\Exceptions\BICException
*/
public function testInvalidBICShouldRaiseException(BIC $validator, $bic)
{
$this->assertFalse($validator->check($bic));
}

/**
* @dataProvider providerForNotBIC
*/
public function testInvalidBICShouldReturnFalse(BIC $validator, $bic)
{
$this->assertFalse($validator->__invoke($bic));
}

public function providerForValidBIC()
{
return array(
array(new BIC("de"), "VZVDDED1XXX"),
array(new BIC("de"), "VZVDDED1")
);
}

public function providerForNotBIC()
{
return array(
array(new BIC("de"), "VZVDDED1~~~")
);
}
}

Loading

0 comments on commit 15dc825

Please sign in to comment.