Auth, Login, Home, Venta->Listados->Precios
This commit is contained in:
9
app/common/Alias/View.php
Normal file
9
app/common/Alias/View.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Alias;
|
||||
|
||||
use Illuminate\Events\Dispatcher;
|
||||
use Slim\Views\Blade;
|
||||
|
||||
class View extends Blade
|
||||
{
|
||||
}
|
14
app/common/Define/Connection.php
Normal file
14
app/common/Define/Connection.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define;
|
||||
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
|
||||
interface Connection
|
||||
{
|
||||
public function connect(): Connection;
|
||||
public function query(string $query): PDOStatement;
|
||||
public function prepare(string $query): PDOStatement;
|
||||
public function execute(string $query, ?array $data = null): PDOStatement;
|
||||
public function getPDO(): PDO;
|
||||
}
|
8
app/common/Define/Database.php
Normal file
8
app/common/Define/Database.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define;
|
||||
|
||||
interface Database
|
||||
{
|
||||
public function getDSN(): string;
|
||||
public function needsUser(): bool;
|
||||
}
|
8
app/common/Define/Model.php
Normal file
8
app/common/Define/Model.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define;
|
||||
|
||||
use JsonSerializable;
|
||||
|
||||
interface Model extends JsonSerializable
|
||||
{
|
||||
}
|
11
app/common/Define/Repository.php
Normal file
11
app/common/Define/Repository.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define;
|
||||
|
||||
interface Repository
|
||||
{
|
||||
public function create(?array $data = null): Model;
|
||||
public function save(Model $model): Model;
|
||||
public function load(array $data_row): Model;
|
||||
public function edit(Model $model, array $new_data): Model;
|
||||
public function remove(Model $model): void;
|
||||
}
|
16
app/common/Ideal/Model.php
Normal file
16
app/common/Ideal/Model.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Ideal;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
|
||||
abstract class Model implements Define\Model
|
||||
{
|
||||
public int $id;
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return [
|
||||
'id' => $this->id
|
||||
];
|
||||
}
|
||||
}
|
126
app/common/Ideal/Repository.php
Normal file
126
app/common/Ideal/Repository.php
Normal file
@ -0,0 +1,126 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Ideal;
|
||||
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use PDO;
|
||||
use Incoviba\Common\Define\Model;
|
||||
use Incoviba\Common\Define;
|
||||
|
||||
abstract class Repository implements Define\Repository
|
||||
{
|
||||
public function __construct(protected Define\Connection $connection) {}
|
||||
|
||||
protected string $table;
|
||||
public function getTable(): string
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
public function setTable(string $table): Repository
|
||||
{
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function load(array $data_row): Model
|
||||
{
|
||||
$model = $this->create($data_row);
|
||||
$model->{$this->getKey()} = $data_row[$this->getKey()];
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function remove(Model $model): void
|
||||
{
|
||||
$query = "DELETE FROM `{$this->getTable()}` WHERE `{$this->getKey()}` = ?";
|
||||
$this->connection->execute($query, [$model->getId()]);
|
||||
}
|
||||
|
||||
public function fetchById(int $id): Define\Model
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}` WHERE `{$this->getKey()}` = ?";
|
||||
return $this->fetchOne($query, [$id]);
|
||||
}
|
||||
public function fetchAll(): array
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}`";
|
||||
return $this->fetchMany($query);
|
||||
}
|
||||
|
||||
protected function getKey(): string
|
||||
{
|
||||
return 'id';
|
||||
}
|
||||
protected function parseData(Define\Model $model, ?array $data, array $data_map): Define\Model
|
||||
{
|
||||
if ($data === null) {
|
||||
return $model;
|
||||
}
|
||||
foreach ($data_map as $column => $settings) {
|
||||
if (isset($data[$column])) {
|
||||
$property = $column;
|
||||
if (isset($settings['property'])) {
|
||||
$property = $settings['property'];
|
||||
}
|
||||
$value = $data[$column];
|
||||
if (isset($settings['function'])) {
|
||||
$value = $settings['function']($data);
|
||||
}
|
||||
$model->{$property} = $value;
|
||||
}
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
protected function saveNew(array $columns, array $values): int
|
||||
{
|
||||
$columns_string = implode(', ', array_map(function($column) {return "`{$column}`";}, $columns));
|
||||
$columns_questions = implode(', ', array_fill(0, count($columns), '?'));
|
||||
$query = "INSERT INTO `{$this->getTable()}` ({$columns_string}) VALUES ($columns_questions)";
|
||||
$this->connection->execute($query, $values);
|
||||
return $this->connection->getPDO()->lastInsertId();
|
||||
}
|
||||
protected function update(Model $model, array $columns, array $data): Define\Model
|
||||
{
|
||||
$changes = [];
|
||||
$values = [];
|
||||
foreach ($columns as $column) {
|
||||
if (isset($data[$column])) {
|
||||
$changes []= $column;
|
||||
$values []= $data[$column];
|
||||
}
|
||||
}
|
||||
if (count($changes) === 0) {
|
||||
return $model;
|
||||
}
|
||||
$columns_string = implode(', ', array_map(function($property) {return "`{$property}` = ?";}, $changes));
|
||||
$query = "UPDATE `{$this->getTable()}` SET {$columns_string} WHERE `{$this->getKey()}` = ?";
|
||||
$values []= $model->id;
|
||||
$this->connection->execute($query, $values);
|
||||
$id = $model->id;
|
||||
$model = $this->create($data);
|
||||
$model->id = $id;
|
||||
return $model;
|
||||
}
|
||||
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);
|
||||
}
|
||||
return $this->load($result);
|
||||
}
|
||||
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);
|
||||
}
|
||||
return array_map([$this, 'load'], $results);
|
||||
}
|
||||
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);
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
}
|
61
app/common/Implement/Connection.php
Normal file
61
app/common/Implement/Connection.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement;
|
||||
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
use PDOException;
|
||||
use Incoviba\Common\Define;
|
||||
|
||||
class Connection implements Define\Connection
|
||||
{
|
||||
public function __construct(protected Define\Database $database) {}
|
||||
|
||||
protected PDO $connection;
|
||||
public function connect(): Define\Connection
|
||||
{
|
||||
if (!isset($this->connection)) {
|
||||
if ($this->database->needsUser()) {
|
||||
$this->connection = new PDO($this->database->getDSN(), $this->database->user, $this->database->password);
|
||||
} else {
|
||||
$this->connection = new PDO($this->database->getDSN());
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function getPDO(): PDO
|
||||
{
|
||||
$this->connect();
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
public function query(string $query): PDOStatement
|
||||
{
|
||||
$this->connect();
|
||||
$statement = $this->connection->query($query);
|
||||
if ($statement === false) {
|
||||
throw new PDOException("Query failed: '{$query}'");
|
||||
}
|
||||
return $statement;
|
||||
}
|
||||
public function prepare(string $query): PDOStatement
|
||||
{
|
||||
$this->connect();
|
||||
$statement = $this->connection->prepare($query);
|
||||
if ($statement === false) {
|
||||
throw new PDOException("Query failed: '{$query}'");
|
||||
}
|
||||
return $statement;
|
||||
}
|
||||
public function execute(string $query, ?array $data = null): PDOStatement
|
||||
{
|
||||
if ($data === null) {
|
||||
return $this->query($query);
|
||||
}
|
||||
$statement = $this->prepare($query);
|
||||
$status = $statement->execute($data);
|
||||
if ($status === false) {
|
||||
throw new PDOException("Query could not be executed: '{$query}'");
|
||||
}
|
||||
return $statement;
|
||||
}
|
||||
}
|
24
app/common/Implement/Database/MySQL.php
Normal file
24
app/common/Implement/Database/MySQL.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Database;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
|
||||
class MySQL implements Define\Database
|
||||
{
|
||||
public function __construct(public string $host, public string $name, public string $user, public string $password) {}
|
||||
|
||||
public int $port = 3306;
|
||||
|
||||
public function getDSN(): string
|
||||
{
|
||||
$dsn = "mysql:host={$this->host};dbname={$this->name}";
|
||||
if ($this->port !== 3306) {
|
||||
$dsn .= ";port={$this->port}";
|
||||
}
|
||||
return $dsn;
|
||||
}
|
||||
public function needsUser(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
15
app/common/Implement/Exception/EmptyResult.php
Normal file
15
app/common/Implement/Exception/EmptyResult.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Exception;
|
||||
|
||||
use Exception;
|
||||
use Throwable;
|
||||
|
||||
class EmptyResult extends Exception
|
||||
{
|
||||
public function __construct(string $query, ?Throwable $previous = null)
|
||||
{
|
||||
$message = "Empty results for {$query}";
|
||||
$code = 700;
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user