Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
4ccc38ffac | |||
ef1603bc4b | |||
8531658899 | |||
f19385ccc1 | |||
02f8bb0b4f | |||
d3771d8844 | |||
10e87b71a3 | |||
aff0d4333d | |||
51124218d5 |
@ -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;
|
||||
|
@ -131,20 +131,58 @@ 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) {
|
||||
$m = 'set' . ucwords($column);
|
||||
$m = $this->getMethod($column, false);
|
||||
if (!method_exists($model, $m)) {
|
||||
continue;
|
||||
}
|
||||
$model->{$m}($data[$column]);
|
||||
}
|
||||
foreach ($this->getOptional() as $column) {
|
||||
if (isset($data[$column])) {
|
||||
$m = 'set' . ucwords($column);
|
||||
$model->{$m}($data[$column]);
|
||||
if (!isset($data[$column])) {
|
||||
continue;
|
||||
}
|
||||
$m = $this->getMethod($column, false);
|
||||
if (!method_exists($model, $m)) {
|
||||
continue;
|
||||
}
|
||||
$model->{$m}($data[$column]);
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
public function mapArray(Model $model, array $data): array
|
||||
{
|
||||
foreach ($this->getColumns() as $column) {
|
||||
if (isset($data[$column])) {
|
||||
continue;
|
||||
}
|
||||
$m = $this->getMethod($column);
|
||||
if (!method_exists($model, $m)) {
|
||||
continue;
|
||||
}
|
||||
$val = $model->{$m}();
|
||||
$data[$column] = $val;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
public function load(array $data): Model
|
||||
{
|
||||
return $this->fillData($this->getNewModel()
|
||||
@ -155,16 +193,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()
|
||||
@ -174,7 +225,10 @@ abstract class Repository implements RepositoryInterface
|
||||
{
|
||||
foreach ($this->getColumns() as $col) {
|
||||
if (isset($data[$col])) {
|
||||
$m = 'set' . ucwords($col);
|
||||
$m = $this->getMethod($col, false);
|
||||
if (!method_exists($model, $m)) {
|
||||
continue;
|
||||
}
|
||||
$model->{$m}($data[$col]);
|
||||
}
|
||||
}
|
||||
@ -191,7 +245,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]);
|
||||
}
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user