7
7
use yii \validators \Validator ;
8
8
9
9
/**
10
- *
11
10
* ECCValidator class
12
11
*
13
12
* Credit Card Validator
17
16
*
18
17
* References of the Mod 10 algorithm
19
18
* http://en.wikipedia.org/wiki/Luhn_algorithm#Mod_10.2B5_Variant
20
- *
21
19
*/
22
20
class ECCValidator extends Validator
23
21
{
24
22
/**
25
- *
26
23
* Detected Credit Card list
24
+ *
27
25
* @var string
26
+ *
28
27
* @link http://en.wikipedia.org/wiki/Bank_card_number#cite_note-NoMoreBankCard-4
29
28
*/
30
29
const MAESTRO = 'Maestro ' ;
@@ -41,7 +40,6 @@ class ECCValidator extends Validator
41
40
const LASER = 'Laser ' ;
42
41
const ALL = 'All ' ;
43
42
/**
44
- *
45
43
* @var array holds the regex patterns to check for valid
46
44
* Credit Card number prefixes
47
45
*/
@@ -58,29 +56,30 @@ class ECCValidator extends Validator
58
56
self ::SWITCH_CARD => '/^(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2}) \\d{10}( \\d{2,3})?)|(?:564182 \\d{10}( \\d{2,3})?)|(6(3(33[0-4][0-9])|759[0-9]{2}) \\d{10}( \\d{2,3})?)$/ ' ,
59
57
self ::ELECTRON => '/^(?:417500|4026 \\d{2}|4917 \\d{2}|4913 \\d{2}|4508 \\d{2}|4844 \\d{2}) \\d{10}$/ ' ,
60
58
self ::LASER => '/^(?:6304|6706|6771|6709) \\d{12}( \\d{2,3})?$/ ' ,
61
- self ::ALL => '/^(5[1-5][0-9]{14}|4[0-9]{12}([0-9]{3})?|3[47][0-9]{13}|3(0[0-5]|[68][0-9])[0-9]{11}|(6011\d{12}|65\d{14})|(3[0-9]{4}|2131|1800)[0-9]{11}|2(?:014|149) \\d{11}|8699[0-9]{11}|(6334[5-9][0-9]|6767[0-9]{2}) \\d{10}( \\d{2,3})?|(?:5020|6 \\d{3}) \\d{12}|56(10 \\d \\d|022[1-5]) \\d{10}|(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2}) \\d{10}( \\d{2,3})?)|(?:564182 \\d{10}( \\d{2,3})?)|(6(3(33[0-4][0-9])|759[0-9]{2}) \\d{10}( \\d{2,3})?)|(?:417500|4026 \\d{2}|4917 \\d{2}|4913 \\d{2}|4508 \\d{2}|4844 \\d{2}) \\d{10}|(?:417500|4026 \\d{2}|4917 \\d{2}|4913 \\d{2}|4508 \\d{2}|4844 \\d{2}) \\d{10})$/ '
59
+ self ::ALL => '/^(5[1-5][0-9]{14}|4[0-9]{12}([0-9]{3})?|3[47][0-9]{13}|3(0[0-5]|[68][0-9])[0-9]{11}|(6011\d{12}|65\d{14})|(3[0-9]{4}|2131|1800)[0-9]{11}|2(?:014|149) \\d{11}|8699[0-9]{11}|(6334[5-9][0-9]|6767[0-9]{2}) \\d{10}( \\d{2,3})?|(?:5020|6 \\d{3}) \\d{12}|56(10 \\d \\d|022[1-5]) \\d{10}|(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2}) \\d{10}( \\d{2,3})?)|(?:564182 \\d{10}( \\d{2,3})?)|(6(3(33[0-4][0-9])|759[0-9]{2}) \\d{10}( \\d{2,3})?)|(?:417500|4026 \\d{2}|4917 \\d{2}|4913 \\d{2}|4508 \\d{2}|4844 \\d{2}) \\d{10}|(?:417500|4026 \\d{2}|4917 \\d{2}|4913 \\d{2}|4508 \\d{2}|4844 \\d{2}) \\d{10})$/ ' ,
62
60
];
63
61
/**
64
- *
65
62
* @var string set with selected Credit Card type to check -ie ECCValidator::MAESTRO
66
63
*/
67
64
public $ format = self ::ALL ;
68
65
/**
69
- * @var boolean whether the attribute value can be null or empty. Defaults to true,
70
- * meaning that if the attribute is empty, it is considered valid.
66
+ * @var bool whether the attribute value can be null or empty. Defaults to true,
67
+ * meaning that if the attribute is empty, it is considered valid
71
68
*/
72
69
public $ allowEmpty = true ;
73
70
74
71
/**
75
72
* Validate attribute
73
+ *
76
74
* @see Validator::validateAttribute()
75
+ *
77
76
* @param \yii\base\Model $object
78
77
* @param string $attribute
78
+ *
79
79
* @throws Exception
80
80
*/
81
81
public function validateAttribute ($ object , $ attribute )
82
82
{
83
-
84
83
$ value = $ object ->$ attribute ;
85
84
if ($ this ->allowEmpty && $ this ->isEmpty ($ value )) {
86
85
return ;
@@ -92,51 +91,52 @@ public function validateAttribute($object, $attribute)
92
91
}
93
92
94
93
/**
95
- *
96
94
* Validates a Credit Card number
97
95
*
98
96
* @param string $creditCardNumber
99
97
*
100
98
* @throws \yii\base\Exception
99
+ *
101
100
* @return bool
102
101
* @return bool
103
102
*/
104
103
public function validateNumber ($ creditCardNumber )
105
104
{
106
-
107
- if (!$ this ->checkType ())
105
+ if (!$ this ->checkType ()) {
108
106
throw new Exception (Yii::t ('ECCValidator ' , 'The "format" property must be specified with a supported Credit Card format. ' ));
107
+ }
109
108
110
109
$ creditCardNumber = preg_replace ('/[ -]+/ ' , '' , $ creditCardNumber );
111
110
112
111
return $ this ->checkFormat ($ creditCardNumber ) && $ this ->mod10 ($ creditCardNumber );
113
112
}
114
113
115
114
/**
116
- *
117
115
* Validates a Credit Card date
118
116
*
119
- * @param integer $creditCardExpiredMonth
120
- * @param integer $creditCardExpiredYear
117
+ * @param int $creditCardExpiredMonth
118
+ * @param int $creditCardExpiredYear
121
119
*
122
120
* @return bool
123
121
*/
124
122
public function validateDate ($ creditCardExpiredMonth , $ creditCardExpiredYear )
125
123
{
126
-
127
124
$ currentYear = intval (date ('Y ' ));
128
125
$ currentMonth = intval (date ('m ' ));
129
126
130
- if (is_scalar ($ creditCardExpiredMonth )) $ creditCardExpiredMonth = intval ($ creditCardExpiredMonth );
131
- if (is_scalar ($ creditCardExpiredYear )) $ creditCardExpiredYear = intval ($ creditCardExpiredYear );
127
+ if (is_scalar ($ creditCardExpiredMonth )) {
128
+ $ creditCardExpiredMonth = intval ($ creditCardExpiredMonth );
129
+ }
130
+ if (is_scalar ($ creditCardExpiredYear )) {
131
+ $ creditCardExpiredYear = intval ($ creditCardExpiredYear );
132
+ }
132
133
133
134
return is_integer ($ creditCardExpiredMonth ) && is_integer ($ creditCardExpiredYear ) && $ creditCardExpiredMonth <= 12
134
135
&& ($ creditCardExpiredMonth >= 1 && $ creditCardExpiredYear > $ currentYear
135
136
&& $ creditCardExpiredYear < $ currentYear + 21 ) || ($ creditCardExpiredYear == $ currentYear && $ creditCardExpiredMonth >= $ currentMonth );
136
137
}
137
138
138
139
/**
139
- *
140
140
* Validates Credit Card holder
141
141
*
142
142
* @param string $creditCardHolder
@@ -145,31 +145,29 @@ public function validateDate($creditCardExpiredMonth, $creditCardExpiredYear)
145
145
*/
146
146
public function validateName ($ creditCardHolder )
147
147
{
148
-
149
148
return !empty ($ creditCardHolder ) && preg_match ('/^[A-Z ]+$/i ' , $ creditCardHolder );
150
149
}
151
150
152
151
/**
153
- *
154
152
* Validates holder, number, and dates of Credit Card numbers
155
153
*
156
154
* @param string $creditCardHolder
157
155
* @param string $creditCardNumber
158
- * @param integer $creditCardExpiredMonth
159
- * @param integer $creditCardExpiredYear
156
+ * @param int $creditCardExpiredMonth
157
+ * @param int $creditCardExpiredYear
160
158
*
161
159
* @return bool
162
160
*/
163
161
public function validateAll ($ creditCardHolder , $ creditCardNumber , $ creditCardExpiredMonth , $ creditCardExpiredYear )
164
162
{
165
-
166
163
return $ this ->validateName ($ creditCardHolder ) && $ this ->validateNumber ($ creditCardNumber ) && $ this ->validateDate ($ creditCardExpiredMonth , $ creditCardExpiredYear );
167
-
168
164
}
169
165
170
166
/**
171
167
* Checks Credit Card Prefixes
168
+ *
172
169
* @param $cardNumber
170
+ *
173
171
* @return bool
174
172
*/
175
173
protected function checkFormat ($ cardNumber )
@@ -179,8 +177,11 @@ protected function checkFormat($cardNumber)
179
177
180
178
/**
181
179
* Check credit card number by Mod 10 algorithm
180
+ *
182
181
* @param $cardNumber
182
+ *
183
183
* @return bool
184
+ *
184
185
* @see http://en.wikipedia.org/wiki/Luhn_algorithm#Mod_10.2B5_Variant
185
186
*/
186
187
protected function mod10 ($ cardNumber )
@@ -199,34 +200,35 @@ protected function mod10($cardNumber)
199
200
}
200
201
$ numSum += $ currentNum ;
201
202
}
202
- return ($ numSum % 10 == 0 );
203
+
204
+ return $ numSum % 10 == 0 ;
203
205
}
204
206
205
207
/**
206
- *
207
208
* Checks if Credit Card Format is a supported one
208
209
* and builds new pattern format in case user has
209
210
* a mixed match search (mastercard|visa)
210
211
*
211
- * @access private
212
- * @return boolean
212
+ * @return bool
213
213
*/
214
214
protected function checkType ()
215
215
{
216
-
217
216
if (is_scalar ($ this ->format )) {
218
217
return array_key_exists ($ this ->format , $ this ->patterns );
219
- } else if (is_array ($ this ->format )) {
220
- $ pattern = array () ;
218
+ } elseif (is_array ($ this ->format )) {
219
+ $ pattern = [] ;
221
220
foreach ($ this ->format as $ f ) {
222
- if (!array_key_exists ($ f , $ this ->patterns )) return false ;
221
+ if (!array_key_exists ($ f , $ this ->patterns )) {
222
+ return false ;
223
+ }
223
224
$ pattern [] = substr ($ this ->patterns [$ f ], 2 , strlen ($ this ->patterns [$ f ]) - 4 );
224
225
}
225
226
$ this ->format = 'custom ' ;
226
- $ this ->patterns [$ this ->format ] = '/^( ' . join ('| ' , $ pattern ) . ')$/ ' ;
227
+ $ this ->patterns [$ this ->format ] = '/^( ' . implode ('| ' , $ pattern ) . ')$/ ' ;
228
+
227
229
return true ;
228
230
}
229
- return false ;
230
231
232
+ return false ;
231
233
}
232
234
}
0 commit comments