make PersistentDataManager::$fillWithData private
All checks were successful
soko-web/pipeline/pr-master This commit looks good

This commit is contained in:
Bence Pőcze 2023-04-30 19:20:25 +02:00
parent ad7ea0de9d
commit e37ea7c09c
Signed by: bence
GPG Key ID: DC5BD6E95A333E6D

View File

@ -58,53 +58,6 @@ class PersistentDataManager implements IPersistentDataManager
return $this->selectFromDb($select, $type, $useRelations, $withRelations);
}
public function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void
{
$relations = $model::getRelations();
if (count($withRelations)) {
$relations = array_intersect_key($relations, array_flip($withRelations));
}
while (key($data)) {
$key = key($data);
$value = current($data);
$relation = key($relations);
if (strpos($key, '__') === false) {
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
if (method_exists($model, $method) && isset($value)) {
$model->$method($value);
}
next($data);
} else if (isset($modelKey) && substr($key, 0, strlen($modelKey . '__')) === $modelKey . '__') {
$key = substr($key, strlen($modelKey) + 2);
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
if (method_exists($model, $method) && isset($value)) {
$model->$method($value);
}
next($data);
} else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
$relationType = current($relations);
$relationModel = new $relationType();
$this->fillWithData($data, $relationModel, $withRelations, $relation);
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
$model->$method($relationModel);
next($relations);
} else {
return;
}
}
$model->saveSnapshot();
}
public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void
{
$relations = $model::getRelations();
@ -240,6 +193,53 @@ class PersistentDataManager implements IPersistentDataManager
}
}
private function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void
{
$relations = $model::getRelations();
if (count($withRelations)) {
$relations = array_intersect_key($relations, array_flip($withRelations));
}
while (key($data)) {
$key = key($data);
$value = current($data);
$relation = key($relations);
if (strpos($key, '__') === false) {
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
if (method_exists($model, $method) && isset($value)) {
$model->$method($value);
}
next($data);
} else if (isset($modelKey) && substr($key, 0, strlen($modelKey . '__')) === $modelKey . '__') {
$key = substr($key, strlen($modelKey) + 2);
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
if (method_exists($model, $method) && isset($value)) {
$model->$method($value);
}
next($data);
} else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
$relationType = current($relations);
$relationModel = new $relationType();
$this->fillWithData($data, $relationModel, $withRelations, $relation);
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
$model->$method($relationModel);
next($relations);
} else {
return;
}
}
$model->saveSnapshot();
}
private function syncRelations(Model $model): void
{
foreach ($model::getRelations() as $relation => $relationType) {