Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add (German) bank account validation with malkusch/bav #188

Merged
merged 1 commit into from Jan 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You must put these rules into Validator docblock too


### 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not?

$callback = array(new BAV(), 'isValidBIC');


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is better on constructor.

}

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);
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

break;

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

parent::__construct($callback);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same changes I recommended to BIC rule.

}
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);
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is the place where the array callback doesn't work anymore. The parameter order wouldn't match for isValidBankAccount($bank, $account). Callback::validate() would call isValidBankAccount($account, $bank). Even if the array_unshift() would be a array_push() it's very bad practice to rely on that implementation detail.

So the best solution here was the anonymous function. As Respect is PHP>=5.3 there is no problem using them. I used them in the other places because of consistency.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💃

break;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same changes I recommended to BIC rule.


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