Advertisement
Stefeno

Dati da Codice Fiscale

Sep 27th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.49 KB | None | 0 0
  1. /*************************************************************************
  2. Semplice esempio di  classe PHP che ricava alcuni dati dal Codice Fiscale Italiano.
  3. Necessita la tabella contenente i codici e comuni italiani, qui prevista in un DB MySql.
  4.  
  5. Esempio di uso:
  6.  
  7.  
  8.  
  9. $daticf = new retDatiDaCF(); // istanzia la classe
  10. $daticf->setCF($cf); // passa il codice fiscale alla classe
  11. $daticf->calcola(); // estrae i dati a partire dalla stringa CF
  12.  
  13.  
  14.  
  15. if ($daticf->hasError()) {
  16.     print "ERRORE: " . $daticf->getError() . "\n";
  17.     exit(1);
  18. } else {
  19.     echo " Data di nascita: " .$daticf->getDataNascitaCF();
  20.     echo " Comune: " .$daticf->getComuneCF();
  21.     echo " Sesso: " .$daticf->getSessoCF();
  22.  
  23. }
  24.  
  25.  
  26.  
  27. ***************************************************************************/
  28.  
  29.  
  30.  
  31. class retDatiDaCF {
  32.  
  33.     const ERR_LEN_CF  = 'Errore nella lunghezza del Codice Fiscale. Deve essere di 16 caratteri invece รจ di ';
  34.     const ERR_DB_CONN  = 'Errore nella connessione al database dei codici catastali';
  35.     const ERR_COD_NTFND = 'Il codice catastale non รจ corretto : ';
  36.     /**
  37.      * Array per il calcolo del mese
  38.      *
  39.      */
  40.     protected $_mesi = array(
  41.         1  => 'A',  2 => 'B',  3 => 'C',  4 => 'D',  5 => 'E',  
  42.         6  => 'H',  7 => 'L',  8 => 'M',  9 => 'P', 10 => 'R',
  43.         11 => 'S', 12 => 'T'
  44.     );
  45.     /**
  46.      * Stringa di errore
  47.      */
  48.     protected $_error = null;
  49.     protected $_cognome = "";
  50.     protected $_nome = "";
  51.     protected $_anno = "";
  52.     protected $_mese = "";
  53.     protected $_giorno = "";
  54.     protected $_sesso = "";
  55.     protected $_comune = "";
  56.     protected $_cc = "";
  57.  
  58.     protected $_CF = "";   // variabile interna del codice fiscale
  59.    
  60.  
  61.     /**
  62.      * Controlla la lunghezza del CF. Se ok memorizza.
  63.      */
  64.     public function setCF($cf) {
  65.             if (strlen($cf) != 16) {  
  66.                         $this->_setError(self::ERR_LEN_CF. (string)strlen($cf));
  67.                         return false;
  68.         }
  69.         $this->_CF = $cf;
  70.        return true;
  71.     }  
  72.    
  73.  
  74.   /**
  75.      * Ritorna alcuni dati estratti dal codice fiscale
  76.      *
  77.      */
  78.     public function calcola() {
  79.         $this->_cognome = substr($this->_CF, 0, 3);  
  80.         $this->_nome = substr($this->_CF, 3, 3);  
  81.         $this->_anno = substr($this->_CF, 6, 2);
  82.         $this->_mese = array_search(strtoupper(substr($this->_CF, 8, 1)), $this->_mesi);
  83.         $this->_giorno = substr($this->_CF, 9, 2);
  84.         if  ((int)$this->_giorno>40) {
  85.             $this->_sesso="F";
  86.             $this->_giorno =  (int)$this->giorno - 40;
  87.         } else {
  88.             $this->_sesso="M";
  89.         }
  90.        
  91.         $this->_comune = $this->_cercaComune(substr($this->_CF, 11, 4));
  92.        
  93.         $this->_cc = substr($this->_CF, 15, 1);
  94.        
  95.        
  96.         if ($this->hasError()) {
  97.             return false;
  98.         }
  99.  
  100.         // return true;
  101.     }
  102.  
  103.    
  104.     public function getComuneCF() {
  105.         return $this->_comune;
  106.     }
  107.     public function getSessoCF() {
  108.         return $this->_sesso;
  109.     }
  110.     public function getDataNascitaCF() {
  111.         return $this->_giorno."/".$this->_mese."/".$this->_anno;
  112.     }
  113.  
  114.  /**
  115.      * Imposta il messaggio di errore
  116.      */
  117.     protected function _setError($string) {
  118.         $this->_error = $string;
  119.     }    
  120.  
  121.     /**
  122.      * Verifica la presenza di un errore.
  123.      * Ritorna TRUE se presente, FALSE altrimenti.
  124.      */
  125.     public function hasError() {
  126.         return !is_null($this->_error);
  127.     }  
  128.    
  129.     /**
  130.      * Ritorna la stringa di errore
  131.      */
  132.     public function getError() {
  133.         return $this->_error;
  134.     }
  135.    
  136.         /**
  137.      * Ritorna il codice catastale del comune richiesto
  138.      */
  139.     protected function _cercaComune($codice) {
  140.  
  141.         $servername = "localhost";
  142.         $username = "username";
  143.         $password = "password";
  144.         $dbname =   "tuodb";
  145.                
  146.         $comune="";
  147.  
  148.         // Create connection
  149.         $conn = new mysqli($servername, $username, $password, $dbname);
  150.         // Check connection
  151.         if ($conn->connect_error) {
  152.                     $this->_setError(self::ERR_DB_CONN);
  153.                      return false;
  154.         }
  155.                
  156.         $sql = "SELECT comune  FROM view_comuni_provincie WHERE codice = '" . strtoupper($codice) . "'";
  157.  
  158.                 $result = $conn->query($sql);
  159.                
  160.  
  161.                 if ($result->num_rows > 0) {
  162.             $row = $result->fetch_assoc();
  163.             $comune=$row['comune'];
  164.         } else {
  165.             $comune="";
  166.         }
  167.        
  168.         $conn->close();
  169.        
  170.         if (is_null($comune) || empty($comune)) {
  171.             $this->_setError(self::ERR_COD_NTFND.$codice);
  172.             return false;
  173.         }
  174.                
  175.         return $comune;
  176.  
  177.     }
  178.    
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement