-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsraeliIdValidator.php
137 lines (121 loc) · 3.73 KB
/
IsraeliIdValidator.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace mipotech\yii2israelidvalidator;
use Yii;
use yii\validators\Validator;
/**
*
* @author Chaim Leichman, MIPO Technologies LTD
*
* Inspired by:
* @link http://opencodeoasis.blogspot.com/2008/08/blog-post_10.html
*/
class IsraeliIdValidator extends Validator
{
/**
* @var string custom error message for invalid checksum
*/
public $messageInvalidChecksum;
/**
* @var string custom error message for invalid characters
*/
public $messageInvalidChars;
/**
* @var string custom error message for number too long
*/
public $messageTooLong;
/**
* @var string custom error message for number too short
*/
public $messageTooShort;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
Yii::$app->i18n->translations['tzvalidator'] = [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => "@vendor/mipotech/yii2-israeli-id-validator/messages",
'forceTranslation' => true,
];
}
/**
* @inheritdoc
*/
public function validateAttribute($model, $attribute)
{
$value = $model->$attribute;
$errorMessages = $this->errorMessages;
if (strlen($value) < 5) {
$this->addError($model, $attribute, $errorMessages['tooShort']);
} elseif (strlen($value) > 9) {
$this->addError($model, $attribute, $errorMessages['tooLong']);
} elseif (preg_match('/[^0-9]/', $value)) {
$this->addError($model, $attribute, $errorMessages['invalidChars']);
}
$paddedValue = str_pad($value, 9, '0', STR_PAD_LEFT);
$mone = 0;
for ($i = 0; $i < 9; $i++) {
$char = mb_substr($paddedValue, $i, 1);
$incNum = intval($char);
$incNum*=($i%2)+1;
if ($incNum > 9) {
$incNum -= 9;
}
$mone += $incNum;
}
if ($mone % 10 != 0) {
$this->addError($model, $attribute, $errorMessages[]);
}
}
/**
* @inheritdoc
*/
public function clientValidateAttribute($model, $attribute, $view)
{
$errorMessages = $this->errorMessages;
$errorMessages = array_map('addslashes', $errorMessages);
$js = <<<EOF
if (value.length < 5) {
messages.push('{$errorMessages['tooShort']}');
} else if (value.length > 9) {
messages.push('{$errorMessages['tooLong']}');
} else if (/[^0-9]/.test(value)) {
messages.push('{$errorMessages['invalidChars']}');
}
var paddedValue = value;
if (paddedValue.length < 9) {
while(paddedValue.length < 9) {
paddedValue = '0' + paddedValue;
}
}
var mone = 0, incNum;
for (var i=0; i < 9; i++) {
incNum = Number(paddedValue.charAt(i));
incNum *= (i%2)+1;
if (incNum > 9) {
incNum -= 9;
}
mone += incNum;
}
if (mone % 10 != 0) {
messages.push('{$errorMessages['invalidChecksum']}');
}
EOF;
return $js;
}
/**
* Generate an array of error messages
*
* @return array
*/
protected function getErrorMessages(): array
{
return [
'invalidChecksum' => $this->messageInvalidChecksum ?: Yii::t('tzvalidator', 'ID number is not valid'),
'invalidChars' => $this->messageInvalidChars ?: Yii::t('tzvalidator', 'ID number must include only digits'),
'tooLong' => $this->messageTooLong ?: Yii::t('tzvalidator', 'ID number must be no more than 9 digits'),
'tooShort' => $this->messageTooShort ?: Yii::t('tzvalidator', 'ID number must be at least 5 digits'),
];
}
}