|
|
|
@ -5,6 +5,7 @@ use ProVM\Concept\Database\Connection;
|
|
|
|
|
use ProVM\Concept\Database\QueryBuilder;
|
|
|
|
|
use ProVM\Concept\Model;
|
|
|
|
|
use ProVM\Concept\Model\Factory;
|
|
|
|
|
use ProVM\Concept\Model\Mapping;
|
|
|
|
|
use ProVM\Concept\Model\Repository as RepositoryInterface;
|
|
|
|
|
|
|
|
|
|
abstract class Repository implements RepositoryInterface
|
|
|
|
@ -73,60 +74,6 @@ abstract class Repository implements RepositoryInterface
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->getMappings() as $mapping) {
|
|
|
|
|
if ($mapping->column === $column) {
|
|
|
|
|
return $mapping->property;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (in_array($column, $this->getProperties())) {
|
|
|
|
|
return $column;
|
|
|
|
|
}
|
|
|
|
|
throw new \InvalidArgumentException("Column {$column} not found in mapping in " . get_called_class());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected array $columns;
|
|
|
|
|
public function setColumns(array $columns): RepositoryInterface
|
|
|
|
|
{
|
|
|
|
@ -142,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
|
|
|
|
@ -220,52 +167,37 @@ abstract class Repository implements RepositoryInterface
|
|
|
|
|
}
|
|
|
|
|
public function fillData(Model $model, array $data): Model
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->getColumns() as $column) {
|
|
|
|
|
try {
|
|
|
|
|
$property = $this->findPropertyByColumn($column);
|
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
foreach ($this->getProperties() as $property) {
|
|
|
|
|
$m = $this->getMethod($property, false);
|
|
|
|
|
if (!method_exists($model, $m)) {
|
|
|
|
|
if (in_array($property, $this->getRequired())) {
|
|
|
|
|
$model->{$m}($data[$property]);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$model->{$m}($data[$column]);
|
|
|
|
|
}
|
|
|
|
|
foreach ($this->getOptional() as $column) {
|
|
|
|
|
try {
|
|
|
|
|
$property = $this->findPropertyByColumn($column);
|
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
|
if (in_array("{$property}_id", $this->getRequired())) {
|
|
|
|
|
$model->{$m}($data["{$property}_id"]);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$m = $this->getMethod($property, false);
|
|
|
|
|
if (!isset($data[$column])) {
|
|
|
|
|
if (in_array($property, $this->getOptional()) and isset($data[$property])) {
|
|
|
|
|
$model->{$m}($data[$property]);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!method_exists($model, $m)) {
|
|
|
|
|
if (in_array("{$property}_id", $this->getOptional()) and isset($data["{$property}_id"])) {
|
|
|
|
|
$model->{$m}($data["{$property}_id"]);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$model->{$m}($data[$column]);
|
|
|
|
|
error_log("Missing {$property} in data for " . get_called_class() . "::fillData");
|
|
|
|
|
}
|
|
|
|
|
return $model;
|
|
|
|
|
}
|
|
|
|
|
public function mapArray(Model $model, array $data): array
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->getProperties() as $property) {
|
|
|
|
|
try {
|
|
|
|
|
$column = $this->findColumnByProperty($property);
|
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (isset($data[$column])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$m = $this->getMethod($property);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
$val = $model->{$m}();
|
|
|
|
|
$data[$column] = $val;
|
|
|
|
|
$data[$column] = $model->{$m}();
|
|
|
|
|
}
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
@ -283,7 +215,7 @@ abstract class Repository implements RepositoryInterface
|
|
|
|
|
$this->update($model);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$values = $this->mapArray($model, []);
|
|
|
|
|
$values = array_replace(array_flip($this->getColumns()), $this->mapArray($model, []));
|
|
|
|
|
$cols = array_fill(0, count($values), '?');
|
|
|
|
|
$query = $this->getQueryBuilder()->insert($this->getTable())->columns($this->getColumns())->values($cols);
|
|
|
|
|
$this->getConnection()->execute($query, array_values($values));
|
|
|
|
|