Findic IPC

This commit is contained in:
Juan Pablo Vial
2025-10-25 11:23:00 -03:00
parent ab60c9db1b
commit 14d75b1ce9
8 changed files with 53 additions and 30 deletions

View File

@ -6,14 +6,17 @@ use DateTimeImmutable;
use JsonException;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Incoviba\Common\Define;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement\Exception\EmptyResponse;
use Incoviba\Service\Money;
class Findic extends Ideal\Money\Provider
{
public function __construct(protected ClientInterface $client) {}
public function __construct(LoggerInterface $logger, protected ClientInterface $client)
{
parent::__construct($logger);
}
protected string $url = 'https://findic.cl/api/';
@ -26,7 +29,7 @@ class Findic extends Ideal\Money\Provider
$dateTime = new DateTimeImmutable();
}
return match (strtolower($money_symbol)) {
Money::UF, Money::USD => $this->getMonth(strtolower($money_symbol), $dateTime),
Money::UF, Money::USD => $this->getDate(strtolower($money_symbol), $dateTime),
Money::IPC => $this->getSum(strtolower($money_symbol), $dateTime,
DateTimeImmutable::createFromFormat('Y-m-d',
$dateTime->modify('-1 year')->format('Y-11-01'))),
@ -75,7 +78,7 @@ class Findic extends Ideal\Money\Provider
* @return float
* @throws EmptyResponse
*/
protected function getMonth(string $symbol, DateTimeInterface $dateTime): float
protected function getDate(string $symbol, DateTimeInterface $dateTime): float
{
$request_uri = "{$this->supportedMap[strtolower($symbol)]}/{$dateTime->format('d-m-Y')}";
return (float) $this->sendRequest($request_uri)[0]['valor'];
@ -90,17 +93,20 @@ class Findic extends Ideal\Money\Provider
*/
protected function getSum(string $symbol, DateTimeInterface $dateTime, ?DateTimeInterface $fromDateTime = null): float
{
$requestUri = "{$this->supportedMap[strtolower($symbol)]}/{$dateTime->format('Y')})";
$json = $this->sendRequest($requestUri);
$values = array_filter($json['serie'], fn($month) => DateTimeImmutable::createFromFormat('Y-m-d', $month['fecha']) <= $dateTime);
$dateTime = $dateTime->modify('last day of this month');
$requestUri = "{$this->supportedMap[strtolower($symbol)]}/{$dateTime->format('Y')}";
$serie = $this->sendRequest($requestUri);
$values = array_filter($serie, fn($month) => DateTimeImmutable::createFromFormat('Y-m-d', $month['fecha']) <= $dateTime);
$value = array_reduce($values, fn($value, $month) => $value + ((float) $month['valor']), 0.0);
if ($fromDateTime === null) {
$fromDateTime = $dateTime->modify('-1 year');
} else {
$fromDateTime = $fromDateTime->modify('last day of this month');
}
$requestUri = "{$this->supportedMap[strtolower($symbol)]}/{$fromDateTime->format('Y')})";
$json = $this->sendRequest($requestUri);
$values = array_filter($json['serie'], fn($month) => DateTimeImmutable::createFromFormat('Y-m-d', $month['fecha']) > $dateTime);
$requestUri = "{$this->supportedMap[strtolower($symbol)]}/{$fromDateTime->format('Y')}";
$serie = $this->sendRequest($requestUri);
$values = array_filter($serie, fn($month) => DateTimeImmutable::createFromFormat('Y-m-d', $month['fecha']) > $fromDateTime);
return array_reduce($values, fn($value, $month) => $value + ((float) $month['valor']), $value);
}
}