25 Commits

Author SHA1 Message Date
3bc54fb9d1 Second version 2022-09-08 21:42:32 -04:00
123d46d33c Fixed Model joins 2021-12-20 23:34:52 -03:00
65c224c636 Merge branch 'develop' into master 2021-08-01 20:51:44 -04:00
6cd26a88ea Merge branch 'develop' into master 2021-08-01 20:34:04 -04:00
022ba575b7 Merge branch 'develop' into master 2021-06-04 21:15:26 -04:00
c913f65b91 Considera leer el dato y correccion en los nombres de los metodos 2021-03-16 00:22:13 -03:00
c8a7781c88 Merge branch 'develop' 2021-03-15 11:17:11 -03:00
1505539e61 Merge branch 'develop' into master 2021-03-09 00:19:55 -03:00
3087a48c43 Merge branch 'develop' into master 2021-03-09 00:15:12 -03:00
43f545516d Merge branch 'develop' 2020-08-04 01:33:09 -04:00
b757ed19b2 Merge branch 'develop' 2020-08-03 23:54:16 -04:00
9dc71e4d77 Merge branch 'develop' 2020-08-03 23:51:05 -04:00
c6806a1c62 Merge branch 'develop' 2020-08-03 23:42:11 -04:00
6fd19a11be Merge branch 'develop' 2020-08-03 23:38:41 -04:00
7c727d93e9 Merge branch 'develop' 2020-08-03 23:27:45 -04:00
e02b8c4063 Merge branch 'develop' 2020-08-03 23:17:38 -04:00
89d1db7a7e Merge branch 'develop' 2020-08-03 23:10:57 -04:00
8dc0a27fd9 Merge branch 'develop' 2020-08-03 22:07:24 -04:00
ae172b902c Merge branch 'develop' 2020-08-03 16:25:19 -04:00
7f81b987c9 Merge branch 'develop' 2020-07-24 12:06:16 -04:00
af801e769f Merge branch 'develop' 2020-07-24 11:58:16 -04:00
c40baaad3f Merge branch 'develop' 2020-07-24 11:47:39 -04:00
a82fdce64b Merge branch 'develop' 2020-07-22 23:08:52 -04:00
8126b1f67d Merge branch 'develop' 2020-07-22 22:57:48 -04:00
2177cb4652 Merge branch 'develop' 2020-07-22 14:08:09 -04:00
6 changed files with 121 additions and 318 deletions

View File

@ -1,11 +1,21 @@
<?php <?php
namespace ProVM\Alias; namespace ProVM\Alias;
use ProVM\Concept\Model\Repository; use ProVM\Concept\Model\Factory;
use ProVM\Concept\Model as ModelInterface; use ProVM\Concept\Model as ModelInterface;
abstract class Model implements ModelInterface abstract class Model implements ModelInterface
{ {
protected Factory $factory;
public function setFactory(Factory $factory): ModelInterface
{
$this->factory = $factory;
return $this;
}
public function getFactory(): Factory
{
return $this->factory;
}
protected int $id; protected int $id;
public function setId(int $id): ModelInterface public function setId(int $id): ModelInterface
{ {
@ -16,4 +26,39 @@ abstract class Model implements ModelInterface
{ {
return $this->id; return $this->id;
} }
protected bool $new;
public function setNew(): ModelInterface
{
$this->new = true;
return $this;
}
public function isNew(): bool
{
return $this->new ?? false;
}
protected bool $dirty;
public function setDirty(): ModelInterface
{
$this->dirty = true;
return $this;
}
public function isDirty(): bool
{
return $this->dirty ?? $this->isNew();
}
public function save(): void
{
if ($this->isDirty()) {
$this->getFactory()->get(get_class($this))->save($this);
}
}
public function edit(array $data): void
{
foreach ($data as $key => $val) {
$m = 'set' . ucwords($key);
$this->{$m}($val);
}
$this->isDirty();
}
} }

View File

@ -1,13 +1,11 @@
<?php <?php
namespace ProVM\Alias\Model; namespace ProVM\Alias\Model;
use PDOException;
use ProVM\Concept\Database\Connection; use ProVM\Concept\Database\Connection;
use ProVM\Concept\Database\QueryBuilder; use ProVM\Concept\Database\QueryBuilder;
use ProVM\Concept\Model; use ProVM\Concept\Model;
use ProVM\Concept\Model\Factory; use ProVM\Concept\Model\Factory;
use ProVM\Concept\Model\Repository as RepositoryInterface; use ProVM\Concept\Model\Repository as RepositoryInterface;
use ProVM\Exception\BlankResult;
abstract class Repository implements RepositoryInterface abstract class Repository implements RepositoryInterface
{ {
@ -20,187 +18,102 @@ abstract class Repository implements RepositoryInterface
} }
protected Connection $connection; protected Connection $connection;
protected QueryBuilder $builder;
protected Factory $factory;
protected string $model;
protected string $table;
public function getConnection(): Connection
{
return $this->connection;
}
public function getQueryBuilder(): QueryBuilder
{
return $this->builder;
}
public function getFactory(): Factory
{
return $this->factory;
}
public function getModel(): string
{
return $this->model;
}
public function getTable(): string
{
return $this->table;
}
public function setConnection(Connection $connection): Repository public function setConnection(Connection $connection): Repository
{ {
$this->connection = $connection; $this->connection = $connection;
return $this; return $this;
} }
public function getConnection(): Connection
{
return $this->connection;
}
protected QueryBuilder $builder;
public function setQueryBuilder(QueryBuilder $builder): RepositoryInterface public function setQueryBuilder(QueryBuilder $builder): RepositoryInterface
{ {
$this->builder = $builder; $this->builder = $builder;
return $this; return $this;
} }
public function getQueryBuilder(): QueryBuilder
{
return $this->builder;
}
protected Factory $factory;
public function setFactory(Factory $factory): RepositoryInterface public function setFactory(Factory $factory): RepositoryInterface
{ {
$this->factory = $factory; $this->factory = $factory;
return $this; return $this;
} }
public function setModel(string $model_class): RepositoryInterface public function getFactory(): Factory
{ {
$this->model = $model_class; return $this->factory;
return $this;
} }
protected string $table;
public function setTable(string $table): RepositoryInterface public function setTable(string $table): RepositoryInterface
{ {
$this->table = $table; $this->table = $table;
return $this; return $this;
} }
public function getTable(): string
public function save(Model &$model): void
{ {
try { return $this->table;
$old = $this->defaultFind($model);
$this->update($model, $old);
} catch (BlankResult $e) {
$this->insert($model);
$model->setId($this->getConnection()->getPDO()->lastInsertId());
}
} }
public function update(Model $model, Model $old): void protected array $columns;
public function setColumns(array $columns): RepositoryInterface
{ {
$model_values = $this->valuesForUpdate($model); foreach ($columns as $column) {
$old_values = $this->valuesForUpdate($old); $this->addColumn($column);
$columns = [];
$values = [];
foreach ($this->fieldsForUpdate() as $i => $column) {
if (isset($model_values[$i]) and $old_values[$i] !== $model_values[$i]) {
$columns []= "`{$column}` = ?";
$values []= $model_values[$i];
}
} }
if (count($columns) === 0) { return $this;
}
public function addColumn(string $column): RepositoryInterface
{
$this->columns []= $column;
return $this;
}
public function getColumns(): array
{
return $this->columns;
}
public function save(Model $model): void
{
if (!$model->isDirty() and !$model->isNew()) {
return; return;
} }
$cols = [];
$values = array_values($values); $values = [];
$values []= $old->{$this->idProperty()}(); foreach ($this->getColumns() as $column) {
$query = $this->getQueryBuilder()->update($this->getTable())->set($columns)->where(["{$this->idField()}} = ?"]); $m = 'get' . ucwords($column);
$cols []= '?';
$values []= $model->{$m}();
}
$query = $this->getQueryBuilder()->insert($this->getTable())->columns($this->getColumns())->values($cols);
$this->getConnection()->execute($query, $values); $this->getConnection()->execute($query, $values);
} }
public function create(array $data): Model public function edit(Model $model, array $data): Model
{ {
try { foreach ($this->getColumns() as $col) {
return $this->defaultSearch($data); if (isset($data[$col])) {
} catch (PDOException | BlankResult $e) { $m = 'set' . ucwords($col);
$data[$this->idField()] = 0; $model->{$m}($data[$col]);
return $this->load($data); }
} }
return $model;
} }
public function delete(Model $model): void
{
$query = $this->getQueryBuilder()->delete($this->getTable())->where(['id = ?']);
$this->getConnection()->execute($query, [$model->getId()]);
$this->resetIndex();
$this->optimize();
}
protected function idProperty(): string
{
return 'getId';
}
protected function idField(): string
{
return 'id';
}
protected function insert(Model $model): void
{
$fields = $this->fieldsForInsert();
$fields_string = array_map(function($field) {
return "`{$field}`";
}, $fields);
$fields_questions = array_fill(0, count($fields), '?');
$query = $this->getQueryBuilder()->insert($this->getTable())->columns($fields_string)->values($fields_questions);
$values = $this->valuesForInsert($model);
$this->getConnection()->execute($query, $values);
}
protected function resetIndex(): void
{
$query = "ALTER TABLE `{$this->getTable()}` AUTO_INCREMENT = 1";
$this->getConnection()->query($query);
}
protected function optimize(): void
{
$query = "OPTIMIZE TABLE `{$this->getTable()}`";
$this->getConnection()->query($query);
}
protected function fetchOne(string $query, ?array $values = null): Model
{
if ($values !== null) {
$rs = $this->getConnection()->prepare($query);
$rs->execute($values);
} else {
$rs = $this->getConnection()->query($query);
}
$row = $rs->getFirstAsArray();
if (!$row) {
throw new BlankResult();
}
return $this->load($row);
}
protected function fetchMany(string $query, ?array $values = null): array
{
if ($values !== null) {
$rs = $this->getConnection()->prepare($query);
$rs->execute($values);
} else {
$rs = $this->getConnection()->query($query);
}
$rows = $rs->getAsArray();
if (!$rows) {
throw new BlankResult();
}
return array_map([$this, 'load'], $rows);
}
public function fetchById(int $id): Model public function fetchById(int $id): Model
{ {
$query = $this->getQueryBuilder() $query = $this->getQueryBuilder()
->select() ->select()
->from($this->getTable()) ->from($this->getTable())
->where(['id = ?']) ->where([['id', '?']])
->limit(1); ->limit(1);
return $this->fetchOne($query, [$id]); return $this->load($this->getConnection()->execute($query, [$id])->getAsArray()[0]);
} }
public function fetchAll(): array public function fetchAll(): array
{ {
$query = $this->getQueryBuilder() $query = $this->getQueryBuilder()
->select() ->select()
->from($this->getTable()); ->from($this->getTable());
return $this->fetchMany($query); return array_map([$this, 'load'], $this->getConnection()->query($query)->getAsArray());
} }
abstract protected function fieldsForUpdate(): array;
abstract protected function fieldsForInsert(): array;
abstract protected function valuesForUpdate(Model $model): array;
abstract protected function valuesForInsert(Model $model): array;
abstract protected function defaultFind(Model $model): Model;
abstract protected function defaultSearch(array $data): Model;
} }

View File

@ -1,11 +1,18 @@
<?php <?php
namespace ProVM\Concept; namespace ProVM\Concept;
use JsonSerializable; use ProVM\Concept\Model\Factory;
use ProVM\Concept\Model\Repository;
interface Model extends JsonSerializable interface Model
{ {
public function setFactory(Factory $factory): Model;
public function getFactory(): Factory;
public function setId(int $id): Model; public function setId(int $id): Model;
public function getId(): int; public function getId(): int;
public function setNew(): Model;
public function isNew(): bool;
public function setDirty(): Model;
public function isDirty(): bool;
public function save(): void;
public function edit(array $data): void;
} }

View File

@ -1,48 +1,13 @@
<?php <?php
namespace ProVM\Concept\Model; namespace ProVM\Concept\Model;
use ProVM\Concept\Model;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
interface Factory interface Factory
{ {
/**
* @param ContainerInterface $container
* @return Factory
*/
public function setContainer(ContainerInterface $container): Factory; public function setContainer(ContainerInterface $container): Factory;
/**
* @return ContainerInterface
*/
public function getContainer(): ContainerInterface; public function getContainer(): ContainerInterface;
public function setNamespace(string $namespace): Factory;
/** public function getNamespace(): string;
* Get Repository by class public function get(string $repository_name): Repository;
* @param string $repository_class
* @return Repository
*/
public function get(string $repository_class): Repository;
/**
* Get Repository by Model class
* @param string $model_class
* @return Repository
*/
public function getByModel(string $model_class): Repository;
/**
* Fetch Model by Id
* @param string $model_class
* @param int $id
* @return Model
*/
public function fetchById(string $model_class, int $id): Model;
/**
* Fetch all Models
* @param string $model_class
* @return array
*/
public function fetchAll(string $model_class): array;
} }

View File

@ -7,105 +7,22 @@ use ProVM\Concept\Model;
interface Repository interface Repository
{ {
/**
* @return Connection
*/
public function getConnection(): Connection;
/**
* @return QueryBuilder
*/
public function getQueryBuilder(): QueryBuilder;
/**
* @return Factory
*/
public function getFactory(): Factory;
/**
* @return string
*/
public function getModel(): string;
/**
* @return string
*/
public function getTable(): string;
/**
* @param Connection $connection
* @return Repository
*/
public function setConnection(Connection $connection): Repository; public function setConnection(Connection $connection): Repository;
/** public function getConnection(): Connection;
* @param QueryBuilder $builder
* @return Repository
*/
public function setQueryBuilder(QueryBuilder $builder): Repository; public function setQueryBuilder(QueryBuilder $builder): Repository;
/** public function getQueryBuilder(): QueryBuilder;
* @param Factory $factory
* @return Repository
*/
public function setFactory(Factory $factory): Repository; public function setFactory(Factory $factory): Repository;
/** public function getFactory(): Factory;
* @param string $model_class
* @return Repository
*/
public function setModel(string $model_class): Repository;
/**
* @param string $table
* @return Repository
*/
public function setTable(string $table): Repository;
/**
* Set up the Repository
* SHOULD CALL
* setTable
* setColumns [setRequired, setOptional]
* setProperties
* setMapping
* setModel
* @return Repository
*/
public function setup(): Repository; public function setup(): Repository;
public function setTable(string $table): Repository;
/** public function getTable(): string;
* Transform result array to Model public function setColumns(array $columns): Repository;
* @param array $data public function addColumn(string $column): Repository;
* @return Model public function getColumns(): array;
*/
public function load(array $data): Model; public function load(array $data): Model;
/** public function save(Model $model): void;
* Save Model to table public function edit(Model $model, array $data): Model;
* @param Model $model
* @return void
*/
public function save(Model &$model): void;
/**
* Update table value with Model
* @param Model $model
* @return void
*/
public function update(Model $model, Model $old): void;
/**
* Create new Model with data
* @param array $data
* @return Model
*/
public function create(array $data): Model; public function create(array $data): Model;
/**
* Delete Model from table
* @param Model $model
* @return void
*/
public function delete(Model $model): void;
/**
* Fetch Model by Id
* @param int $id
* @return Model
*/
public function fetchById(int $id): Model; public function fetchById(int $id): Model;
/**
* Fetch all Models
* @return array
*/
public function fetchAll(): array; public function fetchAll(): array;
} }

View File

@ -1,44 +0,0 @@
<?php
namespace ProVM\Implement\Model;
use Psr\Container\ContainerInterface;
use ProVM\Concept\Model;
use ProVM\Concept\Model\Factory as FactoryInterface;
use ProVM\Concept\Model\Repository;
class Factory implements FactoryInterface
{
public function __construct(ContainerInterface $container)
{
$this->setContainer($container);
}
protected ContainerInterface $container;
public function setContainer(ContainerInterface $container): FactoryInterface
{
$this->container = $container;
return $this;
}
public function getContainer(): ContainerInterface
{
return $this->container;
}
public function get(string $repository_class): Repository
{
return $this->getContainer()->get($repository_class);
}
public function getByModel(string $model_class): Repository
{
$class = str_replace('Model', 'Repository', $model_class);
return $this->get($class);
}
public function fetchById(string $model_class, int $id): Model
{
return $this->getByModel($model_class)->fetchById($id);
}
public function fetchAll(string $model_class): array
{
return $this->getByModel($model_class)->fetchAll();
}
}