77use yii \validators \Validator ;
88
99/**
10- *
1110 * ECCValidator class
1211 *
1312 * Credit Card Validator
1716 *
1817 * References of the Mod 10 algorithm
1918 * http://en.wikipedia.org/wiki/Luhn_algorithm#Mod_10.2B5_Variant
20- *
2119 */
2220class ECCValidator extends Validator
2321{
2422 /**
25- *
2623 * Detected Credit Card list
24+ *
2725 * @var string
26+ *
2827 * @link http://en.wikipedia.org/wiki/Bank_card_number#cite_note-NoMoreBankCard-4
2928 */
3029 const MAESTRO = 'Maestro ' ;
@@ -41,7 +40,6 @@ class ECCValidator extends Validator
4140 const LASER = 'Laser ' ;
4241 const ALL = 'All ' ;
4342 /**
44- *
4543 * @var array holds the regex patterns to check for valid
4644 * Credit Card number prefixes
4745 */
@@ -58,29 +56,30 @@ class ECCValidator extends Validator
5856 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})?)$/ ' ,
5957 self ::ELECTRON => '/^(?:417500|4026 \\d{2}|4917 \\d{2}|4913 \\d{2}|4508 \\d{2}|4844 \\d{2}) \\d{10}$/ ' ,
6058 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})$/ ' ,
6260 ];
6361 /**
64- *
6562 * @var string set with selected Credit Card type to check -ie ECCValidator::MAESTRO
6663 */
6764 public $ format = self ::ALL ;
6865 /**
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
7168 */
7269 public $ allowEmpty = true ;
7370
7471 /**
7572 * Validate attribute
73+ *
7674 * @see Validator::validateAttribute()
75+ *
7776 * @param \yii\base\Model $object
7877 * @param string $attribute
78+ *
7979 * @throws Exception
8080 */
8181 public function validateAttribute ($ object , $ attribute )
8282 {
83-
8483 $ value = $ object ->$ attribute ;
8584 if ($ this ->allowEmpty && $ this ->isEmpty ($ value )) {
8685 return ;
@@ -92,51 +91,52 @@ public function validateAttribute($object, $attribute)
9291 }
9392
9493 /**
95- *
9694 * Validates a Credit Card number
9795 *
9896 * @param string $creditCardNumber
9997 *
10098 * @throws \yii\base\Exception
99+ *
101100 * @return bool
102101 * @return bool
103102 */
104103 public function validateNumber ($ creditCardNumber )
105104 {
106-
107- if (!$ this ->checkType ())
105+ if (!$ this ->checkType ()) {
108106 throw new Exception (Yii::t ('ECCValidator ' , 'The "format" property must be specified with a supported Credit Card format. ' ));
107+ }
109108
110109 $ creditCardNumber = preg_replace ('/[ -]+/ ' , '' , $ creditCardNumber );
111110
112111 return $ this ->checkFormat ($ creditCardNumber ) && $ this ->mod10 ($ creditCardNumber );
113112 }
114113
115114 /**
116- *
117115 * Validates a Credit Card date
118116 *
119- * @param integer $creditCardExpiredMonth
120- * @param integer $creditCardExpiredYear
117+ * @param int $creditCardExpiredMonth
118+ * @param int $creditCardExpiredYear
121119 *
122120 * @return bool
123121 */
124122 public function validateDate ($ creditCardExpiredMonth , $ creditCardExpiredYear )
125123 {
126-
127124 $ currentYear = intval (date ('Y ' ));
128125 $ currentMonth = intval (date ('m ' ));
129126
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+ }
132133
133134 return is_integer ($ creditCardExpiredMonth ) && is_integer ($ creditCardExpiredYear ) && $ creditCardExpiredMonth <= 12
134135 && ($ creditCardExpiredMonth >= 1 && $ creditCardExpiredYear > $ currentYear
135136 && $ creditCardExpiredYear < $ currentYear + 21 ) || ($ creditCardExpiredYear == $ currentYear && $ creditCardExpiredMonth >= $ currentMonth );
136137 }
137138
138139 /**
139- *
140140 * Validates Credit Card holder
141141 *
142142 * @param string $creditCardHolder
@@ -145,31 +145,29 @@ public function validateDate($creditCardExpiredMonth, $creditCardExpiredYear)
145145 */
146146 public function validateName ($ creditCardHolder )
147147 {
148-
149148 return !empty ($ creditCardHolder ) && preg_match ('/^[A-Z ]+$/i ' , $ creditCardHolder );
150149 }
151150
152151 /**
153- *
154152 * Validates holder, number, and dates of Credit Card numbers
155153 *
156154 * @param string $creditCardHolder
157155 * @param string $creditCardNumber
158- * @param integer $creditCardExpiredMonth
159- * @param integer $creditCardExpiredYear
156+ * @param int $creditCardExpiredMonth
157+ * @param int $creditCardExpiredYear
160158 *
161159 * @return bool
162160 */
163161 public function validateAll ($ creditCardHolder , $ creditCardNumber , $ creditCardExpiredMonth , $ creditCardExpiredYear )
164162 {
165-
166163 return $ this ->validateName ($ creditCardHolder ) && $ this ->validateNumber ($ creditCardNumber ) && $ this ->validateDate ($ creditCardExpiredMonth , $ creditCardExpiredYear );
167-
168164 }
169165
170166 /**
171167 * Checks Credit Card Prefixes
168+ *
172169 * @param $cardNumber
170+ *
173171 * @return bool
174172 */
175173 protected function checkFormat ($ cardNumber )
@@ -179,8 +177,11 @@ protected function checkFormat($cardNumber)
179177
180178 /**
181179 * Check credit card number by Mod 10 algorithm
180+ *
182181 * @param $cardNumber
182+ *
183183 * @return bool
184+ *
184185 * @see http://en.wikipedia.org/wiki/Luhn_algorithm#Mod_10.2B5_Variant
185186 */
186187 protected function mod10 ($ cardNumber )
@@ -199,34 +200,35 @@ protected function mod10($cardNumber)
199200 }
200201 $ numSum += $ currentNum ;
201202 }
202- return ($ numSum % 10 == 0 );
203+
204+ return $ numSum % 10 == 0 ;
203205 }
204206
205207 /**
206- *
207208 * Checks if Credit Card Format is a supported one
208209 * and builds new pattern format in case user has
209210 * a mixed match search (mastercard|visa)
210211 *
211- * @access private
212- * @return boolean
212+ * @return bool
213213 */
214214 protected function checkType ()
215215 {
216-
217216 if (is_scalar ($ this ->format )) {
218217 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 = [] ;
221220 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+ }
223224 $ pattern [] = substr ($ this ->patterns [$ f ], 2 , strlen ($ this ->patterns [$ f ]) - 4 );
224225 }
225226 $ this ->format = 'custom ' ;
226- $ this ->patterns [$ this ->format ] = '/^( ' . join ('| ' , $ pattern ) . ')$/ ' ;
227+ $ this ->patterns [$ this ->format ] = '/^( ' . implode ('| ' , $ pattern ) . ')$/ ' ;
228+
227229 return true ;
228230 }
229- return false ;
230231
232+ return false ;
231233 }
232234}
0 commit comments