Files
oficial/app/src/Repository/UF.php
Juan Pablo Vial ea8f483dd5 FIX: Repository UF
2025-05-05 18:13:55 -04:00

65 lines
1.7 KiB
PHP

<?php
namespace Incoviba\Repository;
use DateTimeInterface;
use DateTimeImmutable;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
use Incoviba\Common\Ideal\Repository;
use Incoviba\Common\Implement;
use Incoviba\Model;
class UF extends Ideal\Repository
{
public function getTable(): string
{
return 'uf';
}
protected function getIndex(Define\Model $model): string
{
return 'fecha';
}
protected function setIndex(Define\Model &$model, mixed $value): Repository
{
$model->fecha = new DateTimeImmutable($value);
return $this;
}
protected function getKey(): string
{
return 'fecha';
}
public function create(?array $data = null): Model\UF
{
$map = (new Implement\Repository\MapperParser(['valor']))
->register('fecha', new Implement\Repository\Mapper\DateTime('fecha'));
return $this->parseData(new Model\UF(), $data, $map);
}
public function save(Define\Model $model): Model\UF
{
$this->saveNew(['fecha', 'valor'], [
$model->fecha->format('Y-m-d'),
$model->valor
]);
return $model;
}
public function edit(Define\Model $model, array $new_data): Model\UF
{
return $this->update($model, ['fecha', 'valor'], $new_data);
}
/**
* @param DateTimeInterface $dateTime
* @return Model\UF
* @throws Implement\Exception\EmptyResult
*/
public function fetchByFecha(DateTimeInterface $dateTime): Model\UF
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('fecha = :fecha');
return $this->fetchOne($query, ['fecha' => $dateTime->format('Y-m-d')]);
}
}