davidepastore/codice-fiscale
PHP library to calculate and validate the Italian tax code (Codice Fiscale). Build a Subject (name, surname, birth date, gender, Belfiore code), generate the code, support omocodia levels, and list all possible variants (128 levels).
Installation Add the package via Composer (unchanged):
composer require davidepastore/codice-fiscale
No additional configuration is required—it remains a self-contained library.
First Use Case (Updated for PHP 8.4 compatibility) Generate a tax code (codice fiscale) for a given name, surname, and birthdate:
use DavidePastore\CodiceFiscale\CodiceFiscale;
// PHP 8.4+ compatible (DateTime constructor changes handled internally)
$cf = new CodiceFiscale('Mario', 'Rossi', '1980-05-15', 'M', 'Roma');
echo $cf->getCodiceFiscale(); // Output: RSSMRR80E15A453X
Where to Look First (Updated with CI/CD and Docker context)
DavidePastore\CodiceFiscale\CodiceFiscale (handles generation/validation with PHP 8.4+ fixes).tests/ directory (now includes PHP 8.3/8.4 test configurations)..docker/ setup for local development (see PR #73).Generating a Codice Fiscale (No changes, but now fully PHP 8.4-compatible)
$cf = new CodiceFiscale('Anna', 'Bianchi', '1995-11-20', 'F', 'Milano');
$code = $cf->getCodiceFiscale(); // Returns validated string.
Validating an Existing Codice Fiscale (No changes)
use DavidePastore\CodiceFiscale\Validator;
$validator = new Validator();
$isValid = $validator->validate('RSSMRR80E15A453X'); // Returns bool.
Parsing a Codice Fiscale (No changes)
$parser = new \DavidePastore\CodiceFiscale\Parser();
$data = $parser->parse('RSSMRR80E15A453X');
Laravel Blade Integration (No changes, but now tested on PHP 8.4)
// app/Helpers/CodiceFiscale.php
function generateCodiceFiscale($name, $surname, $birthdate, $gender, $city) {
return (new \DavidePastore\CodiceFiscale\CodiceFiscale($name, $surname, $birthdate, $gender, $city))->getCodiceFiscale();
}
Docker/Laravel Sail Integration (New) Use the package in a Dockerized Laravel environment:
# Start Sail with PHP 8.4
sail up -d
sail composer require davidepastore/codice-fiscale
# Run tests in container
sail artisan test
Form Request Validation (No changes, but now CI-tested on PHP 8.4)
public function rules() {
return [
'codice_fiscale' => ['required', function ($attribute, $value, $fail) {
if (!Validator::validate($value)) {
$fail('Codice fiscale non valido.');
}
}]
];
}
Municipality Code Requirement (Unchanged, but now CI-tested on PHP 8.4)
config/cities.php) or use an external API.Foreign Names/Surnames (Unchanged, but now tested on PHP 8.4)
$normalizedName = strtoupper(preg_replace('/[^A-Za-z]/', '', $name));
PHP 8.1+ DateTime Deprecation (Resolved in v0.10.0)
DateTime constructor changes internally (see PR #81).Docker Setup Quirks (New)
.env files are copied:
cp .env.example .env
sail artisan test
Validation Errors:
$validator = new Validator();
$errors = $validator->getErrors('INVALID123'); // Works on PHP 8.4.
Logging Parsed Data:
$parser = new \DavidePastore\CodiceFiscale\Parser();
$data = $parser->parse('RSSMRR80E15A453X');
\Log::info('Parsed data:', $data);
Custom Municipality Mapping (No changes, but now tested on PHP 8.4)
class CustomCodiceFiscale extends \DavidePastore\CodiceFiscale\CodiceFiscale {
protected function getMunicipalityCode($city) {
return $this->customMapping[$city] ?? parent::getMunicipalityCode($city);
}
}
Docker-Based Development (New) Extend the Docker setup for local testing:
# Add to docker-compose.yml
services:
laravel.test:
build:
context: .
dockerfile: Dockerfile
environment:
- COMPOSER_ALLOW_SUPERUSER=1
PHP 8.4-Specific Optimizations (New) Leverage PHP 8.4 features in custom extensions:
// Example: Use read-only properties (PHP 8.4)
class StrictCodiceFiscale extends \DavidePastore\CodiceFiscale\CodiceFiscale {
public function __construct(
public readonly string $name,
public readonly string $surname,
// ...
) {
parent::__construct($name, $surname, ...);
}
}
Caching: Now tested on PHP 8.4’s OPcache improvements.
Batch Validation: Use Laravel’s parallel with PHP 8.4’s JIT compiler:
$codes = collect($codes)->parallel()->each(fn($code) => Validator::validate($code));
Docker Performance: Enable PHP 8.4’s ZTS mode in Docker for better multi-threading:
RUN docker-php-ext-configure zts && docker-php-ext-install pdo pdo_mysql
How can I help you explore Laravel packages today?