Skip to content

Commit

Permalink
Merge pull request #12 from antonioturdo/validator
Browse files Browse the repository at this point in the history
Added Validator and InverseCalculator functionalities (closed #11)
  • Loading branch information
DavidePastore authored Jan 27, 2018
2 parents 52c6bdc + e2b6f09 commit 42eaf28
Show file tree
Hide file tree
Showing 7 changed files with 879 additions and 153 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,47 @@ $response = $checker->check();
echo $response; // true
```

### Formal validation

Use the Validator to verify if the given codice fiscale is formally valid. The additional configuration array for the `Validator` has the given available keys:
- "omocodiaAllowed": whether to allow or not omocodia, defaults to true;
- "century": for people over 100 years old, it is not possibile to derive unambiguously birth year, so you can specify the century (for example '18' for a person birth in 1899). It allows to check birth date existence. Defaults to null (auto calculation of the century).

```php
use CodiceFiscale\Validator;

$codiceFiscale = "RSSMRA85T10A562S";

$validator = new Validator($codiceFiscale);

$response = $validator->isFormallyValid();
echo $response; // true
```

### Inverse calculation

Use the InverseCalculator to extract birth date, gender and the belfiore code from the given codice fiscale. The additional configuration array for the `InverseCalculator` has the keys already described for the `Validator`.

```php
use CodiceFiscale\InverseCalculator;

$codiceFiscale = "RSSMRA85T10A562S";

$inverseCalculator = new InverseCalculator($codiceFiscale);

$subject = $inverseCalculator->getSubject();
var_dump($response);
// object(CodiceFiscale\Subject)[449]
// private 'name' => null
// private 'surname' => null
// private 'birthDate' =>
// object(DateTime)[452]
// public 'date' => string '1985-12-10 00:00:00.000000' (length=26)
// public 'timezone_type' => int 3
// public 'timezone' => string 'Europe/Berlin' (length=13)
// private 'gender' => string 'M' (length=1)
// private 'belfioreCode' => string 'A562' (length=4)
```

Test
----
Expand Down
170 changes: 170 additions & 0 deletions src/CodiceFiscale/AbstractCalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

namespace CodiceFiscale;

/**
* Codice Fiscale abstract calculator.
*
* @author Antonio Turdo <[email protected]>
*/
abstract class AbstractCalculator
{

// women char
const CHR_WOMEN = 'F';

// male char
const CHR_MALE = 'M';

/**
* Array of all available months.
*/
protected $months = array(
'1' => 'A',
'2' => 'B',
'3' => 'C',
'4' => 'D',
'5' => 'E',
'6' => 'H',
'7' => 'L',
'8' => 'M',
'9' => 'P',
'10' => 'R',
'11' => 'S',
'12' => 'T',
);

/**
* Array of all avaialable odd characters.
*/
protected $odd = array(
'0' => 1,
'1' => 0,
'2' => 5,
'3' => 7,
'4' => 9,
'5' => 13,
'6' => 15,
'7' => 17,
'8' => 19,
'9' => 21,
'A' => 1,
'B' => 0,
'C' => 5,
'D' => 7,
'E' => 9,
'F' => 13,
'G' => 15,
'H' => 17,
'I' => 19,
'J' => 21,
'K' => 2,
'L' => 4,
'M' => 18,
'N' => 20,
'O' => 11,
'P' => 3,
'Q' => 6,
'R' => 8,
'S' => 12,
'T' => 14,
'U' => 16,
'V' => 10,
'W' => 22,
'X' => 25,
'Y' => 24,
'Z' => 23,
);

/**
* Array of all avaialable even characters.
*/
protected $even = array(
'0' => 0,
'1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
'6' => 6,
'7' => 7,
'8' => 8,
'9' => 9,
'A' => 0,
'B' => 1,
'C' => 2,
'D' => 3,
'E' => 4,
'F' => 5,
'G' => 6,
'H' => 7,
'I' => 8,
'J' => 9,
'K' => 10,
'L' => 11,
'M' => 12,
'N' => 13,
'O' => 14,
'P' => 15,
'Q' => 16,
'R' => 17,
'S' => 18,
'T' => 19,
'U' => 20,
'V' => 21,
'W' => 22,
'X' => 23,
'Y' => 24,
'Z' => 25,
);


/**
* Array of all avaialable omocodia characters.
*/
protected $omocodiaCodes = array(
'0' => 'L',
'1' => 'M',
'2' => 'N',
'3' => 'P',
'4' => 'Q',
'5' => 'R',
'6' => 'S',
'7' => 'T',
'8' => 'U',
'9' => 'V',
);

/**
* Calculate the sum by the given dictionary for the given temporary codice fiscale.
*
* @param $temporaryCodiceFiscale The temporary codice fiscale.
* @param $dictionaryArray The dictionary array.
* @param $i The start index value.
* @returns Returns the sum by the given dictionary for the given temporary codice fiscale.
*/
protected function calculateSumByDictionary($temporaryCodiceFiscale, $dictionaryArray, $i)
{
$sum = 0;
for (; $i < 15; $i = $i + 2) {
$k = $temporaryCodiceFiscale{$i};
$sum = $sum + $dictionaryArray[$k];
}

return $sum;
}

/**
* Calculate the check digit.
*
* @param $temporaryCodiceFiscale The first part of the codice fiscale.
* @returns Returns the check digit part of the codice fiscale.
*/
protected function calculateCheckDigit($temporaryCodiceFiscale)
{
$sumEven = $this->calculateSumByDictionary($temporaryCodiceFiscale, $this->even, 1);
$sumOdd = $this->calculateSumByDictionary($temporaryCodiceFiscale, $this->odd, 0);

return chr(($sumOdd + $sumEven) % 26 + 65);
}
}
Loading

0 comments on commit 42eaf28

Please sign in to comment.