Skip to content

Commit

Permalink
Merge b752b15 into fa5c547
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidePastore committed Dec 4, 2020
2 parents fa5c547 + b752b15 commit c1d08cd
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 163 deletions.
2 changes: 1 addition & 1 deletion .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
preset: psr2
preset: psr12

risky: false
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
},
"require-dev": {
"scrutinizer/ocular": "~1.1",
"phpunit/phpunit": "^7.5 || ^8.5"
"phpunit/phpunit": "^7.5 || ^8.5",
"squizlabs/php_codesniffer": "3.*"
},
"license" : "GPL-2.0-or-later",
"type" : "library",
Expand Down
13 changes: 6 additions & 7 deletions src/AbstractCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
*/
abstract class AbstractCalculator
{

// women char
const CHR_WOMEN = 'F';
protected const CHR_WOMEN = 'F';

// male char
const CHR_MALE = 'M';
protected const CHR_MALE = 'M';

/**
* Array of all available months.
*/
Expand All @@ -34,7 +33,7 @@ abstract class AbstractCalculator
'11' => 'S',
'12' => 'T',
);

/**
* Array of all avaialable odd characters.
*/
Expand Down Expand Up @@ -118,7 +117,7 @@ abstract class AbstractCalculator
'Y' => 24,
'Z' => 25,
);


/**
* Array of all avaialable omocodia characters.
Expand Down
77 changes: 62 additions & 15 deletions src/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public function __construct(Subject $subject, $properties = array())
*/
public function calculate()
{
$temporaryCodiceFiscale = $this->calculateSurname().$this->calculateName().
$this->calculateBirthDateAndGender().$this->calculateBelfioreCode();
$temporaryCodiceFiscale = $this->calculateSurname() . $this->calculateName() .
$this->calculateBirthDateAndGender() . $this->calculateBelfioreCode();
$temporaryCodiceFiscale = $this->calculateOmocodia($temporaryCodiceFiscale);

return $temporaryCodiceFiscale.$this->calculateCheckDigit($temporaryCodiceFiscale);
return $temporaryCodiceFiscale . $this->calculateCheckDigit($temporaryCodiceFiscale);
}

/**
Expand Down Expand Up @@ -92,7 +92,7 @@ private function calculateName()
$name = $this->cleanString($this->subject->getName());
$consonants = str_replace($this->vowels, '', strtoupper($name));
if (strlen($consonants) > 3) {
$result = $consonants[0].$consonants[2].$consonants[3];
$result = $consonants[0] . $consonants[2] . $consonants[3];
} elseif (strlen($consonants) == 3) {
$result = $consonants;
} else {
Expand All @@ -113,7 +113,7 @@ private function calculateSmallString($consonants, $string)
{
$string = $this->cleanString($string);
$vowels = str_replace(str_split($consonants), '', strtoupper($string));
$result = substr($consonants.$vowels.'XXX', 0, 3);
$result = substr($consonants . $vowels . 'XXX', 0, 3);

return $result;
}
Expand All @@ -132,7 +132,7 @@ private function calculateBirthDateAndGender()
$day += 40;
}

return $year.$month.$day;
return $year . $month . $day;
}

/**
Expand All @@ -155,13 +155,55 @@ private function calculateOmocodia($temporaryCodiceFiscale)
{
if ($this->omocodiaLevel > 0) {
if ($this->omocodiaLevel) {
$temporaryCodiceFiscale = $this->replaceOmocodiaSection(2, $temporaryCodiceFiscale, 1, 1, $this->omocodiaPositions[0]);
$temporaryCodiceFiscale = $this->replaceOmocodiaSection(4, $temporaryCodiceFiscale, 2, 3, $this->omocodiaPositions[1]);
$temporaryCodiceFiscale = $this->replaceOmocodiaSection(8, $temporaryCodiceFiscale, 4, 7, $this->omocodiaPositions[2]);
$temporaryCodiceFiscale = $this->replaceOmocodiaSection(16, $temporaryCodiceFiscale, 8, 15, $this->omocodiaPositions[3]);
$temporaryCodiceFiscale = $this->replaceOmocodiaSection(32, $temporaryCodiceFiscale, 16, 31, $this->omocodiaPositions[4]);
$temporaryCodiceFiscale = $this->replaceOmocodiaSection(64, $temporaryCodiceFiscale, 32, 63, $this->omocodiaPositions[5]);
$temporaryCodiceFiscale = $this->replaceOmocodiaSection(128, $temporaryCodiceFiscale, 64, 127, $this->omocodiaPositions[6]);
$temporaryCodiceFiscale = $this->replaceOmocodiaSection(
2,
$temporaryCodiceFiscale,
1,
1,
$this->omocodiaPositions[0]
);
$temporaryCodiceFiscale = $this->replaceOmocodiaSection(
4,
$temporaryCodiceFiscale,
2,
3,
$this->omocodiaPositions[1]
);
$temporaryCodiceFiscale = $this->replaceOmocodiaSection(
8,
$temporaryCodiceFiscale,
4,
7,
$this->omocodiaPositions[2]
);
$temporaryCodiceFiscale = $this->replaceOmocodiaSection(
16,
$temporaryCodiceFiscale,
8,
15,
$this->omocodiaPositions[3]
);
$temporaryCodiceFiscale = $this->replaceOmocodiaSection(
32,
$temporaryCodiceFiscale,
16,
31,
$this->omocodiaPositions[4]
);
$temporaryCodiceFiscale = $this->replaceOmocodiaSection(
64,
$temporaryCodiceFiscale,
32,
63,
$this->omocodiaPositions[5]
);
$temporaryCodiceFiscale = $this->replaceOmocodiaSection(
128,
$temporaryCodiceFiscale,
64,
127,
$this->omocodiaPositions[6]
);
}
}

Expand All @@ -178,8 +220,13 @@ private function calculateOmocodia($temporaryCodiceFiscale)
* @param $characterIndex int The index to use to make the substitutions on the $temporaryCodiceFiscale.
* @return string The temporary codice fiscale with the substitutions made.
*/
private function replaceOmocodiaSection($divider, $temporaryCodiceFiscale, $startingIndex, $endingIndex, $characterIndex)
{
private function replaceOmocodiaSection(
$divider,
$temporaryCodiceFiscale,
$startingIndex,
$endingIndex,
$characterIndex
) {
if ($this->omocodiaLevel % $divider >= $startingIndex && $this->omocodiaLevel % $divider <= $endingIndex) {
$k = $temporaryCodiceFiscale[$characterIndex];
$newChar = $this->omocodiaCodes[$k];
Expand Down
2 changes: 1 addition & 1 deletion src/Checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Checker
/**
* Constant to check for all the omocodia levels.
*/
const ALL_OMOCODIA_LEVELS = -1;
public const ALL_OMOCODIA_LEVELS = -1;

/**
* Checker constructor.
Expand Down
6 changes: 3 additions & 3 deletions src/InverseCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class InverseCalculator extends Validator
{
private $belfioreCode = null;

/**
* Create an InverseCalculator instance.
*
Expand All @@ -22,10 +22,10 @@ class InverseCalculator extends Validator
public function __construct($codiceFiscale, $properties = [])
{
parent::__construct($codiceFiscale, $properties);

if ($this->isFormallyValid()) {
$codiceFiscaleWithoutOmocodia = $this->getCodiceFiscaleWithoutOmocodia();

// calculate belfiore code
$this->belfioreCode = substr($codiceFiscaleWithoutOmocodia, 11, 4);
}
Expand Down
35 changes: 19 additions & 16 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,16 @@ class Validator extends AbstractCalculator
126 => '/^[a-z]{14}[0-9][a-z]$/i', //RSSMRAURTMLARS2W
127 => '/^[a-z]{16}$/i', //RSSMRAURTMLARSNL
);

private $codiceFiscale;
private $omocodiaAllowed = true;
private $century = null;

private $foundOmocodiaLevel = null;
private $codiceFiscaleWithoutOmocodia = null;
private $birthDate = null;
private $gender = null;

private $error = null;
private $isValid = false;

Expand All @@ -165,22 +165,22 @@ class Validator extends AbstractCalculator
public function __construct($codiceFiscale, $properties = array())
{
$this->codiceFiscale = strtoupper($codiceFiscale);

if (array_key_exists('omocodiaAllowed', $properties)) {
$this->omocodiaAllowed = $properties['omocodiaAllowed'];
}

if (array_key_exists('century', $properties)) {
$this->century = $properties['century'];
}

try {
$this->validateLength();

$this->validateFormat();

$this->validateCheckDigit();

$this->validateAndReplaceOmocodia();

$this->validateBirthDateAndGender();
Expand Down Expand Up @@ -240,7 +240,7 @@ private function validateFormat()
throw new Exception('The codice fiscale to validate has an invalid format');
}
}

/**
* Validates check digit
*
Expand All @@ -253,7 +253,7 @@ private function validateCheckDigit()
throw new Exception('The codice fiscale to validate has an invalid control character');
}
}

/**
* Validates omocodia and replace with matching chars
*
Expand Down Expand Up @@ -283,7 +283,10 @@ private function validateAndReplaceOmocodia()
*/
private function replaceOmocodiaSection($divider, $startingIndex, $endingIndex, $characterIndex)
{
if ($this->foundOmocodiaLevel % $divider >= $startingIndex && $this->foundOmocodiaLevel % $divider <= $endingIndex) {
if (
$this->foundOmocodiaLevel % $divider >= $startingIndex &&
$this->foundOmocodiaLevel % $divider <= $endingIndex
) {
$charToCheck = $this->codiceFiscaleWithoutOmocodia[$characterIndex];
if (!in_array($charToCheck, $this->omocodiaCodes)) {
throw new Exception('The codice fiscale to validate has an invalid character');
Expand All @@ -292,7 +295,7 @@ private function replaceOmocodiaSection($divider, $startingIndex, $endingIndex,
$this->codiceFiscaleWithoutOmocodia[$characterIndex] = $newChar;
}
}

/**
* Validates birthdate and gender
*
Expand All @@ -318,19 +321,19 @@ private function validateBirthDateAndGender()
if (!in_array($monthChar, $this->months)) {
throw new Exception('The codice fiscale to validate has an invalid character for birth month');
}

// calculate month, year and century
$month = array_search($monthChar, $this->months);
$year = substr($this->codiceFiscaleWithoutOmocodia, 6, 2);
$century = $this->calculateCentury($year);

// validate and calculate birth date
if (!checkdate($month, $day, $century.$year)) {
if (!checkdate($month, $day, $century . $year)) {
throw new Exception('The codice fiscale to validate has an non existent birth date');
}

$this->birthDate = new DateTime();
$this->birthDate->setDate($century.$year, $month, $day)->setTime(0, 0, 0)->format('Y-m-d');
$this->birthDate->setDate($century . $year, $month, $day)->setTime(0, 0, 0)->format('Y-m-d');
}

/**
Expand All @@ -349,7 +352,7 @@ private function calculateCentury($year)
$currentCentury = substr($currentDate->format('Y'), 0, 2);
$century = $year < $currentYear ? $currentCentury : $currentCentury - 1;
}

return $century;
}

Expand Down

0 comments on commit c1d08cd

Please sign in to comment.