Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into feature/console
Browse files Browse the repository at this point in the history
Conflicts:
	library/Zend/Mvc/Service/ModuleManagerFactory.php
	library/Zend/Mvc/View/Http/ViewManager.php
	tests/Zend/Mvc/ApplicationTest.php
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 293 additions and 2 deletions.
202 changes: 202 additions & 0 deletions src/Iban.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_I18n
*/

namespace Zend\Validator;

use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;

/**
* Validates IBAN Numbers (International Bank Account Numbers)
*
* @category Zend
* @package Zend_Validate
*/
class Iban extends AbstractValidator
{
const NOTSUPPORTED = 'ibanNotSupported';
const FALSEFORMAT = 'ibanFalseFormat';
const CHECKFAILED = 'ibanCheckFailed';

/**
* Validation failure message template definitions
*
* @var array
*/
protected $messageTemplates = array(
self::NOTSUPPORTED => "Unknown country within the IBAN",
self::FALSEFORMAT => "The input has a false IBAN format",
self::CHECKFAILED => "The input has failed the IBAN check",
);

/**
* Optional country code by ISO 3166-1
*
* @var string|null
*/
protected $countryCode;

/**
* IBAN regexes by country code
*
* @var array
*/
protected static $ibanRegex = array(
'AD' => '/^AD[0-9]{2}[0-9]{8}[A-Z0-9]{12}$/',
'AT' => '/^AT[0-9]{2}[0-9]{5}[0-9]{11}$/',
'BA' => '/^BA[0-9]{2}[0-9]{6}[0-9]{10}$/',
'BE' => '/^BE[0-9]{2}[0-9]{3}[0-9]{9}$/',
'BG' => '/^BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2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9]{18}$/',
'NL' => '/^NL[0-9]{2}[A-Z]{4}[0-9]{10}$/',
'NO' => '/^NO[0-9]{2}[0-9]{4}[0-9]{7}$/',
'PL' => '/^PL[0-9]{2}[0-9]{8}[0-9]{16}$/',
'PT' => '/^PT[0-9]{2}[0-9]{8}[0-9]{13}$/',
'RO' => '/^RO[0-9]{2}[A-Z]{4}[A-Z0-9]{16}$/',
'SE' => '/^SE[0-9]{2}[0-9]{3}[0-9]{17}$/',
'SI' => '/^SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}$/',
'SK' => '/^SK[0-9]{2}[0-9]{4}[0-9]{16}$/',
'TN' => '/^TN[0-9]{2}[0-9]{5}[0-9]{15}$/',
'TR' => '/^TR[0-9]{2}[0-9]{5}[A-Z0-9]{17}$/'
);

/**
* Sets validator options
*
* @param array|Traversable $options OPTIONAL
*/
public function __construct($options = array())
{
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
}

if (array_key_exists('country_code', $options)) {
$this->setCountryCode($options['country_code']);
}

parent::__construct($options);
}

/**
* Returns the optional country code by ISO 3166-1
*
* @return string|null
*/
public function getCountryCode()
{
return $this->countryCode;
}

/**
* Sets an optional country code by ISO 3166-1
*
* @param string|null $countryCode
* @return Iban provides a fluent interface
* @throws Exception\InvalidArgumentException
*/
public function setCountryCode($countryCode = null)
{
if ($countryCode !== null) {
$countryCode = (string) $countryCode;

if (!isset(self::$ibanRegex[$countryCode])) {
throw new Exception\InvalidArgumentException(
"Country code '{$countryCode}' invalid by ISO 3166-1 or not supported"
);
}
}

$this->countryCode = $countryCode;
return $this;
}

/**
* Returns true if $value is a valid IBAN
*
* @param string $value
* @return boolean
*/
public function isValid($value)
{
if (!is_string($value)) {
$this->error(self::INVALID);
return false;
}

$value = strtoupper($value);
$this->setValue($value);

$countryCode = $this->getCountryCode();
if ($countryCode === null) {
$countryCode = substr($value, 0, 2);
}

if (!array_key_exists($countryCode, self::$ibanRegex)) {
$this->setValue($countryCode);
$this->error(self::NOTSUPPORTED);
return false;
}

if (!preg_match(self::$ibanRegex[$countryCode], $value)) {
$this->error(self::FALSEFORMAT);
return false;
}

$format = substr($value, 4) . substr($value, 0, 4);
$format = str_replace(
array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'),
array('10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22',
'23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35'),
$format
);

$temp = intval(substr($format, 0, 1));
$len = strlen($format);
for ($x = 1; $x < $len; ++$x) {
$temp *= 10;
$temp += intval(substr($format, $x, 1));
$temp %= 97;
}

if ($temp != 1) {
$this->error(self::CHECKFAILED);
return false;
}

return true;
}
}
4 changes: 2 additions & 2 deletions test/CsrfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace ZendTest\Validator;

use Zend\Session\Configuration\StandardConfiguration;
use Zend\Session\Config\StandardConfig;
use Zend\Session\Container;
use Zend\Validator\Csrf;

Expand All @@ -34,7 +34,7 @@ public function setUp()
{
// Setup session handling
$_SESSION = array();
$sessionConfig = new StandardConfiguration(array(
$sessionConfig = new StandardConfig(array(
'storage' => 'Zend\Session\Storage\ArrayStorage',
));
$sessionManager = new TestAsset\SessionManager($sessionConfig);
Expand Down
89 changes: 89 additions & 0 deletions test/IbanTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_I18n
*/

namespace ZendTest\I18n\Validator;

use Zend\Validator\Iban as IbanValidator;

/**
* @category Zend
* @package Zend_Validator
* @subpackage UnitTests
* @group Zend_Validator
*/
class IbanTest extends \PHPUnit_Framework_TestCase
{
public function ibanDataProvider()
{
return array(
array('AD1200012030200359100100', true),
array('AT611904300234573201', true),
array('AT61 1904 3002 3457 3201', false),
array('AD1200012030200354100100', false),
);
}

/**
* Ensures that the validator follows expected behavior
*
* @dataProvider ibanDataProvider
* @return void
*/
public function testBasic($iban, $expected)
{
$validator = new IbanValidator();
$this->assertEquals($expected, $validator->isValid($iban));
}

public function testSettingAndGettingCountryCode()
{
$validator = new IbanValidator();

$validator->setCountryCode('DE');
$this->assertEquals('DE', $validator->getCountryCode());

$this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException', 'ISO 3166-1');
$validator->setCountryCode('foo');
}

public function testInstanceWithCountryCode()
{
$validator = new IbanValidator(array('country_code' => 'AT'));
$this->assertEquals('AT', $validator->getCountryCode());

$this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException', 'ISO 3166-1');
$validator = new IbanValidator(array('country_code' => 'BAR'));
}

public function testIbanNotSupportedCountryCode()
{
$validator = new IbanValidator();
$this->assertFalse($validator->isValid('US611904300234573201'));
}

/**
* @group ZF-10556
*/
public function testIbanDetectionWithoutCountryCode()
{
$validator = new IbanValidator();
$this->assertTrue($validator->isValid('AT611904300234573201'));
}

public function testEqualsMessageTemplates()
{
$validator = new IbanValidator();
$this->assertAttributeEquals(
$validator->getOption('messageTemplates'),
'messageTemplates',
$validator
);
}
}

0 comments on commit e280213

Please sign in to comment.