Improve mapping
This commit is contained in:
@ -73,6 +73,54 @@ abstract class Repository implements RepositoryInterface
|
|||||||
{
|
{
|
||||||
return $this->table;
|
return $this->table;
|
||||||
}
|
}
|
||||||
|
protected array $mappings;
|
||||||
|
public function getMappings(): array
|
||||||
|
{
|
||||||
|
if (isset($this->mappings)) {
|
||||||
|
return $this->mappings;
|
||||||
|
}
|
||||||
|
$mappings = [];
|
||||||
|
foreach ($this->getColumns() as $column) {
|
||||||
|
$mappings []= (object) ['column' => $column, 'property' => $column];
|
||||||
|
}
|
||||||
|
return $mappings;
|
||||||
|
}
|
||||||
|
public function addMapping(string $column, string $property): Repository
|
||||||
|
{
|
||||||
|
$this->mappings []= (object) compact('column', 'property');
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function setMappings(array $mappings): Repository
|
||||||
|
{
|
||||||
|
foreach ($mappings as $mapping) {
|
||||||
|
if (is_array($mapping)) {
|
||||||
|
$this->addMapping($mapping['column'], $mapping['property']);
|
||||||
|
}
|
||||||
|
if (is_object($mapping)) {
|
||||||
|
$this->addMapping($mapping->column, $mapping->property);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function findColumnByProperty(string $property): string
|
||||||
|
{
|
||||||
|
foreach ($this->getMappings() as $mapping) {
|
||||||
|
if ($mapping->property === $property) {
|
||||||
|
return $mapping->column;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new \InvalidArgumentException("Property {$property} not found in mapping in " . get_called_class());
|
||||||
|
}
|
||||||
|
public function findPropertyByColumn(string $column): string
|
||||||
|
{
|
||||||
|
foreach ($this->getMappings() as $mapping) {
|
||||||
|
if ($mapping->column === $column) {
|
||||||
|
return $mapping->property;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new \InvalidArgumentException("Column {$column} not found in mapping in " . get_called_class());
|
||||||
|
}
|
||||||
|
|
||||||
protected array $columns;
|
protected array $columns;
|
||||||
public function setColumns(array $columns): RepositoryInterface
|
public function setColumns(array $columns): RepositoryInterface
|
||||||
{
|
{
|
||||||
@ -90,6 +138,23 @@ abstract class Repository implements RepositoryInterface
|
|||||||
{
|
{
|
||||||
return $this->columns;
|
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;
|
protected array $required;
|
||||||
public function setRequired(array $columns): RepositoryInterface
|
public function setRequired(array $columns): RepositoryInterface
|
||||||
{
|
{
|
||||||
@ -131,9 +196,9 @@ abstract class Repository implements RepositoryInterface
|
|||||||
return $this->optional ?? [];
|
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) {
|
if ($get) {
|
||||||
return "get{$m}";
|
return "get{$m}";
|
||||||
}
|
}
|
||||||
@ -149,18 +214,28 @@ abstract class Repository implements RepositoryInterface
|
|||||||
}
|
}
|
||||||
public function fillData(Model $model, array $data): Model
|
public function fillData(Model $model, array $data): Model
|
||||||
{
|
{
|
||||||
foreach ($this->getRequired() as $column) {
|
foreach ($this->getColumns() as $column) {
|
||||||
$m = $this->getMethod($column, false);
|
try {
|
||||||
|
$property = $this->findPropertyByColumn($column);
|
||||||
|
} catch (\InvalidArgumentException $e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$m = $this->getMethod($property, false);
|
||||||
if (!method_exists($model, $m)) {
|
if (!method_exists($model, $m)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$model->{$m}($data[$column]);
|
$model->{$m}($data[$column]);
|
||||||
}
|
}
|
||||||
foreach ($this->getOptional() as $column) {
|
foreach ($this->getOptional() as $column) {
|
||||||
|
try {
|
||||||
|
$property = $this->findPropertyByColumn($column);
|
||||||
|
} catch (\InvalidArgumentException $e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$m = $this->getMethod($property, false);
|
||||||
if (!isset($data[$column])) {
|
if (!isset($data[$column])) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$m = $this->getMethod($column, false);
|
|
||||||
if (!method_exists($model, $m)) {
|
if (!method_exists($model, $m)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -170,11 +245,13 @@ abstract class Repository implements RepositoryInterface
|
|||||||
}
|
}
|
||||||
public function mapArray(Model $model, array $data): array
|
public function mapArray(Model $model, array $data): array
|
||||||
{
|
{
|
||||||
foreach ($this->getColumns() as $column) {
|
foreach ($this->getProperties() as $property) {
|
||||||
if (isset($data[$column])) {
|
try {
|
||||||
|
$column = $this->findColumnByProperty($property);
|
||||||
|
} catch (\InvalidArgumentException $e) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$m = $this->getMethod($column);
|
$m = $this->getMethod($property);
|
||||||
if (!method_exists($model, $m)) {
|
if (!method_exists($model, $m)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,48 @@
|
|||||||
<?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;
|
||||||
}
|
}
|
||||||
|
@ -7,37 +7,274 @@ 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;
|
public function setModel(string $model_class): Repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function getModel(): string;
|
public function getModel(): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get clean empty Model
|
||||||
|
* @return Model
|
||||||
|
*/
|
||||||
public function getNewModel(): 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;
|
||||||
public function setTable(string $table): Repository;
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function getTable(): string;
|
public function getTable(): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $table
|
||||||
|
* @return Repository
|
||||||
|
*/
|
||||||
|
public function setTable(string $table): Repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Model - table mapping
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getMappings(): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $column
|
||||||
|
* @param string $property
|
||||||
|
* @return Repository
|
||||||
|
*/
|
||||||
|
public function addMapping(string $column, string $property): Repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Model - table mappings
|
||||||
|
* @param array $mappings
|
||||||
|
* @return Repository
|
||||||
|
*/
|
||||||
|
public function setMappings(array $mappings): Repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a property from mapping by column
|
||||||
|
* @param string $column
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function findPropertyByColumn(string $column): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a column from mapping by property
|
||||||
|
* @param string $property
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function findColumnByProperty(string $property): 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;
|
public function setRequired(array $columns): Repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $column
|
||||||
|
* @return Repository
|
||||||
|
*/
|
||||||
public function addRequired(string $column): Repository;
|
public function addRequired(string $column): Repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get required columns
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function getRequired(): array;
|
public function getRequired(): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set optional columns
|
||||||
|
* @param array $columns
|
||||||
|
* @return Repository
|
||||||
|
*/
|
||||||
public function setOptional(array $columns): Repository;
|
public function setOptional(array $columns): Repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $column
|
||||||
|
* @return Repository
|
||||||
|
*/
|
||||||
public function addOptional(string $column): Repository;
|
public function addOptional(string $column): Repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get optional columns
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function getOptional(): 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;
|
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;
|
public function fillData(Model $model, array $data): Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fill data array with Model values
|
||||||
|
* @param Model $model
|
||||||
|
* @param array $data
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function mapArray(Model $model, array $data): 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;
|
public function update(Model $model): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create new Model with data
|
||||||
|
* @param array $data
|
||||||
|
* @return Model
|
||||||
|
*/
|
||||||
public function create(array $data): 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
<?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;
|
||||||
@ -26,4 +27,18 @@ 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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user