-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRs.php
executable file
·72 lines (59 loc) · 2.61 KB
/
Rs.php
1
2
3
4
5
6
7
8
9
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/*
* Copyright (C) 2017 exru.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
namespace exru\rusvalidators;
use Yii;
use yii\validators\Validator;
class Rs extends Validator {
static $LENGTH = 20;
static $FACTOR = [7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1];
public $bikAttribute = 'bik';
private function _getRsFactor($value, $factor) {
$sum = 0;
$number = count($factor);
for ($i = 0; $i != $number; $i++) {
$sum += ($value[$i] * $factor[$i]) % 10;
}
return $sum % 10 == 0;
}
public function validateAttribute($model, $attribute) {
if (!$model->hasAttribute($this->bikAttribute) || $model->{$this->bikAttribute} == '') {
$model->addError($attribute, Yii::t('app', 'Для проверки поля "{attribute}" необходимо заполнить БИК', [
'attribute' => $model->getAttributeLabel($attribute)
]));
return;
}
if ($model->hasErrors($this->bikAttribute)) {
$model->addError($attribute, $model->getErrors($this->bikAttribute));
return;
}
$length = strlen($model->{$attribute});
if ($length != self::$LENGTH) {
$model->addError($attribute, Yii::t('app', '{attribute} должен быть длинной {length} символов', ['attribute' => $model->getAttributeLabel($attribute), 'length' => self::$LENGTH]));
return;
}
if (!$this->_getRsFactor(substr($model->{$this->bikAttribute}, -3, 3).$model->{$attribute}, self::$FACTOR)) {
$model->addError($attribute, Yii::t('app', 'Неправильный {attribute} или {bik}', [
'attribute' =>$model->getAttributeLabel($attribute),
'bik'=>$model->getAttributeLabel($this->bikAttribute)
]));
return;
}
}
}