Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Controller_Validator_Basic often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Controller_Validator_Basic, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class Controller_Validator_Basic extends Controller_Validator_Abstract |
||
4 | { |
||
5 | public function init() |
||
6 | { |
||
7 | parent::init(); |
||
8 | |||
9 | $this->alias = array_merge( |
||
10 | $this->alias, |
||
11 | array( |
||
12 | 'same' => 'eq', |
||
13 | 'different' => 'ne', |
||
14 | ) |
||
15 | ); |
||
16 | } |
||
17 | |||
18 | /* \section Value Test Rules: General */ |
||
19 | |||
20 | public function rule_required($a) |
||
21 | { |
||
22 | if ($a === '' || $a === false || $a === null) { |
||
23 | return $this->fail('must not be empty'); |
||
24 | } |
||
25 | } |
||
26 | |||
27 | public function rule_len($a) |
||
28 | { |
||
29 | if (!is_string($a)) { |
||
30 | return $this->fail('must be a string'); |
||
31 | } |
||
32 | |||
33 | $this->prefix = 'length of '; |
||
34 | |||
35 | return strlen($a); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Requires a regex pattern as the |
||
40 | * next rule in the chain. |
||
41 | * |
||
42 | * Please give your rule a custom error message. |
||
43 | */ |
||
44 | public function rule_regex($a) |
||
45 | { |
||
46 | $opt = array(); |
||
47 | $rule = $this->pullRule(); |
||
48 | if ($rule[0] != '/') { |
||
49 | $rule = '/^'.$rule.'*$/'; |
||
50 | } |
||
51 | $opt['regexp'] = $rule; |
||
52 | |||
53 | if (!filter_var($a, FILTER_VALIDATE_REGEXP, ['options' => $opt])) { |
||
54 | return $this->fail('does not match the pattern'); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | /* \section Value Test Rules: Value Lists */ |
||
59 | |||
60 | /** |
||
61 | * Checks that value is in the list provided. |
||
62 | * Syntax: |in|foo,bar,foobar. |
||
63 | */ |
||
64 | public function rule_in($a) |
||
65 | { |
||
66 | $vals = $this->prep_in_vals($a); |
||
67 | if (!in_array($a, $vals)) { |
||
68 | return $this->fail('has an invalid value'); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Checks that value is not in the list provided. |
||
74 | * Syntax: |not_in|foo,bar,foobar. |
||
75 | */ |
||
76 | public function rule_not_in($a) |
||
77 | { |
||
78 | $vals = $this->prep_in_vals($a); |
||
79 | if (in_array($a, $vals)) { |
||
80 | return $this->fail('has an invalid value'); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | /* \section Value Comparison Rules: absolute value */ |
||
85 | |||
86 | /** |
||
87 | * Inclusive range check. |
||
88 | * |
||
89 | * Overloaded: checks value for numbers, |
||
90 | * string-length for other values. |
||
91 | * |
||
92 | * Next 2 rules must specify the min and max |
||
93 | */ |
||
94 | public function rule_between($a) |
||
95 | { |
||
96 | $min = $this->pullRule(true); |
||
97 | $max = $this->pullRule(true); |
||
98 | |||
99 | if (is_numeric($a)) { |
||
100 | if ($a < $min || $a > $max) { |
||
101 | return $this->fail('must be between {{arg1}} and {{arg2}}', $min, $max); |
||
102 | } |
||
103 | } else { |
||
104 | return $this->fail('Must be a numeric value'); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | View Code Duplication | public function rule_gt($a) |
|
109 | { |
||
110 | $target = $this->pullRule(true); |
||
111 | if ($a <= $target) { |
||
112 | return $this->fail('must be greater than {{arg1}}', $target); |
||
113 | } |
||
114 | |||
115 | return $a; |
||
116 | } |
||
117 | |||
118 | View Code Duplication | public function rule_lt($a) |
|
119 | { |
||
120 | $target = $this->pullRule(true); |
||
121 | if ($a >= $target) { |
||
122 | return $this->fail('must be less than {{arg1}}', $target); |
||
123 | } |
||
124 | |||
125 | return $a; |
||
126 | } |
||
127 | |||
128 | View Code Duplication | public function rule_gte($a) |
|
129 | { |
||
130 | $target = $this->pullRule(true); |
||
131 | if ($a < $target) { |
||
132 | return $this->fail('must be at least {{arg1}}', $target); |
||
133 | } |
||
134 | |||
135 | return $a; |
||
136 | } |
||
137 | |||
138 | View Code Duplication | public function rule_lte($a) |
|
139 | { |
||
140 | $target = $this->pullRule(true); |
||
141 | if ($a > $target) { |
||
142 | return $this->fail('must be not greater than {{arg1}}', $target); |
||
143 | } |
||
144 | |||
145 | return $a; |
||
146 | } |
||
147 | |||
148 | public function rule_eq($a) |
||
149 | { |
||
150 | $target = $this->pullRule(); |
||
151 | if ($a !== $target) { |
||
152 | return $this->fail('must be exactly {{arg1}}', $target); |
||
153 | } |
||
154 | |||
155 | return $a; |
||
156 | } |
||
157 | |||
158 | View Code Duplication | public function rule_eqf($a) |
|
159 | { |
||
160 | $target=$this->pullRule(true); |
||
161 | if ($a !== $this->get($target)) { |
||
162 | return $this->fail('must be same as {{arg1}}', $target); |
||
163 | } |
||
164 | return $a; |
||
165 | } |
||
166 | |||
167 | /* \section Value Test Rules: Numeric */ |
||
168 | |||
169 | /** |
||
170 | * Checks for int or float. |
||
171 | */ |
||
172 | public function rule_number($a) |
||
173 | { |
||
174 | if (!is_numeric($a)) { |
||
175 | return $this->fail('must be a number'); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | public function rule_decimal($a) |
||
180 | { |
||
181 | if (!preg_match('/^[0-9]+\.[0-9]+$/', $a)) { |
||
182 | return $this->fail('must be a decimal number: eg 12.34'); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | public function rule_phone($a) |
||
187 | { |
||
188 | if (strlen(preg_replace('/[^0-9]/', '', $a)) < 6) { |
||
189 | return $this->fail('must contain valid phone number'); |
||
190 | } |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Checks for a specific number of |
||
195 | * decimal places. |
||
196 | * |
||
197 | * Arg: int- number of places |
||
198 | */ |
||
199 | public function rule_decimal_places($a) |
||
200 | { |
||
201 | $places = $this->pullRule(); |
||
202 | $pattern = sprintf('/^[0-9]+\.[0-9]{%s}$/', $places); |
||
203 | if (!preg_match($pattern, $a)) { |
||
204 | return $this->fail('Must have {{arg1}} decimal places', $places); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | public function rule_int($a) |
||
209 | { |
||
210 | if (!preg_match('/^[0-9]*$/', $a)) { |
||
211 | return $this->fail('Must be an integer: eg 1234'); |
||
212 | } |
||
213 | } |
||
214 | |||
215 | /* \section Value Test Rules: String */ |
||
216 | |||
217 | /** |
||
218 | * Test for A-Za-z. |
||
219 | */ |
||
220 | public function rule_alpha($a) |
||
221 | { |
||
222 | $msg = 'must contain only letters'; |
||
223 | if (!preg_match('/^([A-Za-z])*$/', $a)) { |
||
224 | return $this->fail($msg); |
||
225 | } |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Test for A-Za-z0-9. |
||
230 | */ |
||
231 | public function rule_alphanum($a) |
||
232 | { |
||
233 | $msg = 'must contain only digits and letters'; |
||
234 | if (!preg_match('/^([a-zA-Z0-9])*$/', $a)) { |
||
235 | return $this->fail($msg); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | public function rule_type($a) |
||
240 | { |
||
241 | $this->prefix = 'type of '; |
||
242 | |||
243 | return gettype($a); |
||
244 | } |
||
245 | |||
246 | public function rule_class($a) |
||
247 | { |
||
248 | if (!is_object($a)) { |
||
249 | return $this->fail('is not an object'); |
||
250 | } |
||
251 | $this->prefix = 'class of '; |
||
252 | |||
253 | return get_class($a); |
||
254 | } |
||
255 | |||
256 | /* \section Value Test Rules: Boolean */ |
||
257 | |||
258 | /** |
||
259 | * Validate for true|false|t|f|1|0|yes|no|y|n. |
||
260 | * |
||
261 | * Normalizes to lower case |
||
262 | */ |
||
263 | public function rule_bool($a) |
||
264 | { |
||
265 | // We don't use PHP inbuilt test - a bit restrictive |
||
266 | |||
267 | // Changes PHP true/false to 1, 0 |
||
268 | $a = strtolower($a); |
||
269 | |||
270 | $vals = array('true', 'false', 't', 'f', 1, 0, 'yes', 'no', 'y', 'n'); |
||
271 | |||
272 | if (!in_array($a, $vals)) { |
||
273 | return $this->fail('Must be a boolean value'); |
||
274 | } |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Validate for true|t|1|yes|y. |
||
279 | * |
||
280 | * Normalizes to lower case |
||
281 | * |
||
282 | * Useful for 'if' rules |
||
283 | */ |
||
284 | public function rule_true($a) |
||
285 | { |
||
286 | // Changes PHP true to 1 |
||
287 | $a = strtolower($a); |
||
288 | |||
289 | $vals = array('true', 't', 1, 'yes', 'y'); |
||
290 | |||
291 | if (!in_array($a, $vals)) { |
||
292 | return $this->fail('Must be true'); |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Validate for false|f|0|no|n. |
||
298 | * |
||
299 | * Normalizes to lower case |
||
300 | * |
||
301 | * Useful for 'if' rules |
||
302 | */ |
||
303 | public function rule_false($a) |
||
304 | { |
||
305 | // Changes PHP false to 0 |
||
306 | $a = strtolower($a); |
||
307 | |||
308 | $vals = array('false', 'f', 0, 'no', 'n'); |
||
309 | |||
310 | if (!in_array($a, $vals)) { |
||
311 | return $this->fail('Must be false'); |
||
312 | } |
||
313 | } |
||
314 | |||
315 | /* \section Value Test Rules: Date & Time */ |
||
316 | |||
317 | /** |
||
318 | * Validate for ISO date in format YYYY-MM-DD. |
||
319 | * |
||
320 | * Also checks for valid month and day values |
||
321 | */ |
||
322 | public function validate_iso_date($a) |
||
323 | { |
||
324 | $date = explode('-', $a); |
||
325 | |||
326 | $msg = 'Must be date in format: YYYY-MMM-DD'; |
||
327 | |||
328 | if (count($date) != 3) { |
||
329 | return $this->fail($msg); |
||
330 | } |
||
331 | |||
332 | if (strlen($date[0]) !== 4 || strlen($date[1]) !== 2 || strlen($date[2]) !== 2) { |
||
333 | return $this->fail($msg); |
||
334 | } |
||
335 | |||
336 | if (!@checkdate($date[1], $date[2], $date[0])) { |
||
337 | return $this->fail($msg); |
||
338 | } |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * Validate for ISO time. |
||
343 | * |
||
344 | * Requires a complete hour:minute:second time with |
||
345 | * the optional ':' separators. |
||
346 | * |
||
347 | * Checks for hh:mm[[:ss][.**..] where * = microseconds |
||
348 | * Also checks for valid # of hours, mins, secs |
||
349 | */ |
||
350 | public function rule_iso_time($a) |
||
351 | { |
||
352 | $pattern = "/^([0-9]{2}):([0-9]{2})(?::([0-9]{2})(?:(?:\.[0-9]{1,}))?)?$/"; |
||
353 | $msg = 'Must be a valid ISO time'; |
||
354 | |||
355 | if (preg_match($pattern, $a, $matches)) { |
||
356 | if ($matches[1] > 24) { |
||
357 | return $this->fail($msg); |
||
358 | } |
||
359 | |||
360 | if ($matches[2] > 59) { |
||
361 | return $this->fail($msg); |
||
362 | } |
||
363 | |||
364 | if (isset($matches[3]) && $matches[3] > 59) { |
||
365 | return $this->fail($msg); |
||
366 | } |
||
367 | } else { |
||
368 | return $this->fail($msg); |
||
369 | } |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Validate ISO datetime in the format:. |
||
374 | * |
||
375 | * YYYY-MM-DD hh:mm:ss with optional microseconds |
||
376 | */ |
||
377 | public function rule_iso_datetime($a) |
||
378 | { |
||
379 | $parts = explode(' ', $a); |
||
380 | $msg = 'Must be a valid ISO datetime'; |
||
381 | |||
382 | if (count($parts) != 2) { |
||
383 | return $this->fail($msg); |
||
384 | } |
||
385 | |||
386 | try { |
||
387 | $this->rule_iso_date($parts[0]); |
||
|
|||
388 | } catch (Exception $e) { |
||
389 | return $this->fail($msg); |
||
390 | } |
||
391 | try { |
||
392 | $this->rule_iso_time($parts[1]); |
||
393 | } catch (Exception $e) { |
||
394 | return $this->fail($msg); |
||
395 | } |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Checks any PHP datetime format: |
||
400 | * http://www.php.net/manual/en/datetime.formats.date.php. |
||
401 | */ |
||
402 | View Code Duplication | public function rule_before($a) |
|
403 | { |
||
404 | $time = $this->pullRule(); |
||
405 | if (strtotime($a) >= strtotime($time)) { |
||
406 | return $this->fail('Must be before {{arg1}}', $time); |
||
407 | } |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * Checks any PHP datetime format: |
||
412 | * http://www.php.net/manual/en/datetime.formats.date.php. |
||
413 | */ |
||
414 | View Code Duplication | public function rule_after($a) |
|
415 | { |
||
416 | $time = $this->pullRule(); |
||
417 | if (strtotime($a) <= strtotime($time)) { |
||
418 | return $this->fail('Must be after {{arg1}}', $time); |
||
419 | } |
||
420 | } |
||
421 | |||
422 | /* \section Value Test Rules: Postal, Email & Credit Card */ |
||
423 | |||
424 | public function rule_email($a) |
||
425 | { |
||
426 | if (!filter_var($a, FILTER_VALIDATE_EMAIL)) { |
||
427 | return $this->fail('Must be a valid email address'); |
||
428 | } |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * Checks for a 5 digit or extended US zip. |
||
433 | */ |
||
434 | public function rule_zip($a) |
||
435 | { |
||
436 | if (!preg_match('/^\d{5}(-\d{4})?$/', $a)) { |
||
437 | return $this->fail('Must be a valid ZIP code'); |
||
438 | } |
||
439 | } |
||
440 | |||
441 | /** |
||
442 | * Validate for credit card number. |
||
443 | * |
||
444 | * Uses the Luhn Mod 10 check |
||
445 | */ |
||
446 | public function rule_credit_card($a) |
||
447 | { |
||
448 | // Card formats keep changing and there is too high a risk |
||
449 | // of false negatives if we get clever. So we just check it |
||
450 | // with the Luhn Mod 10 formula |
||
451 | |||
452 | // Calculate the Luhn check number |
||
453 | |||
454 | $msg = 'Not a valid card number'; |
||
455 | |||
456 | $sum = 0; |
||
457 | $alt = false; |
||
458 | |||
459 | for ($i = strlen($a) - 1; $i >= 0; --$i) { |
||
460 | $n = substr($a, $i, 1); |
||
461 | if ($alt) { |
||
462 | //square n |
||
463 | $n *= 2; |
||
464 | if ($n > 9) { |
||
465 | //calculate remainder |
||
466 | $n = ($n % 10) + 1; |
||
467 | } |
||
468 | } |
||
469 | $sum += $n; |
||
470 | $alt = !$alt; |
||
471 | } |
||
472 | |||
473 | // If $sum divides exactly by 10 it's valid |
||
474 | |||
475 | if (!($sum % 10 == 0)) { |
||
476 | return $this->fail($msg); |
||
477 | } else { |
||
478 | // Luhn check seems to return true for any string of 0s |
||
479 | |||
480 | $stripped = str_replace('0', '', $a); |
||
481 | |||
482 | if (strlen($stripped) == 0) { |
||
483 | return $this->fail($msg); |
||
484 | } |
||
485 | } |
||
486 | } |
||
487 | |||
488 | /** |
||
489 | * Validate a card "expires end" date. |
||
490 | */ |
||
491 | public function rule_card_to_date($a) |
||
492 | { |
||
493 | $msg = 'Not a valid date'; |
||
494 | if (!$this->card_date_parser($a, 'to')) { |
||
495 | return $this->fail($msg); |
||
496 | } |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * Validate a card "valid from" date. |
||
501 | */ |
||
502 | public function rule_card_from_date($a) |
||
503 | { |
||
504 | $msg = 'Not a valid date'; |
||
505 | if (!$this->card_date_parser($a, 'from')) { |
||
506 | return $this->fail($msg); |
||
507 | } |
||
508 | } |
||
509 | |||
510 | /* \section Value Conversion Rules: General */ |
||
511 | |||
512 | /** |
||
513 | * Strips out the characters matched by the |
||
514 | * pattern in the argument. |
||
515 | * |
||
516 | * The pattern should include the pattern delimiter, eg: |
||
517 | * |
||
518 | * /my_pattern/ |
||
519 | */ |
||
520 | public function to_strip_regex($a) |
||
526 | |||
527 | /* \section Value Conversion Rules: Numeric */ |
||
528 | |||
529 | public function rule_to_int($a) |
||
530 | { |
||
531 | return (int) $a = preg_replace('/[^0-9]/', '', $a); |
||
532 | } |
||
533 | |||
534 | public function rule_to_number($a) |
||
535 | { |
||
536 | return (int) $a = preg_replace('/[^0-9\.]/', '', $a); |
||
537 | } |
||
538 | |||
539 | public function rule_to_float($a) |
||
540 | { |
||
541 | return (int) $a = preg_replace('/[^0-9\.]/', '', $a); |
||
542 | } |
||
543 | |||
544 | public function rule_to_digits_and_single_spaces($a) |
||
545 | { |
||
546 | $a = preg_replace("/[^\d ]/", '', $a); |
||
547 | |||
548 | return $this->rule_strip_extra_space($a); |
||
549 | } |
||
550 | |||
551 | /* \section Value Conversion Rules: String */ |
||
552 | |||
553 | public function rule_to_trim($a) |
||
557 | |||
558 | public function rule_to_ltrim($a) |
||
562 | |||
563 | public function rule_to_rtrim($a) |
||
567 | |||
568 | /** |
||
569 | * Strip out all white space. |
||
570 | */ |
||
571 | public function rule_to_strip_space($a) |
||
572 | { |
||
573 | return preg_replace("/\s/", '', $a); |
||
574 | } |
||
575 | |||
576 | /** |
||
577 | * Reduce sequential whitespaces to a single space. |
||
578 | */ |
||
579 | public function rule_to_strip_extra_space($a) |
||
580 | { |
||
581 | return $this->strip_excess_whitespace($a); |
||
582 | } |
||
583 | |||
584 | /** |
||
585 | * Strip to A-Za-z0-9. |
||
586 | */ |
||
587 | public function rule_to_alpha($a) |
||
588 | { |
||
589 | return preg_replace('/[^a-zA-Z]/', '', $a); |
||
590 | } |
||
591 | |||
592 | /** |
||
593 | * Test for unicode letter characters. |
||
594 | * |
||
595 | * Should work even is PCRE compiled |
||
596 | * without "--enable-unicode-properties". |
||
597 | */ |
||
598 | public function rule_to_alpha_unicode($a) |
||
599 | { |
||
600 | return preg_replace('/(*UTF8)[^\p{L}]/u', '', $a); |
||
601 | } |
||
602 | |||
603 | public function rule_to_alpha_num($a) |
||
607 | |||
608 | public function rule_to_lower($a) |
||
612 | |||
613 | public function rule_to_upper($a) |
||
614 | { |
||
615 | return $this->mb_str_to_upper($a); |
||
616 | } |
||
617 | |||
618 | public function rule_to_upper_words($a) |
||
622 | |||
623 | /* \section Value Conversion Rules: Alphanumeric */ |
||
624 | |||
625 | /** |
||
626 | * Strip to unicode letter characters and 0-9. |
||
627 | * |
||
628 | * Should work even is PCRE compiled |
||
629 | * without "--enable-unicode-properties". |
||
630 | */ |
||
631 | public function rule_to_alpha_num_unicode($a) |
||
632 | { |
||
633 | return preg_replace('/(*UTF8)[^\p{L}0-9]/u', '', $a); |
||
634 | } |
||
635 | |||
636 | public function rule_to_alpha_num_dash($a) |
||
637 | { |
||
638 | return preg_replace('/[^a-zA-Z0-9_-]/', '', $a); |
||
639 | } |
||
640 | |||
641 | /** |
||
642 | * Truncates, and adds '...' to |
||
643 | * end of string. |
||
644 | * |
||
645 | * Requires parameter 'length' |
||
646 | */ |
||
647 | public function rule_to_truncate($a) |
||
648 | { |
||
649 | $len = $this->pullRule(); |
||
650 | |||
651 | return $this->mb_truncate($a, $len); |
||
652 | } |
||
653 | |||
654 | /** |
||
655 | * Requires parameters: length, |
||
656 | * custom string to end of string. |
||
657 | */ |
||
658 | public function rule_to_truncate_custom($a) |
||
659 | { |
||
660 | $len = $this->pullRule(); |
||
661 | $append = $this->pullRule(); |
||
662 | |||
663 | return $this->mb_truncate($a, $len, $append); |
||
664 | } |
||
665 | |||
666 | /** |
||
667 | * Strip to unicode letter characters and 0-9, -, _. |
||
668 | * |
||
669 | * Requires PCRE compiled with "--enable-unicode-properties". |
||
670 | * Most distros these days will offer this |
||
671 | */ |
||
672 | public function rule_to_alpha_num_dash_unicode($a) |
||
673 | { |
||
674 | return preg_replace('/(*UTF8)[^\p{L}0-9_-]/u', '', $a); |
||
675 | } |
||
676 | |||
677 | /* \section Value Conversion Rules: Date & Time */ |
||
678 | |||
679 | public function rule_to_iso_date($a) |
||
680 | { |
||
681 | $a = preg_replace("/[^T0-9\/\-\(\): ]/", '', $a); |
||
682 | |||
683 | return $this->rule_iso_date($a); |
||
684 | } |
||
685 | |||
686 | /* \section Value Conversion Rules: Data Sanitization */ |
||
687 | |||
688 | public function rule_to_strip_tags($a) |
||
689 | { |
||
690 | return strip_tags($a); |
||
691 | } |
||
692 | |||
693 | public function rule_to_quote_meta($a) |
||
694 | { |
||
695 | return quotemeta($a); |
||
696 | } |
||
697 | |||
698 | public function rule_to_add_slashes($a) |
||
702 | |||
703 | public function rule_to_strip_slashes($a) |
||
704 | { |
||
705 | return stripslashes($a); |
||
706 | } |
||
707 | |||
708 | /** |
||
709 | * Strip out attack characters from names & addresses |
||
710 | * and other strings where they have no place. |
||
711 | * |
||
712 | * Strips: * ^ <> ? ! () | / \ [] + = % ; ~ ` |
||
713 | */ |
||
714 | public function rule_to_strip_nasties($a) |
||
715 | { |
||
716 | return preg_replace("|[\*\^<?>!\"\(\)\|\\\\/\[\]\+=#%;~`]|", '', $a); |
||
717 | } |
||
718 | |||
719 | /* \section Value Conversion Rules: Phone, Name, Address */ |
||
720 | |||
721 | /** |
||
722 | * Normalizes, then validates. |
||
723 | */ |
||
724 | public function rule_to_zip($a) |
||
725 | { |
||
726 | // Change 12345 1234 to 12345-1234 |
||
727 | $a = preg_replace('/[^0-9-]/', '', $a); |
||
728 | $this->rule_zip($a); |
||
729 | } |
||
730 | |||
731 | // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
732 | // PERSONAL NAMES (European style) |
||
733 | // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
734 | |||
735 | /** |
||
736 | * Useful for cleaning up input where you don't want to present |
||
737 | * an error to the user - eg a checkout where ease of use is |
||
738 | * more important than accuracy. Can save a lot of re-keying. |
||
739 | * |
||
740 | * Some libraries don't clean up names if already in mixed case. |
||
741 | * Experience shows this isn't very useful, as many users |
||
742 | * will type mAry, JOSepH etc. |
||
743 | * |
||
744 | * Can only be a best guess - but much better than nothing: |
||
745 | * has been in production for years without any negative |
||
746 | * customer feedback. |
||
747 | * |
||
748 | * Set $is_capitalise_prefix if you want prefixes in upper: |
||
749 | * Von Trapp vs von Trapp. You would do this to format |
||
750 | * a last name for use in salutations: |
||
751 | * |
||
752 | * Dear Mr Von Trapp |
||
753 | * |
||
754 | * Can handle full names, 1st only, middle only, last only. |
||
755 | * |
||
756 | * Cleans up extra whitespace. |
||
757 | */ |
||
758 | public function rule_to_name($name, $is_capitalise_prefix = false) |
||
1041 | |||
1042 | /* \section Helper Functions */ |
||
1043 | |||
1044 | /** |
||
1045 | * Reduce sequential whitespaces to a single space. |
||
1046 | */ |
||
1047 | protected function strip_excess_whitespace($a) |
||
1051 | |||
1052 | /** |
||
1053 | * Helper for validating card to and from dates. |
||
1054 | */ |
||
1055 | protected function card_date_parser($a, $type) |
||
1056 | { |
||
1057 | // Strip out any slash |
||
1058 | |||
1059 | $date = str_replace('/', '', $a); |
||
1060 | |||
1061 | // Check that we have 4 digits |
||
1062 | |||
1063 | if (!preg_match('|^[0-9]{4}$|', $date)) { |
||
1064 | return false; |
||
1065 | } |
||
1066 | |||
1067 | $month = substr($date, 0, 2); |
||
1068 | $year = substr($date, 2, 2); |
||
1069 | |||
1070 | // Check month is logical |
||
1071 | |||
1072 | if ($month > 12) { |
||
1073 | return false; |
||
1074 | } |
||
1075 | |||
1076 | $parts = array(date('Y'), date('m'), 1); |
||
1077 | $now_datetime = new DateTime(implode('-', $parts)); |
||
1078 | |||
1079 | $parts = array('20'.$year, $month, '1'); |
||
1080 | $card_datetime = new DateTime(implode('-', $parts)); |
||
1081 | |||
1082 | $interval = $now_datetime->diff($card_datetime); |
||
1083 | $days = $interval->format('%R%a days'); |
||
1084 | |||
1085 | if ($type == 'from') { |
||
1086 | // Check from date is older or equal to current month |
||
1087 | |||
1088 | if ($days <= 0 && $days > -3650) { |
||
1089 | return true; |
||
1090 | } else { |
||
1091 | return false; |
||
1092 | } |
||
1093 | } elseif ($type == 'to') { |
||
1094 | // Check to date is newer or equal to current month |
||
1095 | |||
1096 | if ($days >= 0 && $days < 3650) { |
||
1097 | return true; |
||
1098 | } else { |
||
1099 | return false; |
||
1100 | } |
||
1101 | } else { |
||
1102 | $msg = "Bad date type '$type' in card-date validation"; |
||
1103 | throw new Error($msg); |
||
1104 | } |
||
1105 | } |
||
1106 | |||
1107 | /** |
||
1108 | * Explode and trim a comma-delmited list |
||
1109 | * for the in and not_in rules. |
||
1110 | */ |
||
1111 | protected function prep_in_vals($a) |
||
1124 | } |
||
1125 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.