1: <?php
2:
3: /**
4: * Jyxo PHP Library
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file license.txt.
10: * It is also available through the world-wide-web at this URL:
11: * https://github.com/jyxo/php/blob/master/license.txt
12: */
13:
14: /**
15: * Validates a birth number.
16: *
17: * Taken from David Grudl's http://latrine.dgx.cz/jak-overit-platne-ic-a-rodne-cislo
18: *
19: * @category Jyxo
20: * @package Jyxo_Input
21: * @subpackage Validator
22: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
23: * @license https://github.com/jyxo/php/blob/master/license.txt
24: * @author Jaroslav HanslĂk
25: */
26: class Jyxo_Input_Validator_IsBirthNumber extends Jyxo_Input_Validator_AbstractValidator
27: {
28: /**
29: * Validates a value.
30: *
31: * @param mixed $value Input value
32: * @return boolean
33: */
34: public function isValid($value)
35: {
36: $value = (string) $value;
37:
38: if (!preg_match('~^(\d{2})(\d{2})(\d{2})[ /]*(\d{3})(\d?)$~', trim($value), $matches)) {
39: return false;
40: }
41:
42: list(, $year, $month, $day, $ext, $control) = $matches;
43:
44: // Until 1954 9 numbers were used; can not check
45: if ('' === $control) {
46: if ($year >= 54) {
47: return false;
48: } else {
49: return true;
50: }
51: }
52:
53: // Control number
54: $mod = ($year . $month . $day . $ext) % 11;
55: // Exception for ca 1000 numbers; no such numbers since 1985
56: if (10 === $mod) {
57: $mod = 0;
58: }
59: if ((int) $control !== $mod) {
60: return false;
61: }
62:
63: // Date check
64: $year += $year < 54 ? 2000 : 1900;
65:
66: // 20, 50 or 70 can be added to month number
67: if (($month > 70) && ($year > 2003)) {
68: // Females since 2004 if all 4-digit extension combinations were used that day
69: $month -= 70;
70: } elseif ($month > 50) {
71: // Females
72: $month -= 50;
73: } elseif (($month > 20) && ($year > 2003)) {
74: // Males since 2004 if all 4-digit extension combinations were used that day
75: $month -= 20;
76: }
77:
78: // Date check
79: if (!checkdate($month, $day, $year)) {
80: return false;
81: }
82:
83: // Ok
84: return true;
85: }
86: }
87: