Advertisement
Foxxything

Untitled

Oct 3rd, 2022
1,390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.27 KB | None | 0 0
  1. <?php
  2.   namespace Foxx\ComputerScience\U1;
  3.  
  4.   final class SinCheck {
  5.    
  6.     public function __construct() {
  7.       echo "Welcome to the sin checker!\n";
  8.       $userInput = readline("Enter your 9 digit SIN: ");
  9.  
  10.       $valid = $this->validateSIN($userInput);
  11.  
  12.       if ($valid) {
  13.         echo "Your SIN is valid.";
  14.       } else {
  15.         echo "Your SIN is invalid.";
  16.       }
  17.     }
  18.  
  19.     /**
  20.      * The luhn algorithm is used to check the validity of a number.
  21.      *
  22.      * @param string $str
  23.      * @return bool
  24.      */
  25.     function luhn(string $str): bool {
  26.       $odd = !strlen($str)%2;
  27.       $sum = 0;
  28.       for($i=0;$i<strlen($str);++$i){
  29.         $n=0+$str[$i];
  30.         $odd=!$odd;
  31.         if($odd){
  32.           $sum+=$n;
  33.         }else{
  34.           $x=2*$n;
  35.           $sum+=$x>9?$x-9:$x;
  36.         }
  37.       }
  38.       return(($sum%10)==0);
  39.     }
  40.  
  41.     /**
  42.      * Checks if the given string is a valid SIN.
  43.      *
  44.      * @param string $str
  45.      * @return bool
  46.      */
  47.     function validateSIN(string $sin): bool {
  48.       $sin = preg_replace('/[^0-9]/','',$sin);
  49.       if(strlen($sin) == 9){
  50.         if($sin[0] == '0' || $sin[0] == '8'){
  51.           return false;
  52.         }else{
  53.           return $this->luhn($sin);
  54.         }
  55.       }else{
  56.         return false;
  57.       }
  58.     }
  59.  
  60.     /**
  61.      * Generates a random SIN.
  62.      *
  63.      * @param string $separator
  64.      * @return string
  65.      */
  66.     function generateSIN(string $separator = ' '){
  67.       $validPrefix = array(1,2,3,4,5,6,7,9);
  68.       $sin = array_rand($validPrefix,1);
  69.       $length = 9;
  70.  
  71.       while(strlen($sin) < ($length - 1)){
  72.         $sin .= rand(0,9);
  73.       }
  74.  
  75.       $sum = 0;
  76.       $pos = 0;
  77.  
  78.       $reversedSIN = strrev( $sin );
  79.  
  80.       while($pos < $length - 1){
  81.         $odd = $reversedSIN[ $pos ] * 2;
  82.         if($odd > 9){
  83.           $odd -= 9;
  84.         }
  85.  
  86.         $sum += $odd;
  87.  
  88.         if($pos != ($length - 2)){
  89.           $sum += $reversedSIN[ $pos +1 ];
  90.         }
  91.         $pos += 2;
  92.       }
  93.  
  94.       $checkdigit = (( floor($sum/10) + 1) * 10 - $sum) % 10;
  95.       $sin .= $checkdigit;
  96.  
  97.       $sin1 = substr($sin,0,3);
  98.       $sin2 = substr($sin,3,3);
  99.       $sin3 = substr($sin,6,3);
  100.  
  101.       return $sin1.$separator.$sin2.$separator.$sin3;
  102.     }
  103.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement