Limpieza de input de valor y filtro de datos a nivel Repo

This commit is contained in:
Juan Pablo Vial
2024-07-03 15:13:13 -04:00
parent d5b9be0196
commit d68eba5697
28 changed files with 436 additions and 189 deletions

View File

@ -1,6 +1,7 @@
<?php
namespace Incoviba\Service\Money;
use Exception;
use DateTimeInterface;
use DateTimeImmutable;
use DateInterval;
@ -14,6 +15,10 @@ class Ine implements Provider
protected string $uri = 'https://api-calculadora.ine.cl/ServiciosCalculadoraVariacion';
public function __construct(protected ClientInterface $client) {}
/**
* @throws EmptyResponse
* @throws Exception
*/
public function get(string $money_symbol, DateTimeInterface $dateTime): float
{
$end = new DateTimeImmutable($dateTime->format('Y-m-1'));
@ -21,6 +26,9 @@ class Ine implements Provider
return $this->getVar($start, $end);
}
/**
* @throws EmptyResponse
*/
public function getVar(DateTimeInterface $start, DateTimeInterface $end): float
{
$base = 1000000000;
@ -33,9 +41,10 @@ class Ine implements Provider
];
$request_uri = implode('?', [
$this->uri,
implode('&', array_map(function($val, $key) {
http_build_query($request_query),
/*implode('&', array_map(function($val, $key) {
return "{$key}={$val}";
}, $request_query, array_keys($request_query)))
}, $request_query, array_keys($request_query)))*/
]);
try {
$response = $this->client->get($request_uri);
@ -44,6 +53,9 @@ class Ine implements Provider
}
$body = $response->getBody();
$json = json_decode($body->getContents());
if (empty($json)) {
throw new EmptyResponse($request_uri);
}
return ((int) str_replace('.', '', $json[0]->valorajustado) - $base) / $base;
}
}

View File

@ -15,6 +15,9 @@ class MiIndicador implements Provider
public function __construct(protected ClientInterface $client) {}
/**
* @throws EmptyResponse
*/
public function get(string $money_symbol, DateTimeInterface $dateTime): float
{
$request_uri = "{$money_symbol}/{$dateTime->format('d-m-Y')}";
@ -31,7 +34,7 @@ class MiIndicador implements Provider
$body = $response->getBody();
$json = json_decode($body->getContents());
if ($json->codigo !== $money_symbol or count($json->serie) === 0) {
if (empty($json) or $json->codigo !== $money_symbol or count($json->serie) === 0) {
throw new EmptyResponse($request_uri);
}
return $json->serie[0]->valor;