Advertisement
Guest User

Untitled

a guest
Dec 25th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.11 KB | None | 0 0
  1. <?php
  2. class SwedishSocialSecurityNumber {
  3.  
  4.     private $_socialSecurityNumber;
  5.     private $_year;
  6.     private $_month;
  7.     private $_day;
  8.     private $_code;
  9.     private $_isOver100;
  10.  
  11.     public function __construct($socialSecurityNumber) {
  12.         $this->_socialSecurityNumber = $socialSecurityNumber;
  13.         $this->_isOver100 = false;
  14.  
  15.         if (!$this->hasTheRightPattern()) {
  16.             throw new \Exception("The SSN has to have the format YYNNDD-XXXX or YYMMDD+XXXX");
  17.         }
  18.  
  19.         $this->_year = substr($this->_socialSecurityNumber, 0, 2);
  20.         $this->_month = substr($this->_socialSecurityNumber, 2, 2);
  21.         $this->_day = substr($this->_socialSecurityNumber, 4, 2);
  22.  
  23.         if (substr($this->_socialSecurityNumber, 6, 1) == "+") {
  24.             $this->_isOver100 = true;
  25.         }
  26.  
  27.         $this->_code = substr($this->_socialSecurityNumber, 7, 4);
  28.  
  29.         if (!$this->isDateValid()) {
  30.             throw new \Exception("The date is invalid or is fictional");
  31.         }
  32.  
  33.         if ($this->getCheckSum() != substr($this->_socialSecurityNumber, 10, 11)) {
  34.             throw new \Exception("The control number is not valid");
  35.         }
  36.  
  37.     }
  38.     public function getDay() {return $this->_day;}
  39.     public function getMonth() {return $this->_month;}
  40.     public function getYear() {return $this->_year;}
  41.     public function getCode() {return $this->_code;}
  42.     public function isOver100() {return $this->_isOver100;}
  43.     public function getSocialSecurityNumber() {return $this->_socialSecurityNumber;}
  44.  
  45.     public function isFemale() {
  46.         $num = intval(substr($this->_socialSecurityNumber, 9, 1));
  47.         if (($num % 2) == 0) {
  48.             return true;
  49.         }
  50.  
  51.         return false;
  52.     }
  53.  
  54.     public function isMale() {
  55.         return !$this->isFemale();
  56.     }
  57.  
  58.     public function getCheckSum() {
  59.         $temp = null;
  60.         $variegated = 2;
  61.         $list = [];
  62.  
  63.         for ($i = 0; $i < strlen($this->_socialSecurityNumber) - 1; $i++) {
  64.             if ($i == 6) {
  65.                 continue;
  66.             }
  67.  
  68.             $temp = $this->_socialSecurityNumber[$i];
  69.             $temp = $temp * $variegated;
  70.  
  71.             $tempStr = strval($temp);
  72.             for ($j = 0; $j < strlen($tempStr); $j++) {
  73.                 $list[] = intval($tempStr[$j]);
  74.             }
  75.  
  76.             $variegated = $variegated == 2 ? 1 : 2;
  77.         }
  78.  
  79.         return (10 - (array_sum($list) % 10));
  80.     }
  81.  
  82.     public function getFullYear() {
  83.         $currentYear = date("Y");
  84.         $retValue = null;
  85.  
  86.         if ($this->_year < 10) {
  87.             $retValue = substr($currentYear, 0, 2) . "0" . $this->_year;
  88.         } else {
  89.             $retValue = substr($currentYear, 0, 2) . $this->_year;
  90.         }
  91.  
  92.         if ($this->_isOver100) {
  93.             return $retValue - 100;
  94.         }
  95.  
  96.         if ($retValue > $currentYear) {
  97.             return $retValue - 100;
  98.         }
  99.  
  100.         return $retValue;
  101.  
  102.     }
  103.  
  104.     private function isDateValid() {
  105.         $date = strtotime("{$this->getFullYear()}-{$this->_month}-{$this->_day}");
  106.         $year = date('Y', $date);
  107.         $month = date('m', $date);
  108.         $day = date('d', $date);
  109.  
  110.         $currentDate = strtotime(date("Y-m-d"));
  111.  
  112.         if ($date > $currentDate) {
  113.             return false;
  114.         }
  115.  
  116.         if ($this->getFullYear() == $year && $this->_month == $month && $this->_day == $day) {
  117.             return true;
  118.         }
  119.  
  120.         return false;
  121.     }
  122.  
  123.     private function hasTheRightPattern() {
  124.         if (!preg_match("/^[0-9]{2}[0-1][0-9][0-3][0-9][-|+][0-9]{4}$/", $this->_socialSecurityNumber)) {
  125.             return false;
  126.         }
  127.  
  128.         return true;
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement