Compare commits
2 Commits
b8409182d7
...
master
Author | SHA1 | Date | |
---|---|---|---|
81ad81f7bf | |||
12065b376b |
@ -1,20 +1,20 @@
|
|||||||
<?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 Repository $repository;
|
protected Factory $factory;
|
||||||
public function setRepository(Repository $repository): ModelInterface
|
public function setFactory(Factory $factory): ModelInterface
|
||||||
{
|
{
|
||||||
$this->repository = $repository;
|
$this->factory = $factory;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
public function getRepository(): Repository
|
public function getFactory(): Factory
|
||||||
{
|
{
|
||||||
return $this->repository;
|
return $this->factory;
|
||||||
}
|
}
|
||||||
protected int $id;
|
protected int $id;
|
||||||
public function setId(int $id): ModelInterface
|
public function setId(int $id): ModelInterface
|
||||||
@ -50,7 +50,7 @@ abstract class Model implements ModelInterface
|
|||||||
public function save(): void
|
public function save(): void
|
||||||
{
|
{
|
||||||
if ($this->isDirty()) {
|
if ($this->isDirty()) {
|
||||||
$this->getRepository()->save($this);
|
$this->getFactory()->get(get_class($this))->save($this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public function edit(array $data): void
|
public function edit(array $data): void
|
||||||
@ -61,29 +61,4 @@ abstract class Model implements ModelInterface
|
|||||||
}
|
}
|
||||||
$this->isDirty();
|
$this->isDirty();
|
||||||
}
|
}
|
||||||
public function delete(): void
|
|
||||||
{
|
|
||||||
$this->getRepository()->delete($this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function jsonSerialize(): mixed
|
|
||||||
{
|
|
||||||
$obj = new \ReflectionObject($this);
|
|
||||||
$methods = $obj->getMethods();
|
|
||||||
$output = [];
|
|
||||||
foreach ($methods as $method) {
|
|
||||||
if (!str_contains($method->getName(), 'get')) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if ($method->getName() === 'getRepository') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$p = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', str_replace('get', '', $method->getName())));
|
|
||||||
if (!isset($this->{$p})) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$output[$p] = $this->{$method->getName()}();
|
|
||||||
}
|
|
||||||
return $output;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ 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\Mapping;
|
|
||||||
use ProVM\Concept\Model\Repository as RepositoryInterface;
|
use ProVM\Concept\Model\Repository as RepositoryInterface;
|
||||||
|
|
||||||
abstract class Repository implements RepositoryInterface
|
abstract class Repository implements RepositoryInterface
|
||||||
@ -48,22 +47,6 @@ abstract class Repository implements RepositoryInterface
|
|||||||
{
|
{
|
||||||
return $this->factory;
|
return $this->factory;
|
||||||
}
|
}
|
||||||
protected string $model;
|
|
||||||
public function setModel(string $model_class): RepositoryInterface
|
|
||||||
{
|
|
||||||
$this->model = $model_class;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function getModel(): string
|
|
||||||
{
|
|
||||||
return $this->model;
|
|
||||||
}
|
|
||||||
public function getNewModel(): Model
|
|
||||||
{
|
|
||||||
$class = $this->getModel();
|
|
||||||
return (new $class())
|
|
||||||
->setRepository($this);
|
|
||||||
}
|
|
||||||
protected string $table;
|
protected string $table;
|
||||||
public function setTable(string $table): RepositoryInterface
|
public function setTable(string $table): RepositoryInterface
|
||||||
{
|
{
|
||||||
@ -89,183 +72,42 @@ abstract class Repository implements RepositoryInterface
|
|||||||
}
|
}
|
||||||
public function getColumns(): array
|
public function getColumns(): array
|
||||||
{
|
{
|
||||||
return $this->columns ?? array_merge($this->getRequired(), $this->getOptional());
|
return $this->columns;
|
||||||
}
|
|
||||||
protected array $properties;
|
|
||||||
public function getProperties(): array
|
|
||||||
{
|
|
||||||
return $this->properties;
|
|
||||||
}
|
|
||||||
public function addProperty(string $property): RepositoryInterface
|
|
||||||
{
|
|
||||||
$this->properties []= $property;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function setProperties(array $properties): RepositoryInterface
|
|
||||||
{
|
|
||||||
foreach ($properties as $property) {
|
|
||||||
$this->addProperty($property);
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
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 ?? [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMethod(string $property, bool $get = true): string
|
|
||||||
{
|
|
||||||
$m = str_replace(' ', '', ucwords(str_replace('_', ' ', $property)));
|
|
||||||
if ($get) {
|
|
||||||
return "get{$m}";
|
|
||||||
}
|
|
||||||
return "set{$m}";
|
|
||||||
}
|
|
||||||
public function getProperty(string $method): string
|
|
||||||
{
|
|
||||||
$parts = preg_split('/(?=[A-Z])/', $method);
|
|
||||||
if (in_array(strtolower($parts[0]), ['get', 'set'])) {
|
|
||||||
array_shift($parts);
|
|
||||||
}
|
|
||||||
return strtolower(implode('_', $parts));
|
|
||||||
}
|
|
||||||
public function fillData(Model $model, array $data): Model
|
|
||||||
{
|
|
||||||
foreach ($this->getProperties() as $property) {
|
|
||||||
$m = $this->getMethod($property, false);
|
|
||||||
if (in_array($property, $this->getRequired())) {
|
|
||||||
$model->{$m}($data[$property]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (in_array("{$property}_id", $this->getRequired())) {
|
|
||||||
$model->{$m}($data["{$property}_id"]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (in_array($property, $this->getOptional()) and isset($data[$property])) {
|
|
||||||
$model->{$m}($data[$property]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (in_array("{$property}_id", $this->getOptional()) and isset($data["{$property}_id"])) {
|
|
||||||
$model->{$m}($data["{$property}_id"]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
error_log("Missing {$property} in data for " . get_called_class() . "::fillData");
|
|
||||||
}
|
|
||||||
return $model;
|
|
||||||
}
|
|
||||||
public function mapArray(Model $model, array $data): array
|
|
||||||
{
|
|
||||||
foreach ($this->getColumns() as $column) {
|
|
||||||
$m = $this->getMethod($column);
|
|
||||||
if (!method_exists($model, $m)) {
|
|
||||||
error_log("Missing getter for {$column} in " . get_called_class() . "::mapArray");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$data[$column] = $model->{$m}();
|
|
||||||
}
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
public function load(array $data): Model
|
|
||||||
{
|
|
||||||
return $this->fillData($this->getNewModel()
|
|
||||||
->setId($data['id']), $data);
|
|
||||||
}
|
|
||||||
public function save(Model $model): void
|
public function save(Model $model): void
|
||||||
{
|
{
|
||||||
if (!$model->isDirty() and !$model->isNew()) {
|
if (!$model->isDirty() and !$model->isNew()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!$model->isNew()) {
|
$cols = [];
|
||||||
$this->update($model);
|
$values = [];
|
||||||
return;
|
foreach ($this->getColumns() as $column) {
|
||||||
|
$m = 'get' . ucwords($column);
|
||||||
|
$cols []= '?';
|
||||||
|
$values []= $model->{$m}();
|
||||||
}
|
}
|
||||||
$values = array_replace(array_flip($this->getColumns()), $this->mapArray($model, []));
|
|
||||||
$cols = array_fill(0, count($values), '?');
|
|
||||||
$query = $this->getQueryBuilder()->insert($this->getTable())->columns($this->getColumns())->values($cols);
|
$query = $this->getQueryBuilder()->insert($this->getTable())->columns($this->getColumns())->values($cols);
|
||||||
$this->getConnection()->execute($query, array_values($values));
|
|
||||||
}
|
|
||||||
public function update(Model $model): void
|
|
||||||
{
|
|
||||||
if (!$model->isDirty() and !$model->isNew()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$values = $this->mapArray($model, []);
|
|
||||||
$cols = array_map(function($column) {
|
|
||||||
return "{$column} = ?";
|
|
||||||
}, $values);
|
|
||||||
$values = array_values($values);
|
|
||||||
$values []= $model->getId();
|
|
||||||
$query = $this->getQueryBuilder()->update($this->getTable())->set($cols)->where(['id = ?']);
|
|
||||||
$this->getConnection()->execute($query, $values);
|
$this->getConnection()->execute($query, $values);
|
||||||
}
|
}
|
||||||
public function create(array $data): Model
|
|
||||||
{
|
|
||||||
return $this->fillData($this->getNewModel()
|
|
||||||
->setNew(), $data);
|
|
||||||
}
|
|
||||||
public function edit(Model $model, array $data): Model
|
public function edit(Model $model, array $data): Model
|
||||||
{
|
{
|
||||||
foreach ($this->getColumns() as $col) {
|
foreach ($this->getColumns() as $col) {
|
||||||
if (isset($data[$col])) {
|
if (isset($data[$col])) {
|
||||||
$m = $this->getMethod($col, false);
|
$m = 'set' . ucwords($col);
|
||||||
if (!method_exists($model, $m)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$model->{$m}($data[$col]);
|
$model->{$m}($data[$col]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $model;
|
return $model;
|
||||||
}
|
}
|
||||||
public function delete(Model $model): void
|
|
||||||
{
|
|
||||||
$query = $this->getQueryBuilder()->delete($this->getTable())->where(['id = ?']);
|
|
||||||
$this->getConnection()->execute($query, [$model->getId()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
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->load($this->getConnection()->execute($query, [$id])->getFirstAsArray());
|
return $this->load($this->getConnection()->execute($query, [$id])->getAsArray()[0]);
|
||||||
}
|
}
|
||||||
public function fetchAll(): array
|
public function fetchAll(): array
|
||||||
{
|
{
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
<?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 setRepository(Repository $factory): Model;
|
public function setFactory(Factory $factory): Model;
|
||||||
public function getRepository(): Repository;
|
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 setNew(): Model;
|
||||||
@ -16,5 +15,4 @@ interface Model extends JsonSerializable
|
|||||||
public function isDirty(): bool;
|
public function isDirty(): bool;
|
||||||
public function save(): void;
|
public function save(): void;
|
||||||
public function edit(array $data): void;
|
public function edit(array $data): void;
|
||||||
public function delete(): void;
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
|
||||||
}
|
}
|
||||||
|
@ -7,240 +7,22 @@ use ProVM\Concept\Model;
|
|||||||
|
|
||||||
interface Repository
|
interface Repository
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @param Connection $connection
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setConnection(Connection $connection): Repository;
|
public function setConnection(Connection $connection): Repository;
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Connection
|
|
||||||
*/
|
|
||||||
public function getConnection(): Connection;
|
public function getConnection(): Connection;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param QueryBuilder $builder
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setQueryBuilder(QueryBuilder $builder): Repository;
|
public function setQueryBuilder(QueryBuilder $builder): Repository;
|
||||||
|
|
||||||
/**
|
|
||||||
* @return QueryBuilder
|
|
||||||
*/
|
|
||||||
public function getQueryBuilder(): QueryBuilder;
|
public function getQueryBuilder(): QueryBuilder;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Factory $factory
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setFactory(Factory $factory): Repository;
|
public function setFactory(Factory $factory): Repository;
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Factory
|
|
||||||
*/
|
|
||||||
public function getFactory(): Factory;
|
public function getFactory(): Factory;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $model_class
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setModel(string $model_class): Repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getModel(): string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get clean empty Model
|
|
||||||
* @return Model
|
|
||||||
*/
|
|
||||||
public function getNewModel(): Model;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set up the Repository
|
|
||||||
* SHOULD CALL
|
|
||||||
* setTable
|
|
||||||
* setColumns [setRequired, setOptional]
|
|
||||||
* setProperties
|
|
||||||
* setMapping
|
|
||||||
* setModel
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setup(): Repository;
|
public function setup(): Repository;
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getTable(): string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $table
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setTable(string $table): Repository;
|
public function setTable(string $table): Repository;
|
||||||
|
public function getTable(): string;
|
||||||
/**
|
|
||||||
* Set columns in table
|
|
||||||
* @param array $columns
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setColumns(array $columns): Repository;
|
public function setColumns(array $columns): Repository;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $column
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function addColumn(string $column): Repository;
|
public function addColumn(string $column): Repository;
|
||||||
|
|
||||||
/**
|
|
||||||
* Get table columns
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getColumns(): array;
|
public function getColumns(): array;
|
||||||
|
|
||||||
/**
|
|
||||||
* Get Model properties
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getProperties(): array;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $property
|
|
||||||
* @param $value
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function addProperty(string $property): Repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set Model properties
|
|
||||||
* @param array $properties
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setProperties(array $properties): Repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set required columns
|
|
||||||
* Optional
|
|
||||||
* @param array $columns
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setRequired(array $columns): Repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $column
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function addRequired(string $column): Repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get required columns
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getRequired(): array;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set optional columns
|
|
||||||
* @param array $columns
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setOptional(array $columns): Repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $column
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function addOptional(string $column): Repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get optional columns
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getOptional(): array;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get Model method
|
|
||||||
* @param string $property
|
|
||||||
* @param bool $get
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getMethod(string $property, bool $get = true): string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $method
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getProperty(string $method): string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fill empty Model with data
|
|
||||||
* @param Model $model
|
|
||||||
* @param array $data
|
|
||||||
* @return Model
|
|
||||||
*/
|
|
||||||
public function fillData(Model $model, array $data): Model;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fill data array with Model values. Accepts a preset data array
|
|
||||||
* @param Model $model
|
|
||||||
* @param array $data
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function mapArray(Model $model, array $data): array;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Transform result array to Model
|
|
||||||
* @param array $data
|
|
||||||
* @return Model
|
|
||||||
*/
|
|
||||||
public function load(array $data): Model;
|
public function load(array $data): Model;
|
||||||
|
|
||||||
/**
|
|
||||||
* Save Model to table
|
|
||||||
* @param Model $model
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function save(Model $model): void;
|
public function save(Model $model): void;
|
||||||
|
|
||||||
/**
|
|
||||||
* Update table value with Model
|
|
||||||
* @param Model $model
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function update(Model $model): void;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create new Model with data
|
|
||||||
* @param array $data
|
|
||||||
* @return Model
|
|
||||||
*/
|
|
||||||
public function create(array $data): Model;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Edit Model with data
|
|
||||||
* @param Model $model
|
|
||||||
* @param array $data
|
|
||||||
* @return Model
|
|
||||||
*/
|
|
||||||
public function edit(Model $model, array $data): Model;
|
public function edit(Model $model, 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;
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
@ -23,22 +22,25 @@ class Factory implements FactoryInterface
|
|||||||
{
|
{
|
||||||
return $this->container;
|
return $this->container;
|
||||||
}
|
}
|
||||||
public function get(string $repository_class): Repository
|
protected string $namespace;
|
||||||
|
public function setNamespace(string $namespace): FactoryInterface
|
||||||
{
|
{
|
||||||
return $this->getContainer()->get($repository_class);
|
$this->namespace = $namespace;
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
public function getByModel(string $model_class): Repository
|
public function getNamespace(): string
|
||||||
{
|
{
|
||||||
$class = str_replace('Model', 'Repository', $model_class);
|
return $this->namespace;
|
||||||
return $this->get($class);
|
|
||||||
}
|
}
|
||||||
|
protected function buildRepository(string $repository_name): string
|
||||||
public function fetchById(string $model_class, int $id): Model
|
|
||||||
{
|
{
|
||||||
return $this->getByModel($model_class)->fetchById($id);
|
return implode("\\", [
|
||||||
|
$this->getNamespace(),
|
||||||
|
$repository_name
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
public function fetchAll(string $model_class): array
|
public function get(string $repository_name): Repository
|
||||||
{
|
{
|
||||||
return $this->getByModel($model_class)->fetchAll();
|
return $this->getContainer()->get($this->buildRepository($repository_name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user