5 Commits
2.2.1 ... 2.2.3

Author SHA1 Message Date
02f8bb0b4f FIX 2022-09-12 17:29:32 -03:00
d3771d8844 FIX: condition in old format 2022-09-12 17:29:04 -03:00
10e87b71a3 Update in Repository 2022-09-12 17:23:55 -03:00
aff0d4333d Added update to Repository 2022-09-12 17:23:29 -03:00
51124218d5 Check if value is set before serializing 2022-09-09 17:47:46 -04:00
3 changed files with 55 additions and 7 deletions

View File

@ -79,6 +79,9 @@ abstract class Model implements ModelInterface
continue;
}
$p = strtolower(str_replace('get', '', $method->getName()));
if (!isset($this->{$p})) {
continue;
}
$output[$p] = $this->{$method->getName()}();
}
return $output;

View File

@ -131,6 +131,22 @@ abstract class Repository implements RepositoryInterface
return $this->optional ?? [];
}
public function getMethod(string $column, bool $get = true): string
{
$m = str_replace(' ', '', ucwords(str_replace('_', ' ', $column)));
if ($get) {
return "get{$m}";
}
return "set{$m}";
}
public function getProperty(string $method): string
{
$parts = preg_split('/(?=[A-Z])/', $method);
if (in_array(strtolower($parts[0]), ['get', 'set'])) {
array_shift($parts);
}
return strtolower(implode('_', $parts));
}
public function fillData(Model $model, array $data): Model
{
foreach ($this->getRequired() as $column) {
@ -145,6 +161,18 @@ abstract class Repository implements RepositoryInterface
}
return $model;
}
public function mapArray(Model $model, array $data): array
{
foreach ($this->getColumns() as $column) {
$m = $this->getMethod($column);
$val = $model->{$m}();
if (isset($data[$column])) {
continue;
}
$data[$column] = $val;
}
return $data;
}
public function load(array $data): Model
{
return $this->fillData($this->getNewModel()
@ -155,16 +183,29 @@ abstract class Repository implements RepositoryInterface
if (!$model->isDirty() and !$model->isNew()) {
return;
}
$cols = [];
$values = [];
foreach ($this->getColumns() as $column) {
$m = 'get' . ucwords($column);
$cols []= '?';
$values []= $model->{$m}();
if (!$model->isNew()) {
$this->update($model);
return;
}
$values = $this->mapArray($model, []);
$cols = array_fill(0, count($values), '?');
$query = $this->getQueryBuilder()->insert($this->getTable())->columns($this->getColumns())->values($cols);
$this->getConnection()->execute($query, $values);
}
public function update(Model $model): void
{
if (!$model->isDirty() and !$model->isNew()) {
return;
}
$values = $this->mapArray($model, []);
$cols = array_map(function($column) {
return "{$column} = ?";
}, $values);
$values = array_values($values);
$values []= $model->getId();
$query = $this->getQueryBuilder()->update($this->getTable())->set($cols)->where(['id = ?']);
$this->getConnection()->execute($query, $values);
}
public function create(array $data): Model
{
return $this->fillData($this->getNewModel()
@ -191,7 +232,7 @@ abstract class Repository implements RepositoryInterface
$query = $this->getQueryBuilder()
->select()
->from($this->getTable())
->where([['id', '?']])
->where(['id = ?'])
->limit(1);
return $this->load($this->getConnection()->execute($query, [$id])->getAsArray()[0]);
}

View File

@ -28,9 +28,13 @@ interface Repository
public function setOptional(array $columns): Repository;
public function addOptional(string $column): Repository;
public function getOptional(): array;
public function getMethod(string $column, bool $get = true): string;
public function getProperty(string $method): string;
public function fillData(Model $model, array $data): Model;
public function mapArray(Model $model, array $data): array;
public function load(array $data): Model;
public function save(Model $model): void;
public function update(Model $model): void;
public function create(array $data): Model;
public function edit(Model $model, array $data): Model;
public function delete(Model $model): void;