Overview

Namespaces

  • None
  • Wei
    • Validator

Classes

  • Wei\Validator\All
  • Wei\Validator\AllOf
  • Wei\Validator\Alnum
  • Wei\Validator\Alpha
  • Wei\Validator\BaseValidator
  • Wei\Validator\Between
  • Wei\Validator\Blank
  • Wei\Validator\Callback
  • Wei\Validator\CharLength
  • Wei\Validator\Chinese
  • Wei\Validator\Color
  • Wei\Validator\Contains
  • Wei\Validator\CreditCard
  • Wei\Validator\Date
  • Wei\Validator\DateTime
  • Wei\Validator\Decimal
  • Wei\Validator\Digit
  • Wei\Validator\Dir
  • Wei\Validator\DivisibleBy
  • Wei\Validator\DoubleByte
  • Wei\Validator\Email
  • Wei\Validator\EndsWith
  • Wei\Validator\EqualTo
  • Wei\Validator\Exists
  • Wei\Validator\FieldExists
  • Wei\Validator\File
  • Wei\Validator\GreaterThan
  • Wei\Validator\GreaterThanOrEqual
  • Wei\Validator\IdCardCn
  • Wei\Validator\IdCardHk
  • Wei\Validator\IdCardMo
  • Wei\Validator\IdCardTw
  • Wei\Validator\IdenticalTo
  • Wei\Validator\Image
  • Wei\Validator\In
  • Wei\Validator\Ip
  • Wei\Validator\Length
  • Wei\Validator\LessThan
  • Wei\Validator\LessThanOrEqual
  • Wei\Validator\Lowercase
  • Wei\Validator\Luhn
  • Wei\Validator\MaxLength
  • Wei\Validator\MinLength
  • Wei\Validator\MobileCn
  • Wei\Validator\NaturalNumber
  • Wei\Validator\NoneOf
  • Wei\Validator\Null
  • Wei\Validator\Number
  • Wei\Validator\OneOf
  • Wei\Validator\Password
  • Wei\Validator\Phone
  • Wei\Validator\PhoneCn
  • Wei\Validator\PlateNumberCn
  • Wei\Validator\PositiveInteger
  • Wei\Validator\PostcodeCn
  • Wei\Validator\Present
  • Wei\Validator\QQ
  • Wei\Validator\RecordExists
  • Wei\Validator\Regex
  • Wei\Validator\Required
  • Wei\Validator\SomeOf
  • Wei\Validator\StartsWith
  • Wei\Validator\Time
  • Wei\Validator\Tld
  • Wei\Validator\Type
  • Wei\Validator\Uppercase
  • Wei\Validator\Url
  • Wei\Validator\Uuid
  • Overview
  • Namespace
  • Function
 1: <?php
 2: /**
 3:  * Wei Framework
 4:  *
 5:  * @copyright   Copyright (c) 2008-2015 Twin Huang
 6:  * @license     http://opensource.org/licenses/mit-license.php MIT License
 7:  */
 8: 
 9: namespace Wei\Validator;
10: 
11: /**
12:  * Check if the input is valid Chinese identity card
13:  *
14:  * @author      Twin Huang <twinhuang@qq.com>
15:  */
16: class IdCardCn extends BaseValidator
17: {
18:     protected $invalidMessage = '%name% must be valid Chinese identity card';
19: 
20:     protected $negativeMessage = '%name% must not be valid Chinese identity card';
21: 
22:     /**
23:      * {@inheritdoc}
24:      */
25:     protected function doValidate($input)
26:     {
27:         if (!$this->isString($input)) {
28:             $this->addError('notString');
29:             return false;
30:         }
31: 
32:         $len = strlen($input);
33:         if ($len != 15 && $len != 18) {
34:             $this->addError('invalid');
35:             return false;
36:         }
37: 
38:         // Upgrade to 18-digit
39:         if ($len == 15) {
40:             $input = substr($input, 0, 6) . '19' . substr($input, 6);
41:         }
42: 
43:         // The 1 to 17-digit must be digit
44:         if (!preg_match('/^([0-9]+)$/', substr($input, 0, -1))) {
45:             $this->addError('invalid');
46:             return false;
47:         }
48: 
49:         // Verify date of birth
50:         $month  = substr($input, 10, 2);
51:         $day    = substr($input, 12, 2);
52:         $year   = substr($input, 6, 4);
53:         if (!checkdate($month, $day, $year)) {
54:             $this->addError('invalid');
55:             return false;
56:         }
57: 
58:         // Verify checksum
59:         if (isset($input[17])) {
60:             $checksum = $this->calcChecksum($input);
61:             if (strtoupper($input[17]) !== $checksum) {
62:                 $this->addError('invalid');
63:                 return false;
64:             }
65:         }
66: 
67:         return true;
68:     }
69: 
70:     /**
71:       * Calculate the final digit of id card
72:       *
73:       * @param string $input The 17 or 18-digit code
74:       * @return string
75:       */
76:     public function calcChecksum($input)
77:     {
78:         $wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
79:         $sum = 0;
80: 
81:         for ($i = 16; $i >= 0; $i--) {
82:             $sum += $input[$i] * $wi[$i];
83:         }
84: 
85:         $checksum = (12 - $sum % 11) % 11;
86: 
87:         return $checksum == 10 ? 'X' : (string)$checksum;
88:     }
89: }
90: 
Wei Framework API documentation generated by ApiGen