Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl>
Reviewed-on: #45
This commit is contained in:
2025-10-04 11:40:52 -03:00
parent 6ddc48ec60
commit 742de657c5
815 changed files with 62089 additions and 3287 deletions

View File

@ -9,9 +9,55 @@ abstract class Banco extends Service implements Define\Cartola\Banco
{
public function process(UploadedFileInterface $file): array
{
$data = $this->parseFile($file);
$filename = $this->processUploadedFile($file);
$data = $this->processFile($filename);
return $this->mapColumns($data);
}
/**
* There are banks that need some post-processing
* @param array $movimientos
* @return array
*/
public function processMovimientosDiarios(array $movimientos): array
{
return $movimientos;
}
/**
* Move the UploadedFile into a temp file from getFilename
* @param UploadedFileInterface $uploadedFile
* @return string
*/
protected function processUploadedFile(UploadedFileInterface $uploadedFile): string
{
$filename = $this->getFilename($uploadedFile);
$uploadedFile->moveTo($filename);
return $filename;
}
/**
* Process the temp file from getFilename and remove it
* @param string $filename
* @return array
*/
protected function processFile(string $filename): array
{
$data = $this->parseFile($filename);
unlink($filename);
return $data;
}
/**
* Map columns from uploaded file data to database columns
* @param array $data
* @return array
*/
protected function mapColumns(array $data): array
{
$temp = [];
$columns = $this->columnMap();
foreach ($data as $row) {
$r = [];
foreach ($columns as $old => $new) {
@ -24,11 +70,24 @@ abstract class Banco extends Service implements Define\Cartola\Banco
}
return $temp;
}
public function processMovimientosDiarios(array $movimientos): array
{
return $movimientos;
}
/**
* Get filename where to move UploadedFile
* @param UploadedFileInterface $uploadedFile
* @return string
*/
abstract protected function getFilename(UploadedFileInterface $uploadedFile): string;
/**
* Mapping of uploaded file data columns to database columns
* @return array
*/
abstract protected function columnMap(): array;
abstract protected function parseFile(UploadedFileInterface $uploadedFile): array;
/**
* Translate uploaded file data to database data
* @param string $filename
* @return array
*/
abstract protected function parseFile(string $filename): array;
}

View File

@ -0,0 +1,18 @@
<?php
namespace Incoviba\Common\Ideal;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
abstract class LoggerEnabled implements LoggerAwareInterface
{
public LoggerInterface $logger;
public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
public function getLogger(): LoggerInterface
{
return $this->logger;
}
}

View File

@ -26,7 +26,13 @@ abstract class Model implements Define\Model
public function jsonSerialize(): mixed
{
return [
'id' => $this->id
'id' => $this->id ?? '',
...$this->jsonComplement()
];
}
protected function jsonComplement(): array
{
return [];
}
}

View File

@ -2,7 +2,9 @@
namespace Incoviba\Common\Ideal;
use PDO;
use PDOException;
use ReflectionProperty;
use ReflectionException;
use Incoviba\Common\Define;
use Incoviba\Common\Implement;
use Incoviba\Common\Implement\Exception\EmptyResult;
@ -22,10 +24,15 @@ abstract class Repository implements Define\Repository
return $this;
}
public function getConnection(): Define\Connection
{
return $this->connection;
}
public function load(array $data_row): Define\Model
{
$model = $this->create($data_row);
$model->{$this->getKey()} = $data_row[$this->getKey()];
$this->setIndex($model, $data_row[$this->getKey()]);
return $model;
}
@ -34,9 +41,12 @@ abstract class Repository implements Define\Repository
$query = $this->connection->getQueryBuilder()
->delete()->from($this->getTable())
->where("{$this->getKey()} = ?");
$this->connection->execute($query, [$model->id]);
$this->connection->execute($query, [$this->getIndex($model)]);
}
/**
* @throws EmptyResult
*/
public function fetchById(int $id): Define\Model
{
$query = $this->connection->getQueryBuilder()
@ -45,6 +55,10 @@ abstract class Repository implements Define\Repository
->where("{$this->getKey()} = ?");
return $this->fetchOne($query, [$id]);
}
/**
* @throws EmptyResult
*/
public function fetchAll(null|string|array $ordering = null): array
{
$query = $this->connection->getQueryBuilder()
@ -56,10 +70,27 @@ abstract class Repository implements Define\Repository
return $this->fetchMany($query);
}
protected string $key = 'id';
public function setKey(string $key): Repository
{
$this->key = $key;
return $this;
}
protected function getKey(): string
{
return 'id';
return $this->key;
}
protected function setIndex(Define\Model &$model, mixed $value): Repository
{
$model->{$this->getKey()} = $value;
return $this;
}
protected function getIndex(Define\Model $model): mixed
{
return $model->id;
}
protected function parseData(Define\Model $model, ?array $data, Implement\Repository\MapperParser $data_map): Define\Model
{
if ($data === null) {
@ -92,9 +123,20 @@ abstract class Repository implements Define\Repository
}
$this->setDefault($model, $property);
}
/**
* @param Define\Model $model
* @param string $property
* @return void
*/
protected function setDefault(Define\Model &$model, string $property): void
{
$prop = new ReflectionProperty($model, $property);
try {
$prop = new ReflectionProperty($model, $property);
} catch (ReflectionException) {
$model->{$property} = null;
return;
}
$type = $prop->getType()->getName();
$value = match ($type) {
'int' => 0,
@ -104,6 +146,13 @@ abstract class Repository implements Define\Repository
};
$model->{$property} = $value;
}
/**
* @param array $columns
* @param array $values
* @return int
* @throws PDOException
*/
protected function saveNew(array $columns, array $values): int
{
$query = $this->connection->getQueryBuilder()
@ -114,6 +163,14 @@ abstract class Repository implements Define\Repository
$this->connection->execute($query, $values);
return $this->connection->getPDO()->lastInsertId();
}
/**
* @param Define\Model $model
* @param array $columns
* @param array $data
* @return Define\Model
* @throws EmptyResult
*/
protected function update(Define\Model $model, array $columns, array $data): Define\Model
{
$changes = [];
@ -133,32 +190,68 @@ abstract class Repository implements Define\Repository
->update($this->getTable())
->set($columns_string)
->where("{$this->getKey()} = ?");
$values []= $model->{$this->getKey()};
$this->connection->execute($query, $values);
return $this->fetchById($model->{$this->getKey()});
$values []= $this->getIndex($model);
try {
$this->connection->execute($query, $values);
} catch (PDOException $exception) {
throw new EmptyResult($query, $exception, $data);
}
return $this->fetchById($this->getIndex($model));
}
/**
* @param string $query
* @param array|null $data
* @return Define\Model
* @throws EmptyResult
*/
protected function fetchOne(string $query, ?array $data = null): Define\Model
{
$result = $this->connection->execute($query, $data)->fetch(PDO::FETCH_ASSOC);
if ($result === false) {
throw new EmptyResult($query);
try {
$result = $this->connection->execute($query, $data)->fetch(PDO::FETCH_ASSOC);
if ($result === false) {
throw new EmptyResult($query, null, $data);
}
} catch (PDOException $exception) {
throw new EmptyResult($query, $exception, $data);
}
return $this->load($result);
}
/**
* @param string $query
* @param array|null $data
* @return array
* @throws EmptyResult
*/
protected function fetchMany(string $query, ?array $data = null): array
{
$results = $this->connection->execute($query, $data)->fetchAll(PDO::FETCH_ASSOC);
if ($results === false) {
throw new EmptyResult($query);
try {
$results = $this->connection->execute($query, $data)->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $exception) {
throw new EmptyResult($query, $exception, $data);
}
return array_map([$this, 'load'], $results);
}
/**
* @param string $query
* @param array|null $data
* @return array
* @throws EmptyResult
*/
protected function fetchAsArray(string $query, ?array $data = null): array
{
$results = $this->connection->execute($query, $data)->fetchAll(PDO::FETCH_ASSOC);
if ($results === false) {
throw new EmptyResult($query);
try {
$results = $this->connection->execute($query, $data)->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $exception) {
throw new EmptyResult($query, $exception, $data);
}
return $results;
}
public function filterData(array $data): array
{
return $data;
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace Incoviba\Common\Ideal\Service;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
use Incoviba\Exception\ServiceAction;
abstract class API extends Ideal\Service
{
public function __construct(LoggerInterface $logger)
{
parent::__construct($logger);
}
/**
* @param string|array|null $order
* @return array
*/
abstract public function getAll(null|string|array $order = null): array;
/**
* @param int $id
* @return Define\Model
* @throws ServiceAction\Read
*/
abstract public function get(int $id): Define\Model;
/**
* @param array $data
* @return Define\Model
* @throws ServiceAction\Create
*/
abstract public function add(array $data): Define\Model;
/**
* @param Define\Model $model
* @param array $new_data
* @return Define\Model
* @throws ServiceAction\Update
*/
abstract public function edit(Define\Model $model, array $new_data): Define\Model;
/**
* @param int $id
* @return Define\Model
* @throws ServiceAction\Delete
*/
abstract public function delete(int $id): Define\Model;
/**
* @param Define\Model $model
* @return Define\Model
*/
abstract protected function process(Define\Model $model): Define\Model;
}

View File

@ -0,0 +1,10 @@
<?php
namespace Incoviba\Common\Ideal\Service;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
abstract class Repository extends Ideal\Service
{
abstract public function getRepository(): Define\Repository;
}