-
Notifications
You must be signed in to change notification settings - Fork 772
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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', | ||
) | ||
); | ||
} |
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', | ||
) | ||
); | ||
} |
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.', | ||
) | ||
); | ||
} |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is better on constructor. |
||
} | ||
|
||
parent::__construct($callback); | ||
} | ||
} |
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); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same changes I recommended to BIC rule. |
||
} |
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); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here too. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same changes I recommended to BIC rule. |
||
|
||
parent::__construct($callback); | ||
} | ||
} |
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~~~") | ||
); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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