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