PHP Classes

Calcolo Codice Fiscale: Generate the fiscal code for people in Italy

Recommend this page to a friend!

  Detailed description   Download Download .zip .tar.gz   Install with Composer Install with Composer  
This class can be used the generate the fiscal code for people living in Italy.

It takes as parameters the name, surname, birth date, gender, city and region of a person.

The class processes the parameters and generates the person fiscal code.

The code and comments are in Italian.

In Italian:
Classe per il calcolo del codice fiscale.

Example

<?php

require_once 'codicefiscale.class.php';

/**
 * In questo esempio ho esteso la classe CodiceFiscale_Abstract ed ho
 * implementato il metodo astratto _calcolaCatastale() per andare a leggere
 * un file contenente un array nel formato [CODICE_PROVINCIA][NOME_COMUNE]
 * e caricarlo in una variabile..
 *
 * Ovviamente è possibile implementare il metodo per recuperare il catastale
 * a piacimento (database, webservice, ecc...)
 */
class CodiceFiscale extends CodiceFiscale_Abstract {
   
    private
$_db;
   
    public function
leggiCatastali($filename) {
       
$this->_db = unserialize(file_get_contents($filename));
    }
   
    protected function
_calcolaCatastale() {
        if (!
$this->_db) {
            throw new
CodiceFiscale_Exception('impossibile caricare il file dei catastali');
        }
       
$comune = strtoupper($this->comune);
       
$provincia = strtoupper($this->provincia);
        if (!isset(
$this->_db[$provincia][$comune])) {
           
$message = sprintf("codice catastale non trovato (provincia: %s, comune: %s)", $provincia, $comune);
            throw new
CodiceFiscale_Exception($message);
        }
        return
$this->_db[$provincia][$comune];
    }
}

// funzione di comodità per generare un array da passare alla classe
function componi($nome, $cognome, $data, $comune, $provincia, $sesso) {
    return [
      
'nome' => $nome,
      
'cognome' => $cognome,
      
'data' => $data,
      
'comune' => $comune,
      
'provincia' => $provincia,
      
'sesso' => $sesso
   
];
}

// istanzio la classe e carico il db dei catastali
$cf = new CodiceFiscale();
$cf->leggiCatastali('catastali.txt');
$dati = array();

// Non validi
$dati[] = componi(null, 'cognome', '01-01-1970', 'milano', 'mi', 'm');
$dati[] = componi('nome', null, '01-01-1970', 'milano', 'mi', 'm');
$dati[] = componi('nome', 'cognome', null, 'milano', 'mi', 'm');
$dati[] = componi('nome', 'cognome', '01-01-1970', null, 'mi', 'm');
$dati[] = componi('nome', 'cognome', '01-01-1970', 'milano', null, 'm');
$dati[] = componi('nome', 'cognome', '01-01-1970', 'milano', 'mi', null);
$dati[] = componi('nome', 'cognome', '565-55-5555', 'milano', 'mi', 'm');

// Validi
$dati[] = componi('A', 'B', '01-05-1977', 'rho', 'mi', 'm');
$dati[] = componi('A', 'B', '01-05-1977', 'rho', 'mi', 'f');
$dati[] = componi('Matteo', 'De Gasperi', '15-09-2001', 'roma', 'rm', 'm');
$dati[] = componi('Ramona', 'Si', '21-12-1982', 'senago', 'mi', 'f');
$dati[] = componi('Babbo', 'Natale', '25-12-1938', 'Cinisello Balsamo', 'MI', 'm');

foreach(
$dati as $persona) {
    try {
       
$cf->importa($persona);
        print
"Codice Fiscale: ". $cf->calcola() . "\n";
    } catch (
CodiceFiscale_Exception $e) {
        print
"Errore: ". $e->getMessage() . "\n";
        continue;
    }
}

// Metodo alternativo
$cf->nome = 'Me';
$cf->cognome = 'Medesimo';
$cf->data = '09-09-1982';
$cf->comune = 'senago';
$cf->provincia = 'mi';
$cf->sesso = 'm';

print
"Codice Fiscale (alternativo): ". $cf->calcola() . "\n";

$cf->formatoData('d/m/Y');
$cf->data = '09/09/1982';

print
"Codice Fiscale (alternativo): ". $cf->calcola() . "\n";






  Author Author  
Picture of Michele Brodoloni
Name: Michele Brodoloni <contact>
Classes: 6 packages by
Country: Ireland Ireland
Age: 38
All time rank: 2491 in Ireland Ireland
Week rank: 586 Down1 in Ireland Ireland Equal
Innovation award
Innovation award
Nominee: 3x

  Classes of Michele Brodoloni  >  Calcolo Codice Fiscale  >  Download Download .zip .tar.gz  >  Support forum Support forum (10)  >  Blog Blog  
Name: Calcolo Codice Fiscale
Base name: codicefiscale
Description: Generate the fiscal code for people in Italy
Version: 2.0
PHP version: 5.3
License: Free for non-commercial use
All time users: 1754 users
All time rank: 2237
Week users: 0 users
Week rank: 95 Equal
Country specific: This package is specific mainly for applications used in Italy Italy .
 
  Groups   Rate classes User ratings   Applications   Files Files  

  Groups  
Group folder image Finances Money, exchanging, taxes and stocks View top rated classes


  User ratings  
RatingsUtility Consistency Documentation Examples Tests Videos Overall Rank
All time: Good (83%) Sufficient (75%) - Not sure (50%) - - Not sure (54%) 1991
Month: Not yet rated by the users

  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  Files folder image Files  
File Role Description
Accessible without login Plain text file catastali.txt Data database catastali serializzato
Plain text file codicefiscale.class.php Class Classe astratta Codice Fiscale
Accessible without login Plain text file esempio.php Example Script di esempio

Install with Composer Install with Composer - Download Download all files: codicefiscale.tar.gz codicefiscale.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
  Files folder image Files  
File Role Description
Accessible without login Plain text file catastali.txt Data database catastali serializzato
Plain text file codicefiscale.class.php Class Classe astratta Codice Fiscale
Accessible without login Plain text file esempio.php Example Script di esempio

Install with Composer Install with Composer - Download Download all files: codicefiscale.tar.gz codicefiscale.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.