Compare commits
2 Commits
e576ef5054
...
b8409182d7
Author | SHA1 | Date | |
---|---|---|---|
b8409182d7 | |||
100df73916 |
@ -1,63 +0,0 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
}
|
@ -74,40 +74,6 @@ 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
|
||||
{
|
||||
@ -123,7 +89,7 @@ abstract class Repository implements RepositoryInterface
|
||||
}
|
||||
public function getColumns(): array
|
||||
{
|
||||
return $this->columns;
|
||||
return $this->columns ?? array_merge($this->getRequired(), $this->getOptional());
|
||||
}
|
||||
protected array $properties;
|
||||
public function getProperties(): array
|
||||
@ -201,15 +167,37 @@ abstract class Repository implements RepositoryInterface
|
||||
}
|
||||
public function fillData(Model $model, array $data): Model
|
||||
{
|
||||
foreach ($this->getPropertyMappings() as $mapping) {
|
||||
$mapping($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->getColumnMappings() as $mapping) {
|
||||
$mapping($model, $data);
|
||||
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;
|
||||
}
|
||||
|
@ -1,13 +0,0 @@
|
||||
<?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;
|
||||
}
|
@ -80,40 +80,6 @@ interface 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
|
||||
|
@ -1,16 +0,0 @@
|
||||
<?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