Added Mapping functionality
This commit is contained in:
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\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
|
||||||
@ -73,60 +74,40 @@ abstract class Repository implements RepositoryInterface
|
|||||||
{
|
{
|
||||||
return $this->table;
|
return $this->table;
|
||||||
}
|
}
|
||||||
protected array $mappings;
|
protected array $column_mappings;
|
||||||
public function getMappings(): array
|
protected array $property_mappings;
|
||||||
|
public function getColumnMappings(): array
|
||||||
{
|
{
|
||||||
if (isset($this->mappings)) {
|
return $this->column_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
|
public function addColumnMapping(Mapping $mapping): RepositoryInterface
|
||||||
{
|
{
|
||||||
$this->mappings []= (object) compact('column', 'property');
|
$this->column_mappings []= $mapping;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
public function setMappings(array $mappings): Repository
|
public function setColumnMappings(array $mappings): RepositoryInterface
|
||||||
{
|
{
|
||||||
foreach ($mappings as $mapping) {
|
foreach ($mappings as $mapping) {
|
||||||
if (is_array($mapping)) {
|
$this->addColumnMapping($mapping);
|
||||||
$this->addMapping($mapping['column'], $mapping['property']);
|
|
||||||
}
|
|
||||||
if (is_object($mapping)) {
|
|
||||||
$this->addMapping($mapping->column, $mapping->property);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
public function findColumnByProperty(string $property): string
|
public function getPropertyMappings(): array
|
||||||
{
|
{
|
||||||
foreach ($this->getMappings() as $mapping) {
|
return $this->property_mappings;
|
||||||
if ($mapping->property === $property) {
|
|
||||||
return $mapping->column;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (in_array($property, $this->getColumns())) {
|
|
||||||
return $property;
|
|
||||||
}
|
|
||||||
throw new \InvalidArgumentException("Property {$property} not found in mapping in " . get_called_class());
|
|
||||||
}
|
}
|
||||||
public function findPropertyByColumn(string $column): string
|
public function addPropertyMapping(Mapping $mapping): Repository
|
||||||
{
|
{
|
||||||
foreach ($this->getMappings() as $mapping) {
|
$this->property_mappings []= $mapping;
|
||||||
if ($mapping->column === $column) {
|
return $this;
|
||||||
return $mapping->property;
|
}
|
||||||
}
|
public function setPropertyMappings(array $mappings): Repository
|
||||||
}
|
{
|
||||||
if (in_array($column, $this->getProperties())) {
|
foreach ($mappings as $mapping) {
|
||||||
return $column;
|
$this->addPropertyMapping($mapping);
|
||||||
}
|
}
|
||||||
throw new \InvalidArgumentException("Column {$column} not found in mapping in " . get_called_class());
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected array $columns;
|
protected array $columns;
|
||||||
public function setColumns(array $columns): RepositoryInterface
|
public function setColumns(array $columns): RepositoryInterface
|
||||||
{
|
{
|
||||||
@ -220,52 +201,15 @@ abstract class Repository implements RepositoryInterface
|
|||||||
}
|
}
|
||||||
public function fillData(Model $model, array $data): Model
|
public function fillData(Model $model, array $data): Model
|
||||||
{
|
{
|
||||||
foreach ($this->getColumns() as $column) {
|
foreach ($this->getPropertyMappings() as $mapping) {
|
||||||
try {
|
$mapping($data, $model);
|
||||||
$property = $this->findPropertyByColumn($column);
|
|
||||||
} catch (\InvalidArgumentException $e) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$m = $this->getMethod($property, false);
|
|
||||||
if (!method_exists($model, $m)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$model->{$m}($data[$column]);
|
|
||||||
}
|
|
||||||
foreach ($this->getOptional() as $column) {
|
|
||||||
try {
|
|
||||||
$property = $this->findPropertyByColumn($column);
|
|
||||||
} catch (\InvalidArgumentException $e) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$m = $this->getMethod($property, false);
|
|
||||||
if (!isset($data[$column])) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!method_exists($model, $m)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$model->{$m}($data[$column]);
|
|
||||||
}
|
}
|
||||||
return $model;
|
return $model;
|
||||||
}
|
}
|
||||||
public function mapArray(Model $model, array $data): array
|
public function mapArray(Model $model, array $data): array
|
||||||
{
|
{
|
||||||
foreach ($this->getProperties() as $property) {
|
foreach ($this->getColumnMappings() as $mapping) {
|
||||||
try {
|
$mapping($model, $data);
|
||||||
$column = $this->findColumnByProperty($property);
|
|
||||||
} catch (\InvalidArgumentException $e) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (isset($data[$column])) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$m = $this->getMethod($property);
|
|
||||||
if (!method_exists($model, $m)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$val = $model->{$m}();
|
|
||||||
$data[$column] = $val;
|
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
@ -283,7 +227,7 @@ abstract class Repository implements RepositoryInterface
|
|||||||
$this->update($model);
|
$this->update($model);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$values = $this->mapArray($model, []);
|
$values = array_replace(array_flip($this->getColumns()), $this->mapArray($model, []));
|
||||||
$cols = array_fill(0, count($values), '?');
|
$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));
|
$this->getConnection()->execute($query, array_values($values));
|
||||||
|
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;
|
||||||
|
}
|
@ -81,38 +81,38 @@ interface Repository
|
|||||||
public function setTable(string $table): Repository;
|
public function setTable(string $table): Repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Model - table mapping
|
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getMappings(): array;
|
public function getColumnMappings(): array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param Mapping $mapping
|
||||||
* @param string $property
|
|
||||||
* @return Repository
|
* @return Repository
|
||||||
*/
|
*/
|
||||||
public function addMapping(string $column, string $property): Repository;
|
public function addColumnMapping(Mapping $mapping): Repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set Model - table mappings
|
|
||||||
* @param array $mappings
|
* @param array $mappings
|
||||||
* @return Repository
|
* @return Repository
|
||||||
*/
|
*/
|
||||||
public function setMappings(array $mappings): Repository;
|
public function setColumnMappings(array $mappings): Repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a property from mapping by column
|
* @return array
|
||||||
* @param string $column
|
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
public function findPropertyByColumn(string $column): string;
|
public function getPropertyMappings(): array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a column from mapping by property
|
* @param Mapping $mapping
|
||||||
* @param string $property
|
* @return Repository
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
public function findColumnByProperty(string $property): string;
|
public function addPropertyMapping(Mapping $mapping): Repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $mappings
|
||||||
|
* @return Repository
|
||||||
|
*/
|
||||||
|
public function setPropertyMappings(array $mappings): Repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set columns in table
|
* Set columns in table
|
||||||
|
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