Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
e576ef5054 | |||
5c1f424c76 | |||
b19654bc70 | |||
fe67519682 | |||
0af6be2c8e | |||
0a9bed1735 | |||
ba3c70a70e | |||
422d9a6dbd | |||
bc006f3e01 | |||
23c56f9511 | |||
4ccc38ffac | |||
ef1603bc4b | |||
8531658899 | |||
f19385ccc1 | |||
02f8bb0b4f | |||
d3771d8844 |
@ -78,7 +78,7 @@ abstract class Model implements ModelInterface
|
||||
if ($method->getName() === 'getRepository') {
|
||||
continue;
|
||||
}
|
||||
$p = strtolower(str_replace('get', '', $method->getName()));
|
||||
$p = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', str_replace('get', '', $method->getName())));
|
||||
if (!isset($this->{$p})) {
|
||||
continue;
|
||||
}
|
||||
|
63
src/Alias/Model/Mapping.php
Normal file
63
src/Alias/Model/Mapping.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
namespace ProVM\Alias\Model;
|
||||
|
||||
use ProVM\Concept\Model;
|
||||
|
||||
abstract class Mapping implements \ProVM\Concept\Model\Mapping
|
||||
{
|
||||
protected string $input;
|
||||
protected string $output;
|
||||
protected \Closure $transformation;
|
||||
|
||||
public function getInputName(): string
|
||||
{
|
||||
return $this->input;
|
||||
}
|
||||
public function getOutputName(): string
|
||||
{
|
||||
return $this->output;
|
||||
}
|
||||
public function getTransformation(): callable
|
||||
{
|
||||
return $this->transformation;
|
||||
}
|
||||
|
||||
public function setInputName(string $input): \ProVM\Concept\Model\Mapping
|
||||
{
|
||||
$this->input = $input;
|
||||
return $this;
|
||||
}
|
||||
public function setOutputName(string $output): \ProVM\Concept\Model\Mapping
|
||||
{
|
||||
$this->output = $output;
|
||||
return $this;
|
||||
}
|
||||
public function setTransformation(callable $transformation): \ProVM\Concept\Model\Mapping
|
||||
{
|
||||
$this->transformation = $transformation;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function __invoke(mixed $input, mixed &$output, ?array $params = null): void
|
||||
{
|
||||
if (is_array($input)) {
|
||||
$input_value = $input[$this->getInputName()];
|
||||
}
|
||||
if (is_a($input, Model::class)) {
|
||||
$input_value = $input->{$this->getInputName()}();
|
||||
}
|
||||
|
||||
$args = [$input_value];
|
||||
if ($params !== null) {
|
||||
$args = array_merge($args, $params);
|
||||
}
|
||||
$output_value = call_user_func_array($this->getTransformation(), $args);
|
||||
|
||||
if (is_array($output)) {
|
||||
$output[$this->getOutputName()] = $output_value;
|
||||
}
|
||||
if (is_a($output, Model::class)) {
|
||||
$output->{$this->getOutputName()}($output_value);
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ use ProVM\Concept\Database\Connection;
|
||||
use ProVM\Concept\Database\QueryBuilder;
|
||||
use ProVM\Concept\Model;
|
||||
use ProVM\Concept\Model\Factory;
|
||||
use ProVM\Concept\Model\Mapping;
|
||||
use ProVM\Concept\Model\Repository as RepositoryInterface;
|
||||
|
||||
abstract class Repository implements RepositoryInterface
|
||||
@ -73,6 +74,40 @@ abstract class Repository implements RepositoryInterface
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
protected array $column_mappings;
|
||||
protected array $property_mappings;
|
||||
public function getColumnMappings(): array
|
||||
{
|
||||
return $this->column_mappings;
|
||||
}
|
||||
public function addColumnMapping(Mapping $mapping): RepositoryInterface
|
||||
{
|
||||
$this->column_mappings []= $mapping;
|
||||
return $this;
|
||||
}
|
||||
public function setColumnMappings(array $mappings): RepositoryInterface
|
||||
{
|
||||
foreach ($mappings as $mapping) {
|
||||
$this->addColumnMapping($mapping);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function getPropertyMappings(): array
|
||||
{
|
||||
return $this->property_mappings;
|
||||
}
|
||||
public function addPropertyMapping(Mapping $mapping): Repository
|
||||
{
|
||||
$this->property_mappings []= $mapping;
|
||||
return $this;
|
||||
}
|
||||
public function setPropertyMappings(array $mappings): Repository
|
||||
{
|
||||
foreach ($mappings as $mapping) {
|
||||
$this->addPropertyMapping($mapping);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
protected array $columns;
|
||||
public function setColumns(array $columns): RepositoryInterface
|
||||
{
|
||||
@ -90,6 +125,23 @@ abstract class Repository implements RepositoryInterface
|
||||
{
|
||||
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
|
||||
{
|
||||
@ -131,9 +183,9 @@ abstract class Repository implements RepositoryInterface
|
||||
return $this->optional ?? [];
|
||||
}
|
||||
|
||||
public function getMethod(string $column, bool $get = true): string
|
||||
public function getMethod(string $property, bool $get = true): string
|
||||
{
|
||||
$m = str_replace(' ', '', ucwords(str_replace('_', ' ', $column)));
|
||||
$m = str_replace(' ', '', ucwords(str_replace('_', ' ', $property)));
|
||||
if ($get) {
|
||||
return "get{$m}";
|
||||
}
|
||||
@ -149,27 +201,15 @@ abstract class Repository implements RepositoryInterface
|
||||
}
|
||||
public function fillData(Model $model, array $data): Model
|
||||
{
|
||||
foreach ($this->getRequired() as $column) {
|
||||
$m = 'set' . ucwords($column);
|
||||
$model->{$m}($data[$column]);
|
||||
}
|
||||
foreach ($this->getOptional() as $column) {
|
||||
if (isset($data[$column])) {
|
||||
$m = 'set' . ucwords($column);
|
||||
$model->{$m}($data[$column]);
|
||||
}
|
||||
foreach ($this->getPropertyMappings() as $mapping) {
|
||||
$mapping($data, $model);
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
public function mapArray(Model $model, array $data): array
|
||||
{
|
||||
foreach ($this->getColumns() as $column) {
|
||||
$m = $this->getMethod($column);
|
||||
$val = $model->{$m}();
|
||||
if (isset($data[$column])) {
|
||||
continue;
|
||||
}
|
||||
$data[$column] = $val;
|
||||
foreach ($this->getColumnMappings() as $mapping) {
|
||||
$mapping($model, $data);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
@ -187,10 +227,10 @@ abstract class Repository implements RepositoryInterface
|
||||
$this->update($model);
|
||||
return;
|
||||
}
|
||||
$values = $this->mapArray($model, []);
|
||||
$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);
|
||||
$this->getConnection()->execute($query, $values);
|
||||
$this->getConnection()->execute($query, array_values($values));
|
||||
}
|
||||
public function update(Model $model): void
|
||||
{
|
||||
@ -215,7 +255,10 @@ abstract class Repository implements RepositoryInterface
|
||||
{
|
||||
foreach ($this->getColumns() as $col) {
|
||||
if (isset($data[$col])) {
|
||||
$m = 'set' . ucwords($col);
|
||||
$m = $this->getMethod($col, false);
|
||||
if (!method_exists($model, $m)) {
|
||||
continue;
|
||||
}
|
||||
$model->{$m}($data[$col]);
|
||||
}
|
||||
}
|
||||
@ -232,9 +275,9 @@ abstract class Repository implements RepositoryInterface
|
||||
$query = $this->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where([['id', '?']])
|
||||
->where(['id = ?'])
|
||||
->limit(1);
|
||||
return $this->load($this->getConnection()->execute($query, [$id])->getAsArray()[0]);
|
||||
return $this->load($this->getConnection()->execute($query, [$id])->getFirstAsArray());
|
||||
}
|
||||
public function fetchAll(): array
|
||||
{
|
||||
|
@ -1,11 +1,48 @@
|
||||
<?php
|
||||
namespace ProVM\Concept\Model;
|
||||
|
||||
use ProVM\Concept\Model;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
interface Factory
|
||||
{
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
* @return Factory
|
||||
*/
|
||||
public function setContainer(ContainerInterface $container): Factory;
|
||||
|
||||
/**
|
||||
* @return ContainerInterface
|
||||
*/
|
||||
public function getContainer(): ContainerInterface;
|
||||
|
||||
/**
|
||||
* Get Repository by class
|
||||
* @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;
|
||||
}
|
||||
|
13
src/Concept/Model/Mapping.php
Normal file
13
src/Concept/Model/Mapping.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace ProVM\Concept\Model;
|
||||
|
||||
interface Mapping
|
||||
{
|
||||
public function getInputName(): string;
|
||||
public function getOutputName(): string;
|
||||
public function getTransformation(): callable;
|
||||
public function setInputName(string $input): Mapping;
|
||||
public function setOutputName(string $output): Mapping;
|
||||
public function setTransformation(callable $transformation): Mapping;
|
||||
public function __invoke(mixed $input, mixed &$output, ?array $params = null): mixed;
|
||||
}
|
@ -7,37 +7,274 @@ use ProVM\Concept\Model;
|
||||
|
||||
interface Repository
|
||||
{
|
||||
/**
|
||||
* @param Connection $connection
|
||||
* @return Repository
|
||||
*/
|
||||
public function setConnection(Connection $connection): Repository;
|
||||
|
||||
/**
|
||||
* @return Connection
|
||||
*/
|
||||
public function getConnection(): Connection;
|
||||
|
||||
/**
|
||||
* @param QueryBuilder $builder
|
||||
* @return Repository
|
||||
*/
|
||||
public function setQueryBuilder(QueryBuilder $builder): Repository;
|
||||
|
||||
/**
|
||||
* @return QueryBuilder
|
||||
*/
|
||||
public function getQueryBuilder(): QueryBuilder;
|
||||
|
||||
/**
|
||||
* @param Factory $factory
|
||||
* @return Repository
|
||||
*/
|
||||
public function setFactory(Factory $factory): Repository;
|
||||
|
||||
/**
|
||||
* @return 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 setTable(string $table): Repository;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTable(): string;
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
* @return Repository
|
||||
*/
|
||||
public function setTable(string $table): Repository;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getColumnMappings(): array;
|
||||
|
||||
/**
|
||||
* @param Mapping $mapping
|
||||
* @return Repository
|
||||
*/
|
||||
public function addColumnMapping(Mapping $mapping): Repository;
|
||||
|
||||
/**
|
||||
* @param array $mappings
|
||||
* @return Repository
|
||||
*/
|
||||
public function setColumnMappings(array $mappings): Repository;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getPropertyMappings(): array;
|
||||
|
||||
/**
|
||||
* @param Mapping $mapping
|
||||
* @return Repository
|
||||
*/
|
||||
public function addPropertyMapping(Mapping $mapping): Repository;
|
||||
|
||||
/**
|
||||
* @param array $mappings
|
||||
* @return Repository
|
||||
*/
|
||||
public function setPropertyMappings(array $mappings): Repository;
|
||||
|
||||
/**
|
||||
* Set columns in table
|
||||
* @param array $columns
|
||||
* @return Repository
|
||||
*/
|
||||
public function setColumns(array $columns): Repository;
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
* @return Repository
|
||||
*/
|
||||
public function addColumn(string $column): Repository;
|
||||
|
||||
/**
|
||||
* Get table columns
|
||||
* @return 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;
|
||||
public function getMethod(string $column, bool $get = true): string;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 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): 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;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Fetch all Models
|
||||
* @return array
|
||||
*/
|
||||
public function fetchAll(): array;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Model;
|
||||
|
||||
use ProVM\Concept\Model;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use ProVM\Concept\Model\Factory as FactoryInterface;
|
||||
use ProVM\Concept\Model\Repository;
|
||||
@ -26,4 +27,18 @@ class Factory implements FactoryInterface
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
16
src/Implement/Model/Mapping/Basic.php
Normal file
16
src/Implement/Model/Mapping/Basic.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Model\Mapping;
|
||||
|
||||
use ProVM\Alias\Model\Mapping;
|
||||
|
||||
class Basic extends Mapping
|
||||
{
|
||||
public function __construct(string $input, string $output)
|
||||
{
|
||||
$this->setInputName($input)
|
||||
->setOutputName($output)
|
||||
->setTransformation(function($item) {
|
||||
return $item;
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user