Skip to content

Commit

Permalink
Merge pull request #9 from DenysXavier/6--calcular-digito-do-nosso-nu…
Browse files Browse the repository at this point in the history
…mero

Fix: #6 - Calcular dígito do nosso número
  • Loading branch information
DenysXavier authored Jul 17, 2017
2 parents 8bb6094 + b522b5c commit 04bf181
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ php:
- '5.6'
- '7.0'
- '7.1'
- hhvm
install: composer install
after_script:
- php vendor/bin/coveralls -v
36 changes: 35 additions & 1 deletion src/Titulo.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ public function getNossoNumero() {
return $this->nossoNumero;
}

/** Obtém o número do título no banco com seu dígito verificador
*
* @return string
*/
public function getNossoNumeroComDigito() {
return $this->nossoNumero . $this->calcularDigitoVerificador($this->nossoNumero);
}

/** Obtém o número do Título no cliente.
*
* @return string
Expand Down Expand Up @@ -230,14 +238,40 @@ public function setMensagem($mensagem) {
return $this;
}

/** Calcula o dígito do campo nosso número
*
* return int
*/
private function calcularDigitoVerificador($numero) {
$digito = 0;
$multiplicador = 2;
$total = 0;
$algarismosInvertidos = array_reverse(str_split($numero));

foreach ($algarismosInvertidos as $algarismo) {
$total += $multiplicador * $algarismo;

if (++$multiplicador > 9) {
$multiplicador = 2;
}
}

$modulo = $total % 11;
if ($modulo > 1) {
$digito = 11 - $modulo;
}

return $digito;
}

/** Exporta um array associativo no qual as chaves são as propriedades representadas como no WebService do Santander
*
* @return array
*/
public function exportarArray() {
$formatoDataPadrao = Config::getInstance()->getGeral("formato_data");

$array["TITULO.NOSSO-NUMERO"] = $this->getNossoNumero();
$array["TITULO.NOSSO-NUMERO"] = $this->getNossoNumeroComDigito();
$array["TITULO.SEU-NUMERO"] = $this->getSeuNumero();
$array["TITULO.DT-VENCTO"] = $this->getDataVencimento()->format($formatoDataPadrao);
$array["TITULO.DT-EMISSAO"] = $this->getDataEmissao()->format($formatoDataPadrao);
Expand Down
50 changes: 49 additions & 1 deletion tests/TituloTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function setUpBeforeClass() {
*/
public function osValoresPadroesParaDataDeEmissaoEDataDeVencimentoSaoOProprioDia() {
$obj = new Titulo();

$dataEsperada = new \DateTime();

$this->assertEquals($dataEsperada->format("Y-m-d"), $obj->getDataEmissao()->format("Y-m-d"));
Expand Down Expand Up @@ -160,4 +160,52 @@ public function oArrayExportadoDevePossuirAsMesmasChavesUtilizadasPeloWSdoBanco(
}
}

/**
* @author Denys Xavier <[email protected]>
* @test
*/
public function calculoDeDigitoVerificadorDeNossoNumeroComMenosDe8Algarismos() {
$nossoNumero = 12345;
$nossoNumeroComDigito = 123455;

$titulo = new Titulo();
$titulo->setNossoNumero($nossoNumero);

$this->assertEquals($nossoNumeroComDigito, $titulo->getNossoNumeroComDigito());
}

/**
* @author Denys Xavier <[email protected]>
* @test
*/
public function calculoDeDigitoVerificadorDeNossoNumeroComMaisDe8Algarismos() {
$nossoNumero = 123456789012;
$nossoNumeroComDigito = 1234567890123;

$titulo = new Titulo();
$titulo->setNossoNumero($nossoNumero);

$this->assertEquals($nossoNumeroComDigito, $titulo->getNossoNumeroComDigito());
}

/**
* @author Denys Xavier <[email protected]>
* @test
*/
public function sempreQueOModulo11DaSomatoriaDosAlgarismosForMenorQue1EntaoDeveRetornarZero() {
$titulo = new Titulo();

$nossoNumeroModulo11Igual0 = 2023;
$nossoNumeroModulo11Igual0ComDigito = 20230;

$titulo->setNossoNumero($nossoNumeroModulo11Igual0);
$this->assertEquals($nossoNumeroModulo11Igual0ComDigito, $titulo->getNossoNumeroComDigito());

$nossoNumeroModulo11Igual1 = 2001;
$nossoNumeroModulo11Igual1ComDigito = 20010;

$titulo->setNossoNumero($nossoNumeroModulo11Igual1);
$this->assertEquals($nossoNumeroModulo11Igual1ComDigito, $titulo->getNossoNumeroComDigito());
}

}

0 comments on commit 04bf181

Please sign in to comment.