Iban validator.

@author  petitchevalroux
@licence originale http://creativecommons.org/licenses/by-sa/2.0/fr/

@link    http://dev.petitchevalroux.net/php/validation-iban-php.356.html + comments & links

@param string $iban

@return bool
349985
public static function validate($iban)
    {
        if (!extension_loaded('bcmath')) {
            throw new \RuntimeException(__METHOD__.' needs the bcmath extension.');
        }

        // Per country validation rules
        static $rules = array(
            'AL' => '[0-9]{8}[0-9A-Z]{16}',
            'AD' => '[0-9]{8}[0-9A-Z]{12}',
            'AT' => '[0-9]{16}',
            'BE' => '[0-9]{12}',
            'BA' => '[0-9]{16}',
            'BG' => '[A-Z]{4}[0-9]{6}[0-9A-Z]{8}',
            'HR' => '[0-9]{17}',
            'CY' => '[0-9]{8}[0-9A-Z]{16}',
            'CZ' => '[0-9]{20}',
            'DK' => '[0-9]{14}',
            'EE' => '[0-9]{16}',
            'FO' => '[0-9]{14}',
            'FI' => '[0-9]{14}',
            'FR' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}',
            'PF' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // French Polynesia
            'TF' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // French Southern Territories
            'GP' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // French Guadeloupe
            'MQ' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // French Martinique
            'YT' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // French Mayotte
            'NC' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // New Caledonia
            'RE' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // French Reunion
            'BL' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // French Saint Barthelemy
            'MF' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // French Saint Martin
            'PM' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // Saint Pierre et Miquelon
            'WF' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // Wallis and Futuna Islands
            'GE' => '[0-9A-Z]{2}[0-9]{16}',
            'DE' => '[0-9]{18}',
            'GI' => '[A-Z]{4}[0-9A-Z]{15}',
            'GR' => '[0-9]{7}[0-9A-Z]{16}',
            'GL' => '[0-9]{14}',
            'HU' => '[0-9]{24}',
            'IS' => '[0-9]{22}',
            'IE' => '[0-9A-Z]{4}[0-9]{14}',
            'IL' => '[0-9]{19}',
            'IT' => '[A-Z][0-9]{10}[0-9A-Z]{12}',
            'KZ' => '[0-9]{3}[0-9A-Z]{3}[0-9]{10}',
            'KW' => '[A-Z]{4}[0-9]{22}',
            'LV' => '[A-Z]{4}[0-9A-Z]{13}',
            'LB' => '[0-9]{4}[0-9A-Z]{20}',
            'LI' => '[0-9]{5}[0-9A-Z]{12}',
            'LT' => '[0-9]{16}',
            'LU' => '[0-9]{3}[0-9A-Z]{13}',
            'MK' => '[0-9]{3}[0-9A-Z]{10}[0-9]{2}',
            'MT' => '[A-Z]{4}[0-9]{5}[0-9A-Z]{18}',
            'MR' => '[0-9]{23}',
            'MU' => '[A-Z]{4}[0-9]{19}[A-Z]{3}',
            'MC' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}',
            'ME' => '[0-9]{18}',
            'NL' => '[A-Z]{4}[0-9]{10}',
            'NO' => '[0-9]{11}',
            'PL' => '[0-9]{24}',
            'PT' => '[0-9]{21}',
            'RO' => '[A-Z]{4}[0-9A-Z]{16}',
            'SM' => '[A-Z][0-9]{10}[0-9A-Z]{12}',
            'SA' => '[0-9]{2}[0-9A-Z]{18}',
            'RS' => '[0-9]{18}',
            'SK' => '[0-9]{20}',
            'SI' => '[0-9]{15}',
            'ES' => '[0-9]{20}',
            'SE' => '[0-9]{20}',
            'CH' => '[0-9]{5}[0-9A-Z]{12}',
            'TN' => '[0-9]{20}',
            'TR' => '[0-9]{5}[0-9A-Z]{17}',
            'AE' => '[0-9]{19}',
            'GB' => '[A-Z]{4}[0-9]{14}',
            'CI' => '[0-9A-Z]{2}[0-9]{22}',
            'BR' => '[0-9]{8}[0-9]{5}[0-9]{10}[A-Z]{1}[A-Z0-9]{1}',
        );
        // Min length check
        if (mb_strlen($iban) < 15) {
            return false;
        }
        // Fetch country code from IBAN
        $ctr = substr($iban, 0, 2);
        if (isset($rules[$ctr]) === false) {
            return false;
        }
        // Fetch country validation rule
        $check = substr($iban, 4);
        if (preg_match('~^'.$rules[$ctr].'$~', $check) !== 1) {
            return false;
        }
        // Fetch needed string for validation
        $check = $check.substr($iban, 0, 4);
        // Replace characters by decimal values
        $check = 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'),
            $check
        );

        // Final check
        return bcmod($check, 97) === '1';
    }
715111
public function validate($validator)
    {
        // Check Value and Timestamp:
        
        if (!empty($this->value) || $this->tooSoon()) {
            
            // Define Validation Error:
            
            $validator->validationError(
                $this->name,
                _t(
                    __CLASS__ . '.VALIDATIONERROR',
                    'Sorry, an error has occurred. Please try again later.'
                ),
                'validation'
            );
            
            // Answer Failure:
            
            return false;
            
        }
        
        // Answer Success:
        
        return true;
    }
842413
public function prepareCheckDigitsCalculate($iban)
    {
        $firstPlaces = substr((string)$iban, 0, 2);
        $lastPlaces = substr((string)$iban, 4);
        $conversion = strtr((string)$lastPlaces . (string)$firstPlaces . '00', $this->charMatching);
        return $conversion;
    }
656018
public function isValid($value, $context = null)
    {
        $this->_getValidators();
        $this->messages = array();
        $result         = false;
        foreach ($this->validators as $element) {
            /** @var $validator ValidatorInterface */
            $validator = $element['instance'];
            if (!$validator->isValid($value, $context)) {
                $messages       = $validator->getMessages();
                $this->messages = array_replace_recursive($this->messages, $messages);
                continue;
            }
            $result         = true;
        }
        return $result;
    }
583423
public function isValid( $value ) {
        parent::isValid( $value );

        $parser = new \Bumble\Uri\UriParser();
        $this->URI = $parser->parse( $value );

        if( !$this->URI ) {
            $this->error( self::INVALID_URI, $value );
            return false;
        }

        if( $this->hasMessages() )
            return false;
        
        return true;
    }
575490
public static function checkEcn($ecn)
    {
        $ecn = trim(StringUtils::removeHyphen($ecn));
        /*
         * ECN을 더이상 사용하지 않고, 그 대안으로 UCI를 사용하도록 하였다.
         * 기존에 ECN을 발급받은 도서들의 경우
         * UCI를 발급받지 않고,
         * ECN 번호 앞에 I410을 붙여 UCI 번호로 하기로 하였다.
         */
        $ecn = str_replace('I410', '', $ecn);

        if (mb_strlen($ecn) !== 23 || preg_match('/0{23}/', $ecn)) {
            throw new MsgException('ECN 형식에 맞지 않습니다.');
        }
    }
159341
protected function validateAttribute($rules) {
        $validator = new Validator();
        foreach ($rules as $key => $item) {
            if (!$this->isNewRecord && !$this->hasAttribute($key)) {
                continue;
            }
            $item = $validator->converterRule($item);
            // 增加必须判断
            if (!$this->hasAttribute($key)
                && !isset($item['rules']['required'])) {
                continue;
            }
            $value = $this->getAttributeValue($key);
            foreach ($item['rules'] as $rule => $args) {
                if (is_callable($args)) {
                    if (false !== call_user_func($args, $value)) {
                        continue;
                    }
                    $validator->messages()->add($key, $validator->getMessage($key, $rule, $item['message']));
                    continue;
                }
                if (method_exists($this, $rule)) {
                    if (false !== call_user_func([$this, $rule], $value)) {
                        continue;
                    }
                    $validator->messages()->add($key, $validator->getMessage($key, $rule, $item['message']));
                    continue;
                }
                if (Validator::buildRule($rule, (array)$args)
                    ->validate($value)) {
                    continue;
                }
                $validator->messages()->add($key, $validator->getMessage($key, $rule, $item['message']));
            }
        }
        $this->setError($validator->messages()->getMessages());
        return !$this->hasError();
    }
91560
public static function validator($validator = null)
    {
        if (func_num_args()) {
            static::$_validators[static::class] = $validator;
            return;
        }
        $self = static::class;
        if (isset(static::$_validators[$self])) {
            return static::$_validators[$self];
        }
        $class = static::$_classes['validator'];
        $validator = static::$_validators[$self] = new $class();
        static::_rules($validator);
        return $validator;
    }
772147
public function validate($value)
    {
        return ($value instanceof \stdClass && count((array) $value) <= $this->size)
            || parent::validate($value);
    }
828745
public static function isNationalTransaction($iban1, $iban2)
    {
        // remove whitespaces
        $iban1 = preg_replace('#\s+#','',$iban1);
        $iban2 = preg_replace('#\s+#','',$iban2);

        // check the country code
        if(stripos($iban1,substr($iban2,0,2)) === 0)
            return true;
        else
            return false;
    }
21154
public function validate($validator)
    {
        // Validate currency
        $currencies = $this->getAllowedCurrencies();
        $currency = $this->fieldCurrency->dataValue();
        if ($currency && $currencies && !in_array($currency, $currencies)) {
            $validator->validationError(
                $this->getName(),
                _t(
                    __CLASS__ . '.INVALID_CURRENCY',
                    'Currency {currency} is not in the list of allowed currencies',
                    ['currency' => $currency]
                )
            );
            return false;
        }

        // Field-specific validation
        return $this->fieldAmount->validate($validator) && $this->fieldCurrency->validate($validator);
    }
497094
public function validate($value)
    {
        if (!is_array($value)) {
            return false;
        }

        if (empty($value)) {
            return true;
        }

        return $this->checkIfAllUniques($value);
    }
286939
public static function validate($value)
    {
        if (!is_array($value)) {
            return false;
        }
        if (isset($value['from']) && IdentityValidator::validate($value['from']) === false) {
            return false;
        }
        if (isset($value['to']) && IdentityValidator::validate($value['to']) === false) {
            return false;
        }
        if (!isset($value['topics']) || !is_array($value['topics'])) {
            return false;
        }
        foreach ($value['topics'] as $topic) {
            if (IdentityValidator::validate($topic) === false) {
                return false;
            }
        }
        if (!isset($value['payload'])) {
            return false;
        }
        if (HexValidator::validate($value['payload']) === false) {
            return false;
        }
        if (!isset($value['priority'])) {
            return false;
        }
        if (QuantityValidator::validate($value['priority']) === false) {
            return false;
        }
        if (!isset($value['ttl'])) {
            return false;
        }
        if (isset($value['ttl']) && QuantityValidator::validate($value['ttl']) === false) {
            return false;
        }
        return true;
    }
1335174
public function isValid($value)
    {
        $valueString = (string) $value;

        $this->_setValue($valueString);

        if ((ip2long($valueString) === false) || (long2ip(ip2long($valueString)) !== $valueString)) {
            if (!function_exists('inet_pton')) {
                $this->_error();
                return false;
            } else if ((@inet_pton($value) === false) ||(inet_ntop(@inet_pton($value)) !== $valueString)) {
                $this->_error();
                return false;
            }
        }

        return true;
    }
1324555
public function calculateCheckDigits($iban)
    {
        $value = $this->prepareCheckDigitsCalculate($iban);

        $modulo = null;
        $valueLength = strlen($value);
        $nextPosition = 0;
        $nextLength = 9;
        do {
            $subValue = substr($value, $nextPosition, $nextLength);
            $subValue = $modulo . $subValue;
            $modulo = $subValue % 97;

            $nextPosition += $nextLength;
            $nextLength = 9 - strlen($modulo);
        } while ($valueLength > $nextPosition);

        $checksum = 98 - $modulo;
        return (int)$checksum;
    }
484781
public function getIBAN()
    {
        if (!is_null($this->iban)) {
            return $this->iban;
        }
        $this->country = strtoupper($this->country);

        $part1 = ord($this->country[0]) - ord('A') + 10;
        $part2 = ord($this->country[1]) - ord('A') + 10;

        $accountPrefix = 0;
        $accountNumber = $this->account;
        if (strpos($accountNumber, '-') !== false) {
            $accountParts = explode('-', $accountNumber);
            $accountPrefix = $accountParts[0];
            $accountNumber = $accountParts[1];
        }

        $numeric = sprintf('%04d%06d%010d%d%d00', $this->bank, $accountPrefix, $accountNumber, $part1, $part2);

        $mod = '';
        foreach (str_split($numeric) as $n) {
            $mod = ($mod . $n) % 97;
        }
        $mod = intval($mod);

        $this->iban = sprintf('%.2s%02d%04d%06d%010d', $this->country, 98 - $mod, $this->bank, $accountPrefix, $accountNumber);

        return $this->iban;
    }
945298
public function validate(BaseValue $value)
    {
        $isValid = true;

        if ($this->constraints['maxStringLength'] !== false &&
            $this->constraints['maxStringLength'] !== 0 &&
            mb_strlen($value->text) > $this->constraints['maxStringLength']) {
            $this->errors[] = new ValidationError(
                'The string can not exceed %size% character.',
                'The string can not exceed %size% characters.',
                array(
                    '%size%' => $this->constraints['maxStringLength'],
                )
            );
            $isValid = false;
        }
        if ($this->constraints['minStringLength'] !== false &&
            $this->constraints['minStringLength'] !== 0 &&
            mb_strlen($value->text) < $this->constraints['minStringLength']) {
            $this->errors[] = new ValidationError(
                'The string cannot be shorter than %size% character.',
                'The string cannot be shorter than %size% characters.',
                array(
                    '%size%' => $this->constraints['minStringLength'],
                )
            );
            $isValid = false;
        }

        return $isValid;
    }