Remove Mapping, simplifying Repository
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user