58 lines
1.5 KiB
PHP
58 lines
1.5 KiB
PHP
<?php
|
|
namespace ProVM\KI\Common\Service;
|
|
|
|
use GuzzleHttp\Client;
|
|
use Carbon\Carbon;
|
|
|
|
class Indicadores {
|
|
//protected $cliente;
|
|
protected $base_uri;
|
|
|
|
public function __construct(string $indicadores_url) {
|
|
$this->base_uri = $indicadores_url;
|
|
}
|
|
public function get(string $indicador, \DateTime $fecha) {
|
|
$url = implode('/', [
|
|
$this->base_uri,
|
|
$indicador,
|
|
$fecha->format('Y')
|
|
]);
|
|
if ( ini_get('allow_url_fopen') ) {
|
|
$json = file_get_contents($url);
|
|
} else {
|
|
$curl = curl_init($url);
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
$json = curl_exec($curl);
|
|
curl_close($curl);
|
|
}
|
|
$data = json_decode($json);
|
|
$output = (object) [
|
|
'valor' => 0,
|
|
'fecha' => $fecha->format('d-m-Y')
|
|
];
|
|
if (isset($data->serie[0])) {
|
|
$n = 0;
|
|
foreach ($data->serie as $i => $d) {
|
|
if (Carbon::parse($d->fecha)->format('d-m-Y') == $fecha->format('d-m-Y')) {
|
|
$n = $i;
|
|
break;
|
|
}
|
|
}
|
|
$output->fecha = Carbon::parse($data->serie[$n]->fecha)->format('d-m-Y');
|
|
$output->valor = $data->serie[$n]->valor;
|
|
switch ($data->unidad_medida) {
|
|
case 'Pesos':
|
|
$output->valor = '$ ' . number_format($output->valor, 2, ',', '.');
|
|
break;
|
|
case 'Porcentaje':
|
|
$output->valor = number_format($output->valor, 2, ',', '.') . '%';
|
|
break;
|
|
case 'Dólar':
|
|
$output->valor = 'US$ ' . number_format($output->valor, 2, ',', '.');
|
|
break;
|
|
}
|
|
}
|
|
return $output;
|
|
}
|
|
}
|