From 100df739160cbc93aa915bfeb9d6bbb21f8b95e0 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Mon, 10 Oct 2022 22:19:09 -0300 Subject: [PATCH] Remove Mapping, simplifying Repository --- src/Alias/Model/Mapping.php | 63 ------------------------- src/Alias/Model/Repository.php | 66 +++++++++++---------------- src/Concept/Model/Mapping.php | 13 ------ src/Concept/Model/Repository.php | 34 -------------- src/Implement/Model/Mapping/Basic.php | 16 ------- 5 files changed, 27 insertions(+), 165 deletions(-) delete mode 100644 src/Alias/Model/Mapping.php delete mode 100644 src/Concept/Model/Mapping.php delete mode 100644 src/Implement/Model/Mapping/Basic.php diff --git a/src/Alias/Model/Mapping.php b/src/Alias/Model/Mapping.php deleted file mode 100644 index 28e478f..0000000 --- a/src/Alias/Model/Mapping.php +++ /dev/null @@ -1,63 +0,0 @@ -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); - } - } -} \ No newline at end of file diff --git a/src/Alias/Model/Repository.php b/src/Alias/Model/Repository.php index 0c9a444..1557e65 100644 --- a/src/Alias/Model/Repository.php +++ b/src/Alias/Model/Repository.php @@ -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; } diff --git a/src/Concept/Model/Mapping.php b/src/Concept/Model/Mapping.php deleted file mode 100644 index 15e19cf..0000000 --- a/src/Concept/Model/Mapping.php +++ /dev/null @@ -1,13 +0,0 @@ -setInputName($input) - ->setOutputName($output) - ->setTransformation(function($item) { - return $item; - }); - } -} \ No newline at end of file