/** * Retorna os digitos verificadores. * @return string */ public function generateVerifierDigit() { $base = new \Sped\Commons\StringHelper($this->getBaseNumber()); for ($n = 1; $n <= $this->getDigitsCount(); $n++) { $soma = 0; $mult = 2; for ($i = $base->length - 1; $i > -1; $i--) { $soma += $mult * intval($base->left($i, 1)->getValue()); if (++$mult > $this->getMaxMultiplier()) { $mult = 2; } } $base->concat($soma * 10 % 11 % 10); } return $base->right($this->getDigitsCount())->getValue(); }
/** * Validação genérica de vários tipos de números. Dentre eles: CPF, CNPJ, PIS. * @param \Sped\Commons\Documents\AbstractDocument $value Valor a ser validado. * @return boolean */ public function validate($value) { if (!$value instanceof \Sped\Commons\Documents\AbstractDocument) { throw new \Exception('O documento não é do tipo \\Sped\\Types\\AbstractDocument'); } $this->setDocument($value); $base = new \Sped\Commons\StringHelper($value->getBaseNumber()); for ($n = 1; $n <= $value->getDigitsCount(); $n++) { $soma = 0; $mult = 2; for ($i = $base->length - 1; $i > -1; $i--) { $soma += $mult * intval($base->substring($i, 1)->toString()); if (++$mult > $value->getMaxMultiplier()) { $mult = 2; } } $base->concat($soma * 10 % 11 % 10); } return $value->getValueUnmasked() === $base->toString(); }