util.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace common\helpers\IDValidator;
  3. /**
  4. * 工具
  5. */
  6. class util {
  7. private static $GB2260;
  8. private static $instance;
  9. function __construct() {
  10. if (!class_exists("GB2260")){
  11. include_once 'GB2260.php';
  12. }
  13. self::$GB2260 = GB2260::getGB2260 ();
  14. }
  15. public static function getInstance() {
  16. if (is_null ( self::$instance )) {
  17. self::$instance = new util ();
  18. }
  19. return self::$instance;
  20. }
  21. public function checkArg($id) {
  22. $id = strtoupper ( $id );
  23. $code = null;
  24. if (strlen ( $id ) === 18) {
  25. // 18位
  26. $code = array (
  27. "body" => substr ( $id, 0, 17 ),
  28. "checkBit" => substr ( $id, - 1 ),
  29. "type" => 18
  30. );
  31. } else if (strlen($id) === 15) {
  32. // 15位
  33. $code = array (
  34. "body" => $id,
  35. "type" => 15
  36. );
  37. } else {
  38. return false;
  39. }
  40. return $code;
  41. }
  42. // 地址码检查
  43. function checkAddr($addr) {
  44. $addrInfo = $this->getAddrInfo ( $addr );
  45. return ($addrInfo === false ? false : true);
  46. }
  47. // 取得地址码信息
  48. function getAddrInfo($addr) {
  49. // 查询GB/T2260,没有引入GB2260时略过
  50. if (self::$GB2260 === null) {
  51. return $addr;
  52. }
  53. if (! isset ( self::$GB2260 [$addr] )) {
  54. // 考虑标准不全的情况,搜索不到时向上搜索
  55. $tmpAddr = substr ( $addr, 0, 4 ) . '00';
  56. if (! isset ( self::$GB2260 [$tmpAddr] )) {
  57. $tmpAddr = substr ( $addr, 0, 2 ) . '0000';
  58. if (! isset ( self::$GB2260 [$tmpAddr] )) {
  59. return false;
  60. } else {
  61. return self::$GB2260 [$tmpAddr] . '未知地区';
  62. }
  63. } else {
  64. return self::$GB2260 [$tmpAddr] . '未知地区';
  65. }
  66. } else {
  67. return self::$GB2260 [$addr];
  68. }
  69. }
  70. // 生日码检查
  71. function checkBirth($birth) {
  72. $year = $month = $day = 0;
  73. if (strlen ( $birth ) == 8) {
  74. $year = intval ( substr ( $birth, 0, 4 ) );
  75. $month = intval ( substr ( $birth, 4, 2 ) );
  76. $day = intval ( substr ( $birth, - 2 ) );
  77. } else if (strlen($birth) == 6) {
  78. $year = intval ( '19' + substr ( $birth, 0, 2 ) );
  79. $month = intval ( substr ( $birth, 2, 2 ) );
  80. $day = intval ( substr($birth, - 2 ) );
  81. } else {
  82. return false;
  83. }
  84. // TODO 是否需要判断年份
  85. /*
  86. * if( year<1800 ){ return false; }
  87. */
  88. // TODO 按月份检测
  89. if ($month > 12 || $month === 0 || $day > 31 || $day === 0) {
  90. return false;
  91. }
  92. return true;
  93. }
  94. // 顺序码检查
  95. function checkOrder($order) {
  96. // 暂无需检测
  97. return true;
  98. }
  99. // 加权
  100. function weight($t) {
  101. return pow ( 2, $t - 1 ) % 11;
  102. }
  103. // 随机整数
  104. function rand($max, $min = 1) {
  105. //return round ( rand(0, 1) * ($max - $min) ) + $min;
  106. return rand($min, $max);
  107. }
  108. // 数字补位
  109. function str_pad($str, $len = 2, $chr = '0', $right = false) {
  110. $str = strval($str);
  111. if (strlen ( $str ) >= $len) {
  112. return $str;
  113. } else {
  114. for($i = 0, $j = $len - strlen ( $str ); $i < $j; $i ++) {
  115. if ($right) {
  116. $str = $str . $chr;
  117. } else {
  118. $str = $chr . $str;
  119. }
  120. }
  121. return $str;
  122. }
  123. }
  124. }