1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace Wei\Validator;
10:
11: 12: 13: 14: 15: 16:
17: class IdCardTw extends BaseValidator
18: {
19: protected $invalidMessage = '%name% must be valid Taiwan identity card';
20:
21: protected $negativeMessage = '%name% must not be valid Taiwan identity card';
22:
23: protected $map = array(
24: 'A' => 10,
25: 'B' => 11,
26: 'C' => 12,
27: 'D' => 13,
28: 'E' => 14,
29: 'F' => 15,
30: 'G' => 16,
31: 'H' => 17,
32: 'I' => 34,
33: 'J' => 18,
34: 'K' => 19,
35: 'M' => 21,
36: 'N' => 22,
37: 'O' => 35,
38: 'P' => 23,
39: 'Q' => 24,
40: 'T' => 27,
41: 'U' => 28,
42: 'V' => 29,
43: 'W' => 32,
44: 'X' => 30,
45: 'Z' => 33,
46: 'L' => 20,
47: 'R' => 25,
48: 'S' => 26,
49: 'Y' => 31
50: );
51:
52: 53: 54:
55: protected function doValidate($input)
56: {
57: if (!$this->isString($input)) {
58: $this->addError('notString');
59: return false;
60: }
61:
62: if (10 != strlen($input)) {
63: $this->addError('invalid');
64: return false;
65: }
66:
67: $input = strtoupper($input);
68:
69:
70: $first = ord($input[0]);
71: if ($first < 65 || $first > 90) {
72: $this->addError('invalid');
73: return false;
74: }
75:
76:
77: if ($input[1] != '1' && $input[1] != '2') {
78: $this->addError('invalid');
79: return false;
80: }
81:
82: list($x1, $x2) = str_split((string)$this->map[$input[0]]);
83: $sum = $x1 + 9 * $x2;
84: for ($i = 1, $j = 8; $i < 9; $i++, $j--) {
85: $sum += $input[$i] * $j;
86: }
87: $sum += $input[9];
88:
89: if (0 !== $sum % 10) {
90: $this->addError('invalid');
91: return false;
92: }
93:
94: return true;
95: }
96: }
97: