Abstracts

This commit is contained in:
2022-09-08 21:35:33 -04:00
parent 7f33475e35
commit 230faaf3cc
2 changed files with 183 additions and 0 deletions

64
src/Alias/Model.php Normal file
View File

@ -0,0 +1,64 @@
<?php
namespace ProVM\Alias;
use ProVM\Concept\Model\Factory;
use ProVM\Concept\Model as 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;
public function setId(int $id): ModelInterface
{
$this->id = $id;
return $this;
}
public function getId(): int
{
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

@ -0,0 +1,119 @@
<?php
namespace ProVM\Alias\Model;
use ProVM\Concept\Database\Connection;
use ProVM\Concept\Database\QueryBuilder;
use ProVM\Concept\Model;
use ProVM\Concept\Model\Factory;
use ProVM\Concept\Model\Repository as RepositoryInterface;
abstract class Repository implements RepositoryInterface
{
public function __construct(Connection $connection, QueryBuilder $builder, Factory $factory)
{
$this->setConnection($connection)
->setQueryBuilder($builder)
->setFactory($factory)
->setup();
}
protected Connection $connection;
public function setConnection(Connection $connection): Repository
{
$this->connection = $connection;
return $this;
}
public function getConnection(): Connection
{
return $this->connection;
}
protected QueryBuilder $builder;
public function setQueryBuilder(QueryBuilder $builder): RepositoryInterface
{
$this->builder = $builder;
return $this;
}
public function getQueryBuilder(): QueryBuilder
{
return $this->builder;
}
protected Factory $factory;
public function setFactory(Factory $factory): RepositoryInterface
{
$this->factory = $factory;
return $this;
}
public function getFactory(): Factory
{
return $this->factory;
}
protected string $table;
public function setTable(string $table): RepositoryInterface
{
$this->table = $table;
return $this;
}
public function getTable(): string
{
return $this->table;
}
protected array $columns;
public function setColumns(array $columns): RepositoryInterface
{
foreach ($columns as $column) {
$this->addColumn($column);
}
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;
}
$cols = [];
$values = [];
foreach ($this->getColumns() as $column) {
$m = 'get' . ucwords($column);
$cols []= '?';
$values []= $model->{$m}();
}
$query = $this->getQueryBuilder()->insert($this->getTable())->columns($this->getColumns())->values($cols);
$this->getConnection()->execute($query, $values);
}
public function edit(Model $model, array $data): Model
{
foreach ($this->getColumns() as $col) {
if (isset($data[$col])) {
$m = 'set' . ucwords($col);
$model->{$m}($data[$col]);
}
}
return $model;
}
public function fetchById(int $id): Model
{
$query = $this->getQueryBuilder()
->select()
->from($this->getTable())
->where([['id', '?']])
->limit(1);
return $this->load($this->getConnection()->execute($query, [$id])->getAsArray()[0]);
}
public function fetchAll(): array
{
$query = $this->getQueryBuilder()
->select()
->from($this->getTable());
return array_map([$this, 'load'], $this->getConnection()->query($query)->getAsArray());
}
}