Skip to content
This repository has been archived by the owner on Oct 2, 2022. It is now read-only.

Commit

Permalink
alteracao na chamada da classe indices e criação de exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusvalbert committed Sep 9, 2022
1 parent ada0bf2 commit 6699dd6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 30 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@ Para obter o código dos índices, basta verificar no [Link](https://www3.bcb.go
## Utilização

```php
$indice = new Indices();

$indice
->codigoSerie(189)
Indices::codigoSerie(189)
->numeroMeses(12)
->get();

$indice
->codigoSerie(189)
Indices::codigoSerie(189)
->dataInicioFim('01/01/2020', '01/01/2022')
->get();
```
Expand Down
23 changes: 17 additions & 6 deletions src/Indices.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,26 @@ class Indices
* @param int $codigoSerie
* @return $this
*/
public function codigoSerie(int $codigoSerie): self
public static function codigoSerie(int $codigoSerie): self
{
$this->codigoSerie = $codigoSerie;
$indices = new self();
$indices->codigoSerie = $codigoSerie;

return $this;
return $indices;
}

/**
* Set data inicial e final da busca
* @param string $inicial
* @param string $final
* @return $this
* @throws Exception
*/
public function dataInicioFim(string $inicial, string $final): self
{
unset($this->ultimos);
if (isset($this->ultimos)) {
throw new chamadaFuncaoException();
}
$this->dataInicial = $inicial;
$this->dataFinal = $final;

Expand All @@ -63,11 +67,13 @@ public function dataInicioFim(string $inicial, string $final): self
* Set número últimos meses
* @param int $meses
* @return $this
* @throws Exception
*/
public function numeroMeses(int $meses): self
{
unset($this->dataInicial);
unset($this->dataFinal);
if (isset($this->dataInicial) || isset($this->dataFinal)) {
throw new chamadaFuncaoException();
}
$this->ultimos = $meses;

return $this;
Expand Down Expand Up @@ -99,9 +105,14 @@ private function calculaInflacaoAcumulada(string $valores): float
* Realiza o request para a API do Banco Central
* @param bool $inflacaoAcumulada
* @return array|Exception[]|GuzzleException[]
* @throws Exception
*/
public function get(bool $inflacaoAcumulada = true): array
{
if (! isset($this->ultimos) && ! (isset($this->dataInicial) && isset($this->dataFinal))) {
throw new Exception('É necessário chamar a classe numeroMeses ou dataInicioFim antes de chamar o get');
}

$client = new Client();

try {
Expand Down
24 changes: 24 additions & 0 deletions src/chamadaFuncaoException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Valbert\IndicesCorrecao;

use Throwable;

class chamadaFuncaoException extends \Exception
{
/**
* Mensagem de erro Exception
* @var string
*/
protected $message = 'Não é permitido chamar a função numeroMeses e dataInicioFim em conjunto.';

/**
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
{
parent::__construct($this->message, $code, $previous);
}
}
44 changes: 26 additions & 18 deletions tests/IndicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,41 @@
use Valbert\IndicesCorrecao\Indices;

/** @test */
it('Número de messes', function () {
$indice = new Indices();
it('Erro na chamada numeroMeses e dataInicioFim em conjuto', function () {
Indices::codigoSerie(189)
->numeroMeses(12)
->dataInicioFim('01/01/2020', '01/01/2022')
->get();
})->expectException(\Valbert\IndicesCorrecao\chamadaFuncaoException::class);

/** @test */
it('Erro ao chamar get sem chamar numeroMeses ou dataInicioFim', function () {
Indices::codigoSerie(189)
->get();
})->expectException(Exception::class);

$ind = $indice
->codigoSerie(189)
/** @test */
it('Número de messes', function () {
$indice = Indices::codigoSerie(189)
->numeroMeses(12)
->get();

expect($ind['inflacao'])->toBeFloat()
->and($ind['inflacao'])->toBeLessThan(1)
->and($ind['inflacao'])->toBeGreaterThanOrEqual(0)
->and($ind['indices'])->toBeString()
->and($ind)->toBeArray();
expect($indice['inflacao'])->toBeFloat()
->and($indice['inflacao'])->toBeLessThan(1)
->and($indice['inflacao'])->toBeGreaterThanOrEqual(0)
->and($indice['indices'])->toBeString()
->and($indice)->toBeArray();
});

/** @test */
it('Período início e fim', function () {
$indice = new Indices();

$ind = $indice
->codigoSerie(189)
$indice = Indices::codigoSerie(189)
->dataInicioFim('01/01/2020', '01/01/2022')
->get();

expect($ind['inflacao'])->toBeFloat()
->and($ind['inflacao'])->toBeLessThan(1)
->and($ind['inflacao'])->toBeGreaterThanOrEqual(0)
->and($ind['indices'])->toBeString()
->and($ind)->toBeArray();
expect($indice['inflacao'])->toBeFloat()
->and($indice['inflacao'])->toBeLessThan(1)
->and($indice['inflacao'])->toBeGreaterThanOrEqual(0)
->and($indice['indices'])->toBeString()
->and($indice)->toBeArray();
});

0 comments on commit 6699dd6

Please sign in to comment.