Version 3.0

New technologies
This commit is contained in:
2022-08-05 21:28:59 -04:00
parent 06071884c7
commit a9968dec58
69 changed files with 600 additions and 2696 deletions

View File

@ -0,0 +1,151 @@
<?php
namespace Common\Alias;
use Common\Concept\Database as DatabaseInterface;
abstract class Database implements DatabaseInterface
{
protected string $host;
protected int $port;
public function setHost(string $host, ?int $port = null): DatabaseInterface
{
$this->host = $host;
if ($port !== null) {
$this->port = $port;
}
return $this;
}
public function getHost(): string
{
return $this->host;
}
public function getPort(): int
{
return $this->port;
}
protected string $name;
public function setName(string $database_name): Database
{
$this->name = $database_name;
return $this;
}
public function getName(): string
{
return $this->name;
}
protected string $username;
protected string $password;
public function setUser(string $username, string $password): Database
{
$this->username = $username;
$this->password = $password;
return $this;
}
public function getUser(): string
{
return $this->username;
}
public function getPassword(): string
{
return $this->password;
}
protected \PDO $connection;
public function connect(): Database
{
if ($this->needsUser()) {
$this->connection = new \PDO($this->getDsn(), $this->getUser(), $this->getPassword());
return $this;
}
$this->connection = new \PDO($this->getDsn());
return $this;
}
public function getConnection(): \PDO
{
if (!isset($this->connection)) {
return $this->connect()->connection;
}
return $this->connection;
}
public function query(string $query): array
{
$st = $this->getConnection()->query($query);
if (!$st) {
throw new \PDOException("Could not retrieve anything with '{$query}'.");
}
$results = $st->fetchAll(\PDO::FETCH_ASSOC);
if (!$results) {
throw new \PDOException('Could not retrieve any results.');
}
return $results;
}
public function beginTransaction(): void
{
if (!$this->getConnection()->beginTransaction()) {
throw new \PDOException('Could not begin transaction.');
}
}
public function commit(): void
{
if (!$this->getConnection()->commit()) {
throw new \PDOException('Could not commit');
}
}
public function rollBack(): void
{
if (!$this->getConnection()->rollBack()) {
throw new \PDOException('Could not rollback.');
}
}
protected \PDOStatement $prepared_statement;
public function prepare(string $query): Database
{
$st = $this->getConnection()->prepare($query);
if (!$st) {
throw new \PDOException("Could not prepare query '{$query}'.");
}
$this->prepared_statement = $st;
return $this;
}
public function execute(array $data): array
{
if (!isset($this->prepared_statement)) {
throw new \Exception('No prepared statement.');
}
if (!$this->prepared_statement->execute($data)) {
throw new \PDOException('Could not execute prepared statement.');
}
$results = $this->prepared_statement->fetchAll(\PDO::FETCH_ASSOC);
if (!$results) {
throw new \PDOException('Could not retrieve any results.');
}
return $results;
}
public function insert(array $values): void
{
if (!isset($this->prepared_statement)) {
throw new \Exception('No prepared statement.');
}
if (!$this->prepared_statement->execute($values)) {
throw new \PDOException('Could not insert.');
}
}
public function update(array $data): void
{
if (!isset($this->prepared_statement)) {
throw new \Exception('No prepared statement.');
}
if (!$this->prepared_statement->execute($data)) {
throw new \PDOException('Could not update.');
}
}
public function delete(array $data): void
{
if (!isset($this->prepared_statement)) {
throw new \Exception('No prepared statement.');
}
if (!$this->prepared_statement->execute($data)) {
throw new \PDOException('Could not delete.');
}
}
}

View File

@ -1,6 +0,0 @@
<?php
namespace Contabilidad\Common\Alias;
interface DocumentHandler {
public function load(): ?array;
}

View File

@ -0,0 +1,26 @@
<?php
namespace Common\Alias\Factory;
use Common\Concept\Factory\Model as FactoryInterface;
use Common\Concept\Model as ModelInterface;
use Common\Concept\Repository;
use Psr\Container\ContainerInterface;
abstract class Model implements FactoryInterface
{
protected ContainerInterface $container;
public function setContainer(ContainerInterface $container): Model
{
$this->container = $container;
return $this;
}
public function getContainer(): ContainerInterface
{
return $this->container;
}
public function find(ModelInterface $model_name): Repository
{
$class = str_replace('Model', 'Repository', get_class($model_name));
return $this->getContainer()->get($class);
}
}

46
api/common/Alias/File.php Normal file
View File

@ -0,0 +1,46 @@
<?php
namespace Common\Alias;
use function Safe\{fopen,fclose,fwrite};
use Common\Concept\File as FileInterface;
abstract class File implements FileInterface
{
protected string $filename;
public function setFilename(string $filename): FileInterface
{
$this->filename = $filename;
return $this;
}
public function getFilename(): string
{
return $this->filename;
}
public function isDir(): bool
{
return is_dir($this->getFilename());
}
public function isReadable(): bool
{
return is_readable($this->getFilename());
}
public function isWriteable(): bool
{
return is_writeable($this->getFilename());
}
public function read(?int $length = null): string
{
$fh = fopen($this->getFilename(), 'r');
$str = fgets($fh, $length);
fclose($fh);
return $str;
}
public function write(string $data, ?int $length = null): void
{
$fh = fopen($this->getFilename(), 'r');
fwrite($fh, $data, $length);
fclose($fh);
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace Common\Alias;
use function Safe\{touch,mkdir,unlink};
use Common\Concept\Filesystem as FilesystemInterface;
abstract class Filesystem implements FilesystemInterface
{
protected string $base_path;
public function setBasePath(string $path): FilesystemInterface
{
$this->base_path = $path;
return $this;
}
public function getBasePath(): string
{
return $this->base_path;
}
public function buildPath(string $relative_path): string
{
return implode(DIRECTORY_SEPARATOR, [
$this->getBasePath(),
$relative_path
]);
}
public function has(string $relative_path): bool
{
return file_exists($this->buildPath($relative_path));
}
public function mkdir(string $relative_path): void
{
mkdir($this->buildPath($relative_path));
}
public function create(string $relative_path): void
{
touch($this->buildPath($relative_path));
}
public function delete(string $relative_path): void
{
unlink($this->buildPath($relative_path));
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace Common\Alias;
use Common\Concept\Model as ModelInterface;
abstract class Model implements ModelInterface
{
}

View File

@ -0,0 +1,62 @@
<?php
namespace Common\Alias;
use Common\Concept\Database;
use Common\Concept\Model;
use Common\Concept\Repository as RepositoryInterface;
abstract class Repository implements RepositoryInterface
{
protected string $table;
public function setTable(string $table): RepositoryInterface
{
$this->table = $table;
return $this;
}
public function getTable(): string
{
return $this->table;
}
protected array $properties;
public function addProperty(string $name): RepositoryInterface
{
$this->properties []= $name;
return $this;
}
public function setProperties(array $properties): RepositoryInterface
{
foreach ($properties as $property) {
$this->addProperty($property);
}
return $this;
}
public function getProperties(): array
{
return $this->properties;
}
protected Database $database;
public function setDatabase(Database $database): RepositoryInterface
{
$this->database = $database;
return $this;
}
public function getDatabase(): Database
{
return $this->database;
}
public function fetchAll(): array
{
$query = "SELECT * FROM {$this->getTable()}";
return array_map([$this, 'load'], $this->getDatabase()->query($query));
}
public function save(Model $model): void
{
$columns = implode(', ', array_map(function($item) {return "'{$item}'";}, $this->getProperties()));
$values = implode(', ', array_map(function($item) {return '?';}, $this->getProperties()));
$query = "INSERT INTO {$this->getTable()} ({$columns}) VALUES ({$values})";
$values = array_map(function($item) use ($model) {$method = str_replace(' ', '', ucwords(str_replace('_', '', $item)));return $model->{"get{$method}"}();}, $this->getProperties());
$this->getDatabase()->prepare($query)->insert($values);
}
}