diff --git a/src/PersistentData/PersistentDataManager.php b/src/PersistentData/PersistentDataManager.php index c203b8b..f87f022 100644 --- a/src/PersistentData/PersistentDataManager.php +++ b/src/PersistentData/PersistentDataManager.php @@ -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) {