Guest User

Untitled

a guest
Nov 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. <?php
  2. /**
  3. *
  4. */
  5. class sfValidatorBIC extends sfValidatorBase
  6. {
  7. protected $validator;
  8. protected $errors;
  9.  
  10. protected function configure($options = array(), $messages = array())
  11. {
  12. $this->validator = $this;
  13.  
  14. $this->addRequiredOption('bank_code_field');
  15. $this->addRequiredOption('guichet_code_field');
  16. $this->addRequiredOption('account_number_field');
  17. $this->addRequiredOption('rib_key_field');
  18.  
  19. $this->addOption('empty_values', false);
  20.  
  21. $this->setMessage('invalid', 'Your BIC key is invalid.');
  22.  
  23. $this->addMessage('length_bank_code', 'Your bank code must have 5 characters.');
  24. $this->addMessage('length_guichet_code', 'Your agency code must have 5 characters.');
  25. $this->addMessage('max_length_account_number', 'Your account number is too long (11 characters max).');
  26. $this->addMessage('min_length_account_number', 'Your account number is too short (8 characters min).');
  27. $this->addMessage('length_account_number', 'Your account number must have 11 characteres.');
  28. $this->addMessage('length_bic', 'Your bic key must have 2 characters.');
  29.  
  30. parent::configure($options, $messages);
  31. }
  32.  
  33. /**
  34. * @see sfValidatorBase
  35. */
  36. protected function doClean($values)
  37. {
  38. $this->errors = null;
  39. if($values === null || empty($values))
  40. throw new InvalidArgumentException('You must pass an none empty array parameter to the clean() method');
  41.  
  42. if (!is_array($values))
  43. throw new InvalidArgumentException('You must pass an array parameter to the clean() method');
  44.  
  45. $bankCode = $values[$this->getOption('bank_code_field')];
  46. $guichetCode = $values[$this->getOption('guichet_code_field')];
  47. $accountNumber = $values[$this->getOption('account_number_field')];
  48. $bicKey = $values[$this->getOption('rib_key_field')];
  49.  
  50. if(strlen($accountNumber) < 8)
  51. $this->errors[$this->getOption('account_number_field')] = new sfValidatorError($this, 'min_length_account_number');
  52.  
  53. if(strlen($accountNumber) > 11)
  54. $this->errors[$this->getOption('account_number_field')] = new sfValidatorError($this, 'max_length_account_number');
  55.  
  56. if(strlen($accountNumber) < 11)
  57. $accountNumber = sprintf("%011s", $accountNumber);
  58.  
  59. if(strlen($bankCode) < 5 || strlen($bankCode) > 5)
  60. $this->errors[$this->getOption('bank_code_field')] = new sfValidatorError($this, 'length_bank_code');
  61.  
  62. if(strlen($guichetCode) < 5 || strlen($guichetCode) > 5)
  63. $this->errors[$this->getOption('guichet_code_field')] = new sfValidatorError($this, 'length_guichet_code');
  64.  
  65. if(strlen($bicKey) < 2 || strlen($bicKey) > 2)
  66. $this->errors[$this->getOption('rib_key_field')] = new sfValidatorError($this, 'length_bic');
  67.  
  68. if($this->errors)
  69. {
  70. throw new sfValidatorErrorSchema($this, $this->errors);
  71. return false;
  72. }
  73.  
  74. if (!$this->_checkRIB($bankCode, $guichetCode, $accountNumber, $bicKey))
  75. {
  76. throw new sfValidatorErrorSchema($this, array(
  77. $this->getOption('rib_key_field') => new sfValidatorError($this, 'invalid')
  78. ));
  79. }
  80.  
  81. return $values;
  82. }
  83.  
  84. protected function _checkRIB($bankCode, $guichetCode, $accountNumber, $bicKey)
  85. {
  86. if(strlen($accountNumber) != 11)
  87. $this->errors[$this->getOption('account_number_field')] = new sfValidatorError($this->validator, 'length_account_number');
  88.  
  89. $accountNumber = $this->_convertToDecimal($accountNumber);
  90.  
  91. $account = $bankCode . $guichetCode . $accountNumber . $bicKey;
  92.  
  93. return (strlen($account) >= 21 && bcmod($account, 97) == 0);
  94. }
  95.  
  96. protected function _convertToDecimal($text)
  97. {
  98. $letters = array(
  99. 'A' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5, 'F' => 6, 'G' => 7,
  100. 'H' => 8, 'I' => 9, 'J' => 1, 'K' => 2, 'L' => 3, 'M' => 4, 'N' => 5,
  101. 'O' => 6, 'P' => 7, 'Q' => 8, 'R' => 9, 'S' => 2, 'T' => 3, 'U' => 4,
  102. 'V' => 5, 'W' => 6, 'X' => 7, 'Y' => 8, 'Z' => 9,
  103. );
  104.  
  105. $search = array_keys($letters);
  106. $replace = array_values($letters);
  107.  
  108. return str_replace($search, $replace, $text);
  109. }
  110. }
Add Comment
Please, Sign In to add comment