Download the PHP package robertogallea/laravel-codicefiscale without Composer
On this page you can find all versions of the php package robertogallea/laravel-codicefiscale. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download robertogallea/laravel-codicefiscale
More information about robertogallea/laravel-codicefiscale
Files in robertogallea/laravel-codicefiscale
Package laravel-codicefiscale
Short Description Codice fiscale validation for php/laravel
License
Informations about the package laravel-codicefiscale
laravel-codicefiscale
laravel-codicefiscale is a package for the management of the Italian CodiceFiscale
(i.e. tax code).
The package allows easy validation and parsing of the CodiceFiscale. It is also suited for Laravel since it provides a
convenient custom validator for request validation.
Laravel Version Compatibility
Laravel | Package |
---|---|
11.x | 2.x |
10.x | 1.x |
9.x | 1.x |
8.x | 1.x |
7.x | 1.x |
6.x | 1.x |
Important update: now you can dynamically load city codes from ISTAT using the non-default
IstatRemoteCSVList
city decoder.
- Installation
- Configuration
- Validation
- Utility CodiceFiscale class
- Codice fiscale Generation
- Faker integration
- City code parsing
- Integrate your own cities
Installation
Run the following command to install the latest applicable version of the package:
Laravel
In your app config, add the Service Provider to the $providers
array (only for Laravel 5.4 or below):
The validation error messages are translated in it
and en
languages, if you want to add new language please send me
a PR.
Lumen
In bootstrap/app.php
, register the Service Provider
Configuration
To customize the package configuration, you must export the configuration file into config/codicefiscale.php
.
This can be achieved by launching the following command:
You can configure the following parameters:
city-decoder
: the class used for decoding city codes (see City code parsing), default toInternationalCitiesStaticList
.date-format
: the date format used for parsing birthdates, default to'Y-m-d'
.labels
: the labels used formale
andfemale
persons, defaults to'M'
and'F'
.
Language Files
You can customize the validation messages publishing the validation translations with this command:
Validation
To validate a codice fiscale, use the codice_fiscale
keyword in your validation rules array
From version 1.9.0 you can validate your codice fiscale against other form fields to check whether there is a match or not.
You must specify all of the required fields:
first_name
last_name
birthdate
place
gender
giving parameters to the codice_fiscale
rule.
For example:
Validation fails if the provided codicefiscale and the one generated from the input fields do not match.
Utility CodiceFiscale class
A codice fiscale can be wrapped in the robertogallea\LaravelCodiceFiscale\CodiceFiscale
class to enhance it with
useful utility methods.
In case of a valid codicefiscale it produces the following result:
in case of an error, CodiceFiscale::parse()
throws an CodiceFiscaleValidationException
, which returns one of the
defined constants with $exception->getCode()
:
CodiceFiscaleException::NO_ERROR
CodiceFiscaleException::NO_CODE
CodiceFiscaleException::WRONG_SIZE
CodiceFiscaleException::BAD_CHARACTERS
CodiceFiscaleException::BAD_OMOCODIA_CHAR
CodiceFiscaleException::WRONG_CODE
CodiceFiscaleException::MISSING_CITY_CODE
If you rather not want to catch exceptions, you can use CodiceFiscale::tryParse()
:
which returns the same values as above, you can use $cf->isValid()
to check if the codicefiscale is valid and
$cf->getError()
to get the error.
This is especially useful in a blade template:
Codice fiscale Generation
Class CodiceFiscale
could be used to generate codice fiscale strings from input values:
Faker integration
You can generate fake codice fiscale in your factories using the provided faker extension:
Note: you can provide some, all or none of the information required for the generation of codice fiscale
(firstName
, lastName
, birthDate
, birthPlace
, gender
)
City code parsing
There are three strategies for decoding the city code:
InternationalCitiesStaticList
: a static list of Italian cities;ItalianCitiesStaticList
: a static list of International cities;IstatRemoteCSVList
: a dynamic (loaded from web) list of Italian cities loaded from official ISTAT csv file. Please note that the list is cached (one day by default, see config to change).CompositeCitiesList
: merge the results from twoCityDecoderInterface
classes (for exampleIstatRemoteCSVList
andInternationalCitiesStaticList
) using the baseCityDecoderInterface
in the config keycodicefiscale.cities-decoder-list
.
By default, the package uses the class InternationalCitiesStaticList
to lookup the city from the code and viceversa.
However, you could use your own class to change the strategy used.
You just need to implement the CityDecoderInterface
and its getList()
method.
Then, to use it, just pass an instance to the CodiceFiscale
class.
For example:
Integrate your own cities
Note: if you find missing cities, please make a PR!
If you want to integrate the cities list, you can use the CompositeCitiesList
by merging the results of one of the
decoders provided and a custom decoder.
For example:
where MyCustomList
is defined as:
All versions of laravel-codicefiscale with dependencies
illuminate/support Version ^11.0
illuminate/validation Version ^11.0
nesbot/carbon Version ^3.5
guzzlehttp/guzzle Version ^7.3