This commit is contained in:
Juan Pablo Vial
2025-03-03 14:57:22 -03:00
parent d165440483
commit 8f16f33a1e
56 changed files with 749 additions and 105 deletions

View File

@ -3,13 +3,42 @@ namespace Incoviba\Common\Define;
use PDO;
use PDOStatement;
use PDOException;
interface Connection
{
/**
* @return Connection
* @throws PDOException
*/
public function connect(): Connection;
/**
* @param string $query
* @return PDOStatement
* @throws PDOException
*/
public function query(string $query): PDOStatement;
/**
* @param string $query
* @return PDOStatement
* @throws PDOException
*/
public function prepare(string $query): PDOStatement;
/**
* @param string $query
* @param array|null $data
* @return PDOStatement
* @throws PDOException
*/
public function execute(string $query, ?array $data = null): PDOStatement;
/**
* @return PDO
* @throws PDOException
*/
public function getPDO(): PDO;
public function getQueryBuilder(): Query\Builder;
}

View File

@ -2,8 +2,15 @@
namespace Incoviba\Common\Define\Money;
use DateTimeInterface;
use Incoviba\Common\Implement\Exception\EmptyResponse;
interface Provider
{
/**
* @param string $money_symbol
* @param DateTimeInterface $dateTime
* @return float
* @throws EmptyResponse
*/
public function get(string $money_symbol, DateTimeInterface $dateTime): float;
}

View File

@ -1,11 +1,42 @@
<?php
namespace Incoviba\Common\Define;
use Incoviba\Common\Implement\Exception\EmptyResult;
use PDOException;
interface Repository
{
/**
* @param array|null $data
* @return Model
*/
public function create(?array $data = null): Model;
/**
* @param Model $model
* @return Model
* @throws PDOException
*/
public function save(Model $model): Model;
/**
* @param array $data_row
* @return Model
*/
public function load(array $data_row): Model;
/**
* @param Model $model
* @param array $new_data
* @return Model
* @throws EmptyResult
*/
public function edit(Model $model, array $new_data): Model;
/**
* @param Model $model
* @return void
* @throws PDOException
*/
public function remove(Model $model): void;
}

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;
@ -116,9 +118,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,
@ -128,6 +141,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()
@ -138,6 +158,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 = [];
@ -158,30 +186,59 @@ abstract class Repository implements Define\Repository
->set($columns_string)
->where("{$this->getKey()} = ?");
$values []= $this->getIndex($model);
$this->connection->execute($query, $values);
try {
$this->connection->execute($query, $values);
} catch (PDOException $exception) {
throw new EmptyResult($query, $exception);
}
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);
} catch (PDOException $exception) {
throw new EmptyResult($query, $exception);
}
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);
}
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);
}
return $results;
}

View File

@ -22,6 +22,7 @@ class Connection implements Define\Connection
}
return $this;
}
public function getPDO(): PDO
{
$this->connect();