-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from bav-php/bav
Add (German) bank account validation with malkusch/bav
- Loading branch information
Showing
12 changed files
with
480 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.', | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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~~~") | ||
); | ||
} | ||
} | ||
|
Oops, something went wrong.