You are here

public function CpfService::isValid in CPF 8

Checks if the value you entered is a valid CPF number.

Parameters

string $value: The value of the CPF number.

Return value

bool Returns TRUE if the value entered is a valid CPF number. Otherwise, returns FALSE.

File

src/CpfService.php, line 20

Class

CpfService
Defines the CpfService service, for CPF module.

Namespace

Drupal\cpf

Code

public function isValid($value) {
  $invalids = [
    '00000000000',
    '11111111111',
    '22222222222',
    '33333333333',
    '44444444444',
    '55555555555',
    '66666666666',
    '77777777777',
    '88888888888',
    '99999999999',
  ];
  $value = $this
    ->digits($value);
  if (strlen($value) != 11 || in_array($value, $invalids)) {
    return FALSE;
  }
  else {
    for ($t = 9; $t < 11; $t++) {
      for ($d = 0, $c = 0; $c < $t; $c++) {
        $d += $value[$c] * ($t + 1 - $c);
      }
      $d = 10 * $d % 11 % 10;
      if ($value[$c] != $d) {
        return FALSE;
      }
    }
  }
  return TRUE;
}