Skip to content

Commit

Permalink
agregada función de validación de digito de verificación
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Vidal committed Mar 28, 2019
1 parent c5efad2 commit 8df5e4f
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/DNI.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,60 @@ public function get($dni, $asArray = false)
}
}

public function validateDigit($dni, $digit)
{
return $this->validate($dni) && $this->validateHash($dni, $digit);
}

protected function validate($value)
{
if (is_numeric($value)) {
return strlen($value) == 8;
}
return false;
}

protected function validateHash($v, $h)
{
$identificationDocument = $v . '0';
$addition = 0;
$hash = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2 ];
$identificationDocumentLength = strlen($identificationDocument);

$identificationComponent = substr($identificationDocument, 0, $identificationDocumentLength - 1);

$identificationComponentLength = strlen($identificationComponent);

$diff = count($hash) - $identificationComponentLength;

for ($i = $identificationComponentLength - 1; $i >= 0; $i--)
{
$addition += ($identificationComponent[$i] - '0') * $hash[$i + $diff];
}

$addition = 11 - ($addition % 11);

if ($addition == 11)
{
$addition = 0;
}

$last = $identificationDocument[$identificationDocumentLength - 1];

$hashValue = '';

if (ctype_alpha($last))
{
$hashLetters = [ 'K', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' ];
$hashValue = $hashLetters[$addition];
}

else if (is_numeric($last))
{
$hashNumbers = [ '6', '7', '8', '9', '0', '1', '1', '2', '3', '4', '5' ];
$hashValue = $hashNumbers[$addition];
}

return $h == $hashValue;
}
}

0 comments on commit 8df5e4f

Please sign in to comment.