28 Commits

Author SHA1 Message Date
37c5a79d5a Made model jsonserializable and added functionality into repository 2022-09-09 15:57:01 -04:00
d12f3f7897 Implement delete 2022-09-09 13:15:54 -04:00
7fc7de7390 Added Factory implementation 2022-09-09 10:33:19 -04:00
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
5 changed files with 146 additions and 274 deletions

View File

@ -78,10 +78,7 @@ abstract class Model implements ModelInterface
if ($method->getName() === 'getRepository') { if ($method->getName() === 'getRepository') {
continue; continue;
} }
$p = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', str_replace('get', '', $method->getName()))); $p = strtolower(str_replace('get', '', $method->getName()));
if (!isset($this->{$p})) {
continue;
}
$output[$p] = $this->{$method->getName()}(); $output[$p] = $this->{$method->getName()}();
} }
return $output; return $output;

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,164 +18,172 @@ 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 getFactory(): Factory
{
return $this->factory;
}
protected string $model;
public function setModel(string $model_class): RepositoryInterface public function setModel(string $model_class): RepositoryInterface
{ {
$this->model = $model_class; $this->model = $model_class;
return $this; return $this;
} }
public function getModel(): string
{
return $this->model;
}
public function getNewModel(): Model
{
$class = $this->getModel();
return (new $class())
->setRepository($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);
}
return $this;
}
public function addColumn(string $column): RepositoryInterface
{
$this->columns []= $column;
return $this;
}
public function getColumns(): array
{
return $this->columns;
}
protected array $required;
public function setRequired(array $columns): RepositoryInterface
{
foreach ($columns as $item) {
$this->addRequired($item);
}
return $this;
}
public function addRequired(string $column): RepositoryInterface
{
$this->required []= $column;
return $this;
}
public function getRequired(): array
{
if (isset($this->optional) and !isset($this->required)) {
return array_diff($this->getColumns(), $this->getOptional());
}
return $this->required ?? $this->getColumns();
}
protected array $optional;
public function setOptional(array $columns): RepositoryInterface
{
foreach ($columns as $item) {
$this->addColumn($item);
}
return $this;
}
public function addOptional(string $column): RepositoryInterface
{
$this->optional []= $column;
return $this;
}
public function getOptional(): array
{
if (isset($this->required) and !isset($this->optional)) {
return array_diff($this->getColumns(), $this->getRequired());
}
return $this->optional ?? [];
}
$columns = []; public function fillData(Model $model, array $data): Model
$values = []; {
foreach ($this->fieldsForUpdate() as $i => $column) { foreach ($this->getRequired() as $column) {
if (isset($model_values[$i]) and $old_values[$i] !== $model_values[$i]) { $m = 'set' . ucwords($column);
$columns []= "`{$column}` = ?"; $model->{$m}($data[$column]);
$values []= $model_values[$i]; }
foreach ($this->getOptional() as $column) {
if (isset($data[$column])) {
$m = 'set' . ucwords($column);
$model->{$m}($data[$column]);
} }
} }
if (count($columns) === 0) { return $model;
}
public function load(array $data): Model
{
return $this->fillData($this->getNewModel()
->setId($data['id']), $data);
}
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 create(array $data): Model
{ {
try { return $this->fillData($this->getNewModel()
return $this->defaultSearch($data); ->setNew(), $data);
} catch (PDOException | BlankResult $e) { }
$data[$this->idField()] = 0; public function edit(Model $model, array $data): Model
return $this->load($data); {
foreach ($this->getColumns() as $col) {
if (isset($data[$col])) {
$m = 'set' . ucwords($col);
$model->{$m}($data[$col]);
}
} }
return $model;
} }
public function delete(Model $model): void public function delete(Model $model): void
{ {
$query = $this->getQueryBuilder()->delete($this->getTable())->where(['id = ?']); $query = $this->getQueryBuilder()->delete($this->getTable())->where(['id = ?']);
$this->getConnection()->execute($query, [$model->getId()]); $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
@ -185,22 +191,15 @@ abstract class Repository implements RepositoryInterface
$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,48 +1,11 @@
<?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;
/**
* Get Repository by class
* @param string $repository_class
* @return Repository
*/
public function get(string $repository_class): 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,33 @@ 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; public function setModel(string $model_class): Repository;
/** public function getModel(): string;
* @param string $table public function getNewModel(): Model;
* @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 setRequired(array $columns): Repository;
public function addRequired(string $column): Repository;
public function getRequired(): array;
public function setOptional(array $columns): Repository;
public function addOptional(string $column): Repository;
public function getOptional(): array;
public function fillData(Model $model, array $data): Model;
public function load(array $data): Model; public function load(array $data): Model;
/** public function save(Model $model): void;
* Save Model to table
* @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;
/** public function edit(Model $model, array $data): Model;
* Delete Model from table
* @param Model $model
* @return void
*/
public function delete(Model $model): 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,7 +1,6 @@
<?php <?php
namespace ProVM\Implement\Model; namespace ProVM\Implement\Model;
use ProVM\Concept\Model;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use ProVM\Concept\Model\Factory as FactoryInterface; use ProVM\Concept\Model\Factory as FactoryInterface;
use ProVM\Concept\Model\Repository; use ProVM\Concept\Model\Repository;
@ -27,18 +26,4 @@ class Factory implements FactoryInterface
{ {
return $this->getContainer()->get($repository_class); 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();
}
} }