Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************************************************************************
- Semplice esempio di classe PHP che ricava alcuni dati dal Codice Fiscale Italiano.
- Necessita la tabella contenente i codici e comuni italiani, qui prevista in un DB MySql.
- Esempio di uso:
- $daticf = new retDatiDaCF(); // istanzia la classe
- $daticf->setCF($cf); // passa il codice fiscale alla classe
- $daticf->calcola(); // estrae i dati a partire dalla stringa CF
- if ($daticf->hasError()) {
- print "ERRORE: " . $daticf->getError() . "\n";
- exit(1);
- } else {
- echo " Data di nascita: " .$daticf->getDataNascitaCF();
- echo " Comune: " .$daticf->getComuneCF();
- echo " Sesso: " .$daticf->getSessoCF();
- }
- ***************************************************************************/
- class retDatiDaCF {
- const ERR_LEN_CF = 'Errore nella lunghezza del Codice Fiscale. Deve essere di 16 caratteri invece รจ di ';
- const ERR_DB_CONN = 'Errore nella connessione al database dei codici catastali';
- const ERR_COD_NTFND = 'Il codice catastale non รจ corretto : ';
- /**
- * Array per il calcolo del mese
- *
- */
- protected $_mesi = array(
- 1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D', 5 => 'E',
- 6 => 'H', 7 => 'L', 8 => 'M', 9 => 'P', 10 => 'R',
- 11 => 'S', 12 => 'T'
- );
- /**
- * Stringa di errore
- */
- protected $_error = null;
- protected $_cognome = "";
- protected $_nome = "";
- protected $_anno = "";
- protected $_mese = "";
- protected $_giorno = "";
- protected $_sesso = "";
- protected $_comune = "";
- protected $_cc = "";
- protected $_CF = ""; // variabile interna del codice fiscale
- /**
- * Controlla la lunghezza del CF. Se ok memorizza.
- */
- public function setCF($cf) {
- if (strlen($cf) != 16) {
- $this->_setError(self::ERR_LEN_CF. (string)strlen($cf));
- return false;
- }
- $this->_CF = $cf;
- return true;
- }
- /**
- * Ritorna alcuni dati estratti dal codice fiscale
- *
- */
- public function calcola() {
- $this->_cognome = substr($this->_CF, 0, 3);
- $this->_nome = substr($this->_CF, 3, 3);
- $this->_anno = substr($this->_CF, 6, 2);
- $this->_mese = array_search(strtoupper(substr($this->_CF, 8, 1)), $this->_mesi);
- $this->_giorno = substr($this->_CF, 9, 2);
- if ((int)$this->_giorno>40) {
- $this->_sesso="F";
- $this->_giorno = (int)$this->giorno - 40;
- } else {
- $this->_sesso="M";
- }
- $this->_comune = $this->_cercaComune(substr($this->_CF, 11, 4));
- $this->_cc = substr($this->_CF, 15, 1);
- if ($this->hasError()) {
- return false;
- }
- // return true;
- }
- public function getComuneCF() {
- return $this->_comune;
- }
- public function getSessoCF() {
- return $this->_sesso;
- }
- public function getDataNascitaCF() {
- return $this->_giorno."/".$this->_mese."/".$this->_anno;
- }
- /**
- * Imposta il messaggio di errore
- */
- protected function _setError($string) {
- $this->_error = $string;
- }
- /**
- * Verifica la presenza di un errore.
- * Ritorna TRUE se presente, FALSE altrimenti.
- */
- public function hasError() {
- return !is_null($this->_error);
- }
- /**
- * Ritorna la stringa di errore
- */
- public function getError() {
- return $this->_error;
- }
- /**
- * Ritorna il codice catastale del comune richiesto
- */
- protected function _cercaComune($codice) {
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "tuodb";
- $comune="";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- $this->_setError(self::ERR_DB_CONN);
- return false;
- }
- $sql = "SELECT comune FROM view_comuni_provincie WHERE codice = '" . strtoupper($codice) . "'";
- $result = $conn->query($sql);
- if ($result->num_rows > 0) {
- $row = $result->fetch_assoc();
- $comune=$row['comune'];
- } else {
- $comune="";
- }
- $conn->close();
- if (is_null($comune) || empty($comune)) {
- $this->_setError(self::ERR_COD_NTFND.$codice);
- return false;
- }
- return $comune;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement