Compare commits
4 Commits
2.4.2
...
b8409182d7
Author | SHA1 | Date | |
---|---|---|---|
b8409182d7 | |||
100df73916 | |||
e576ef5054 | |||
5c1f424c76 |
@ -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,6 @@ abstract class Repository implements RepositoryInterface
|
|||||||
{
|
{
|
||||||
return $this->table;
|
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;
|
protected array $columns;
|
||||||
public function setColumns(array $columns): RepositoryInterface
|
public function setColumns(array $columns): RepositoryInterface
|
||||||
{
|
{
|
||||||
@ -142,7 +89,7 @@ abstract class Repository implements RepositoryInterface
|
|||||||
}
|
}
|
||||||
public function getColumns(): array
|
public function getColumns(): array
|
||||||
{
|
{
|
||||||
return $this->columns;
|
return $this->columns ?? array_merge($this->getRequired(), $this->getOptional());
|
||||||
}
|
}
|
||||||
protected array $properties;
|
protected array $properties;
|
||||||
public function getProperties(): array
|
public function getProperties(): array
|
||||||
@ -220,52 +167,37 @@ 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->getProperties() as $property) {
|
||||||
try {
|
|
||||||
$property = $this->findPropertyByColumn($column);
|
|
||||||
} catch (\InvalidArgumentException $e) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$m = $this->getMethod($property, false);
|
$m = $this->getMethod($property, false);
|
||||||
if (!method_exists($model, $m)) {
|
if (in_array($property, $this->getRequired())) {
|
||||||
|
$model->{$m}($data[$property]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$model->{$m}($data[$column]);
|
if (in_array("{$property}_id", $this->getRequired())) {
|
||||||
}
|
$model->{$m}($data["{$property}_id"]);
|
||||||
foreach ($this->getOptional() as $column) {
|
|
||||||
try {
|
|
||||||
$property = $this->findPropertyByColumn($column);
|
|
||||||
} catch (\InvalidArgumentException $e) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$m = $this->getMethod($property, false);
|
if (in_array($property, $this->getOptional()) and isset($data[$property])) {
|
||||||
if (!isset($data[$column])) {
|
$model->{$m}($data[$property]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!method_exists($model, $m)) {
|
if (in_array("{$property}_id", $this->getOptional()) and isset($data["{$property}_id"])) {
|
||||||
|
$model->{$m}($data["{$property}_id"]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$model->{$m}($data[$column]);
|
error_log("Missing {$property} in data for " . get_called_class() . "::fillData");
|
||||||
}
|
}
|
||||||
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->getColumns() as $column) {
|
||||||
try {
|
$m = $this->getMethod($column);
|
||||||
$column = $this->findColumnByProperty($property);
|
|
||||||
} catch (\InvalidArgumentException $e) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (isset($data[$column])) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$m = $this->getMethod($property);
|
|
||||||
if (!method_exists($model, $m)) {
|
if (!method_exists($model, $m)) {
|
||||||
|
error_log("Missing getter for {$column} in " . get_called_class() . "::mapArray");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$val = $model->{$m}();
|
$data[$column] = $model->{$m}();
|
||||||
$data[$column] = $val;
|
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
@ -283,7 +215,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));
|
||||||
|
@ -80,40 +80,6 @@ interface Repository
|
|||||||
*/
|
*/
|
||||||
public function setTable(string $table): Repository;
|
public function setTable(string $table): Repository;
|
||||||
|
|
||||||
/**
|
|
||||||
* Get Model - table mapping
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getMappings(): array;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $column
|
|
||||||
* @param string $property
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function addMapping(string $column, string $property): Repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set Model - table mappings
|
|
||||||
* @param array $mappings
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setMappings(array $mappings): Repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Find a property from mapping by column
|
|
||||||
* @param string $column
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function findPropertyByColumn(string $column): string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Find a column from mapping by property
|
|
||||||
* @param string $property
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function findColumnByProperty(string $property): string;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set columns in table
|
* Set columns in table
|
||||||
* @param array $columns
|
* @param array $columns
|
||||||
|
Reference in New Issue
Block a user