PHP code example of robertogallea / laravel-codicefiscale

1. Go to this page and download the library: Download robertogallea/laravel-codicefiscale library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

robertogallea / laravel-codicefiscale example snippets


'providers' => [
    ...
    robertogallea\LaravelCodiceFiscale\CodiceFiscaleServiceProvider::class,
],

$app->register(robertogallea\LaravelCodiceFiscale\CodiceFiscaleServiceProvider::class);

    public function rules()
    {
        return [
            'codicefiscale' => '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' => '

use robertogallea\LaravelCodiceFiscale\CodiceFiscale;

...
try {
    $cf = new CodiceFiscale();
    $result = $cf->parse('RSSMRA95E05F205Z');
    var_dump($result);
} catch (Exception $exception) {
    echo $exception;
}

[
  "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)
  }
]

use robertogallea\LaravelCodiceFiscale\CodiceFiscale;

...
$cf = new CodiceFiscale();
$result = $cf->tryParse('RSSMRA95E05F205Z');
if ($result) {
    var_dump($cf->asArray());
} else {
    echo $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

$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);

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),
        ];
    }

class MyCityList implements CityDecoderInterface
{
  public function getList()
  {
    // Implementation
  }
}

...
$cf = new CodiceFiscale(new MyCityList)
...

php artisan vendor:publish --provider="robertogallea\LaravelCodiceFiscale\CodiceFiscaleServiceProvider" --tag="config"

php artisan vendor:publish --provider="robertogallea\LaravelCodiceFiscale\CodiceFiscaleServiceProvider" --tag="lang"

// conf/codicefiscale.php

return [
  'city-decoder' => '\robertogallea\LaravelCodiceFiscale\CityCodeDecoders\CompositeCitiesList',

  ...
  
  'cities-decoder-list' => [
        '\robertogallea\LaravelCodiceFiscale\CityCodeDecoders\InternationalCitiesStaticList',
        'YourNamespace\MyCustomList',
    ]

...

class MyCustomList implements CityDecoderInterface
{
  public function getList()
  {
    return [
      'XYZ1' => 'My city 1',
      'XYZ2' => 'My city 2',
    ]
  }
}