Skip to content

Commit

Permalink
Fix validation regex
Browse files Browse the repository at this point in the history
  • Loading branch information
robertogallea committed Jul 7, 2021
1 parent 88f7d54 commit 4d4d684
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 44 deletions.
24 changes: 12 additions & 12 deletions src/Checks/CheckForWrongCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class CheckForWrongCode implements Check
{
const CF_REGEX = '/^[a-z]{6}[0-9]{2}[a-z][0-9]{2}[a-z][0-9]{2}[0-9a-z][a-z]$/i';
const CF_REGEX = '/^[a-z]{6}[0-9a-z]{2}[a-z][0-9a-z]{2}[a-z][0-9a-z]{3}[a-z]$/i';

protected $tabEvenChars = [
'0' => 0,
Expand Down Expand Up @@ -87,16 +87,16 @@ class CheckForWrongCode implements Check
];

protected $tabControlCode = [
0 => 'A',
1 => 'B',
2 => 'C',
3 => 'D',
4 => 'E',
5 => 'F',
6 => 'G',
7 => 'H',
8 => 'I',
9 => 'J',
0 => 'A',
1 => 'B',
2 => 'C',
3 => 'D',
4 => 'E',
5 => 'F',
6 => 'G',
7 => 'H',
8 => 'I',
9 => 'J',
10 => 'K',
11 => 'L',
12 => 'M',
Expand Down Expand Up @@ -144,6 +144,6 @@ public function check($code)
*/
protected function checkRegex(string $code): bool
{
return (bool) preg_match(self::CF_REGEX, $code);
return (bool)preg_match(self::CF_REGEX, $code);
}
}
20 changes: 10 additions & 10 deletions src/CodiceFiscale.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,18 @@ public function parse($cf)
if ($this->gender === 'F') {
$this->day = $this->day - 40;
if (strlen($this->day) === 1) {
$this->day = '0'.$this->day;
$this->day = '0' . $this->day;
}
}

return [
'gender' => $this->getGender(),
'birth_place' => $this->getBirthPlace(),
'gender' => $this->getGender(),
'birth_place' => $this->getBirthPlace(),
'birth_place_complete' => $this->getBirthPlaceComplete(),
'day' => $this->getDay(),
'month' => $this->getMonth(),
'year' => $this->getYear(),
'birthdate' => $this->getBirthdate(),
'day' => $this->getDay(),
'month' => $this->getMonth(),
'year' => $this->getYear(),
'birthdate' => $this->getBirthdate(),
];
}

Expand Down Expand Up @@ -193,16 +193,16 @@ public function getYear()
{
$current_year = Carbon::today()->year;
if (2000 + $this->year < $current_year) {
return '20'.$this->year;
return '20' . $this->year;
}

return '19'.$this->year;
return '19' . $this->year;
}

public function getBirthdate(): Carbon
{
try {
return Carbon::parse($this->getYear().'-'.$this->getMonth().'-'.$this->getDay());
return Carbon::parse($this->getYear() . '-' . $this->getMonth() . '-' . $this->getDay());
} catch (\Exception $exception) {
throw new CodiceFiscaleValidationException('Parsed date is not valid');
}
Expand Down
36 changes: 18 additions & 18 deletions src/CodiceFiscaleGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,37 @@ class CodiceFiscaleGenerator
];

protected $_mesi = [
1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D', 5 => 'E',
6 => 'H', 7 => 'L', 8 => 'M', 9 => 'P', 10 => 'R',
1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D', 5 => 'E',
6 => 'H', 7 => 'L', 8 => 'M', 9 => 'P', 10 => 'R',
11 => 'S', 12 => 'T',
];

protected $_pari = [
'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,
'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,
];

protected $_dispari = [
'0' => 1, '1' => 0, '2' => 5, '3' => 7, '4' => 9,
'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,
'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,
'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,
];

protected $_controllo = [
'0' => 'A', '1' => 'B', '2' => 'C', '3' => 'D',
'4' => 'E', '5' => 'F', '6' => 'G', '7' => 'H',
'8' => 'I', '9' => 'J', '10' => 'K', '11' => 'L',
'0' => 'A', '1' => 'B', '2' => 'C', '3' => 'D',
'4' => 'E', '5' => 'F', '6' => 'G', '7' => 'H',
'8' => 'I', '9' => 'J', '10' => 'K', '11' => 'L',
'12' => 'M', '13' => 'N', '14' => 'O', '15' => 'P',
'16' => 'Q', '17' => 'R', '18' => 'S', '19' => 'T',
'20' => 'U', '21' => 'V', '22' => 'W', '23' => 'X',
Expand Down Expand Up @@ -203,7 +203,7 @@ protected function _calcolaDataNascita()
$gg = (strtoupper($this->sesso) == 'M') ? $giorno : $giorno + 40;
$gg = str_pad($gg, 2, '0', STR_PAD_LEFT);

return $aa.$mm.$gg;
return $aa . $mm . $gg;
}

protected function _calcolaCatastale()
Expand Down Expand Up @@ -236,13 +236,13 @@ protected function _calcolaCifraControllo($codice)

public function calcola()
{
$codice = $this->_calcolaCognome().
$this->_calcolaNome().
$this->_calcolaDataNascita().
$codice = $this->_calcolaCognome() .
$this->_calcolaNome() .
$this->_calcolaDataNascita() .
$this->_calcolaCatastale();
$codice .= $this->_calcolaCifraControllo($codice);
if (strlen($codice) != 16) {
throw new CodiceFiscaleGenerationException('Generated code has not 16 digits: '.$codice);
throw new CodiceFiscaleGenerationException('Generated code has not 16 digits: ' . $codice);
}

return $codice;
Expand Down
2 changes: 1 addition & 1 deletion src/CodiceFiscaleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ private function publishConfig()

private function packagePath($path)
{
return __DIR__."/../$path";
return __DIR__ . "/../$path";
}
}
19 changes: 16 additions & 3 deletions tests/CodiceFiscaleValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,26 @@ public function testWrongOmocodiaCode()
$res = $cf->parse($codice_fiscale);
}

public function testOmocodiaCode()
/**
* @dataProvider omocodiaProvider
*/
public function testOmocodiaCode($codice_fiscale, $city)
{
$codice_fiscale = 'RSSMRA95E05F20RU';
// $codice_fiscale = 'RSSMRA95E05F20RU';
$cf = new CodiceFiscale();

$res = $cf->parse($codice_fiscale);
$this->assertEquals($res['birth_place_complete'], 'Milano');
$this->assertEquals($res['birth_place_complete'], $city);
}



public function omocodiaProvider()
{
return [
['RSSMRA95E05F20RU', 'Milano'],
['MKJRLA80A01L4L7I', 'Treviso']
];
}

public function testUnregularCode()
Expand Down

0 comments on commit 4d4d684

Please sign in to comment.