Laravel-codicefiscale是一套意大利CodiceFiscale
(即税法)的软件包。该软件包允许轻松验证和解析CodiceIceFiscale。它也适用于Laravel,因为它为请求验证提供了方便的自定义验证器。
拉拉维尔 | 包裹 |
---|---|
11.x | 2.x |
10.x | 1.x |
9.x | 1.x |
8.x | 1.x |
7.x | 1.x |
6.x | 1.x |
重要更新:现在,您可以使用非默认
IstatRemoteCSVList
City Dododer从ISTAT动态加载城市代码。
运行以下命令以安装软件包的最新适用版本:
composer require robertogallea/laravel-codicefiscale:^2
在您的应用程序配置中,将服务提供商添加到$providers
数组(仅适用于Laravel 5.4或以下) :
' providers ' => [
...
robertogallea LaravelCodiceFiscale CodiceFiscaleServiceProvider ::class,
],
验证错误消息将以it
和en
语言翻译,如果要添加新语言,请给我发送PR。
在bootstrap/app.php
中,注册服务提供商
$ app -> register (robertogallea LaravelCodiceFiscale CodiceFiscaleServiceProvider ::class);
要自定义软件包配置,您必须将配置文件导出到config/codicefiscale.php
中。
这可以通过启动以下命令来实现:
php artisan vendor:publish --provider="robertogalleaLaravelCodiceFiscaleCodiceFiscaleServiceProvider" --tag="config"
您可以配置以下参数:
city-decoder
:用于解码城市代码的课程(请参阅城市代码解析),默认为InternationalCitiesStaticList
。date-format
:用于解析生日的日期格式,默认为'Ym-d'
。labels
:用于male
和female
的标签,默认为'M'
和'F'
。 您可以使用此命令自定义发布验证翻译的验证消息:
php artisan vendor:publish --provider="robertogalleaLaravelCodiceFiscaleCodiceFiscaleServiceProvider" --tag="lang"
要验证Codice Fiscale,请在验证规则数组中使用codice_fiscale
关键字
public function rules ()
{
return [
' codicefiscale ' => ' codice_fiscale ' ,
//...
];
}
从1.9.0版本,您可以针对其他表单字段验证Codice Fiscale,以检查是否有匹配项。
您必须指定所有必需的字段:
first_name
last_name
birthdate
place
gender
给出codice_fiscale
规则的参数。
例如:
public function rules ()
{
return [
' codicefiscale ' => ' codice_fiscale:first_name=first_name_field,last_name=last_name_field,birthdate=birthdate_field,place=place_field,gender=gender_field ' ,
' first_name_field ' => ' required|string ' ,
' last_name_field ' => ' required|string ' ,
' birthdate_field ' => ' required|date ' ,
' place_field ' => ' required|string ' ,
' gender_field ' => ' required|string|max:1 ' ,
//...
];
}
如果提供的codiceIteFiscale和从输入字段生成的验证案例不匹配,则验证会失败。
可以将Codice Fiscale包裹在robertogalleaLaravelCodiceFiscaleCodiceFiscale
类中,以通过有用的效用方法来增强它。
use robertogallea LaravelCodiceFiscale CodiceFiscale ;
. . .
try {
$ cf = new CodiceFiscale ();
$ result = $ cf -> parse ( ' RSSMRA95E05F205Z ' );
var_dump ( $ result );
} catch ( Exception $ exception ) {
echo $ exception ;
}
如果有效的CodiceIceFiscale会产生以下结果:
[
" gender " => " M "
"birth_place" => " F205 "
"birth_place_complete" => " Milano " ,
" day " => " 05 "
"month" => " 05 "
"year" => " 1995 "
"birthdate" => Carbon @ 799632000 {
date: 1995 - 05 - 05 00 : 00 : 00.0 UTC (+ 00 : 00 )
}
]
如果发生错误, CodiceFiscale::parse()
抛出了一个CodiceFiscaleValidationException
返回了一个定义的常数之一,则具有$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
如果您不想捕获异常,则可以使用CodiceFiscale::tryParse()
:
use robertogallea LaravelCodiceFiscale CodiceFiscale ;
. . .
$ cf = new CodiceFiscale ();
$ result = $ cf -> tryParse ( ' RSSMRA95E05F205Z ' );
if ( $ result ) {
var_dump ( $ cf -> asArray ());
} else {
echo $ cf -> getError ();
}
返回与上面相同的值,您可以使用$cf->isValid()
检查codiceIceFiscale是否有效,并且$cf->getError()
以获取错误。这在刀片模板中特别有用:
@ php ( $ cf = new robertogallea LaravelCodiceFiscale CodiceFiscale ())
@ if ( $ cf -> tryParse ( $ codicefiscale ))
<p><i class="fa fa-check" style="color:green"></i>{{ $ cf -> getCodiceFiscale ()}}</p>
@else
<p><i class="fa fa-check" style="color:red"></i>{{ $ cf -> getError ()-> getMessage ()}}</p>
@endif
类CodiceFiscale
可用于从输入值中生成Codice Fiscale Fiscale字符串:
$ first_name = ' Mario ' ;
$ last_name = ' Rossi ' ;
$ birth_date = ' 1995-05-05 ' ; // or Carbon::parse('1995-05-05')
$ birth_place = ' F205 ' ; // or 'Milano'
$ gender = ' M ' ;
$ cf_string = CodiceFiscale :: generate ( $ first_name , $ last_name , $ birth_date , $ birth_place , $ gender );
您可以使用提供的Faker扩展名来在工厂中生成假的Codice Fiscale:
class PersonFactory extends Factory
{
public function definition (): array
{
return [
' first_name ' => $ firstName = fake ()-> firstName (),
' last_name ' => $ lastName = fake ()-> lastName (),
' fiscal_number ' => fake ()-> codiceFiscale (firstName: $ firstName , lastName: $ lastName ),
];
}
注意:您可以提供一些生成Codice Fiscale所需的全部或无信息( firstName
, lastName
, birthDate
, birthPlace
, gender
)
有三种解码城市法规的策略:
InternationalCitiesStaticList
:意大利城市的静态清单;ItalianCitiesStaticList
:国际城市的静态清单;IstatRemoteCSVList
:从官方ISTAT CSV文件中加载的意大利城市列表的动态(从Web加载)。请注意,该列表是缓存的(默认情况下有一天,请参见“要更改”的配置)。CompositeCitiesList
:使用配置键codicefiscale.cities-decoder-list
中的基本CityDecoderInterface
合并两个CityDecoderInterface
类(例如IstatRemoteCSVList
和InternationalCitiesStaticList
)的结果。默认情况下,该软件包使用InternationalCitiesStaticList
类从代码和Viceversa查找城市。但是,您可以使用自己的班级来改变所使用的策略。
您只需要实现CityDecoderInterface
及其getList()
方法即可。然后,要使用它,只需将一个实例传递到CodiceFiscale
类。
例如:
class MyCityList implements CityDecoderInterface
{
public function getList ()
{
// Implementation
}
}
. . .
$ cf = new CodiceFiscale ( new MyCityList )
. . .
注意:如果您发现缺失城市,请进行公关!
如果要集成城市列表,则可以通过合并所提供的解码器和自定义解码器的结果来使用CompositeCitiesList
。
例如:
// conf/codicefiscale.php
return [
'city-decoder' => 'robertogalleaLaravelCodiceFiscaleCityCodeDecodersCompositeCitiesList',
...
'cities-decoder-list' => [
'robertogalleaLaravelCodiceFiscaleCityCodeDecodersInternationalCitiesStaticList',
'YourNamespaceMyCustomList',
]
MyCustomList
被定义为:
...
class MyCustomList implements CityDecoderInterface
{
public function getList()
{
return [
'XYZ1' => 'My city 1',
'XYZ2' => 'My city 2',
]
}
}