Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- */
- class sfValidatorBIC extends sfValidatorBase
- {
- protected $validator;
- protected $errors;
- protected function configure($options = array(), $messages = array())
- {
- $this->validator = $this;
- $this->addRequiredOption('bank_code_field');
- $this->addRequiredOption('guichet_code_field');
- $this->addRequiredOption('account_number_field');
- $this->addRequiredOption('rib_key_field');
- $this->addOption('empty_values', false);
- $this->setMessage('invalid', 'Your BIC key is invalid.');
- $this->addMessage('length_bank_code', 'Your bank code must have 5 characters.');
- $this->addMessage('length_guichet_code', 'Your agency code must have 5 characters.');
- $this->addMessage('max_length_account_number', 'Your account number is too long (11 characters max).');
- $this->addMessage('min_length_account_number', 'Your account number is too short (8 characters min).');
- $this->addMessage('length_account_number', 'Your account number must have 11 characteres.');
- $this->addMessage('length_bic', 'Your bic key must have 2 characters.');
- parent::configure($options, $messages);
- }
- /**
- * @see sfValidatorBase
- */
- protected function doClean($values)
- {
- $this->errors = null;
- if($values === null || empty($values))
- throw new InvalidArgumentException('You must pass an none empty array parameter to the clean() method');
- if (!is_array($values))
- throw new InvalidArgumentException('You must pass an array parameter to the clean() method');
- $bankCode = $values[$this->getOption('bank_code_field')];
- $guichetCode = $values[$this->getOption('guichet_code_field')];
- $accountNumber = $values[$this->getOption('account_number_field')];
- $bicKey = $values[$this->getOption('rib_key_field')];
- if(strlen($accountNumber) < 8)
- $this->errors[$this->getOption('account_number_field')] = new sfValidatorError($this, 'min_length_account_number');
- if(strlen($accountNumber) > 11)
- $this->errors[$this->getOption('account_number_field')] = new sfValidatorError($this, 'max_length_account_number');
- if(strlen($accountNumber) < 11)
- $accountNumber = sprintf("%011s", $accountNumber);
- if(strlen($bankCode) < 5 || strlen($bankCode) > 5)
- $this->errors[$this->getOption('bank_code_field')] = new sfValidatorError($this, 'length_bank_code');
- if(strlen($guichetCode) < 5 || strlen($guichetCode) > 5)
- $this->errors[$this->getOption('guichet_code_field')] = new sfValidatorError($this, 'length_guichet_code');
- if(strlen($bicKey) < 2 || strlen($bicKey) > 2)
- $this->errors[$this->getOption('rib_key_field')] = new sfValidatorError($this, 'length_bic');
- if($this->errors)
- {
- throw new sfValidatorErrorSchema($this, $this->errors);
- return false;
- }
- if (!$this->_checkRIB($bankCode, $guichetCode, $accountNumber, $bicKey))
- {
- throw new sfValidatorErrorSchema($this, array(
- $this->getOption('rib_key_field') => new sfValidatorError($this, 'invalid')
- ));
- }
- return $values;
- }
- protected function _checkRIB($bankCode, $guichetCode, $accountNumber, $bicKey)
- {
- if(strlen($accountNumber) != 11)
- $this->errors[$this->getOption('account_number_field')] = new sfValidatorError($this->validator, 'length_account_number');
- $accountNumber = $this->_convertToDecimal($accountNumber);
- $account = $bankCode . $guichetCode . $accountNumber . $bicKey;
- return (strlen($account) >= 21 && bcmod($account, 97) == 0);
- }
- protected function _convertToDecimal($text)
- {
- $letters = array(
- 'A' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5, 'F' => 6, 'G' => 7,
- 'H' => 8, 'I' => 9, 'J' => 1, 'K' => 2, 'L' => 3, 'M' => 4, 'N' => 5,
- 'O' => 6, 'P' => 7, 'Q' => 8, 'R' => 9, 'S' => 2, 'T' => 3, 'U' => 4,
- 'V' => 5, 'W' => 6, 'X' => 7, 'Y' => 8, 'Z' => 9,
- );
- $search = array_keys($letters);
- $replace = array_values($letters);
- return str_replace($search, $replace, $text);
- }
- }
Add Comment
Please, Sign In to add comment