BranchModificationStore: fix serialization

This commit is contained in:
Thomas Gelf 2021-08-24 18:46:55 +02:00
parent 607f53fecc
commit 23f1fae784

View File

@ -152,12 +152,12 @@ class BranchModificationStore
if ($objectId) { if ($objectId) {
$existing = $this->fetchOptional($objectId, $branchUuid); $existing = $this->fetchOptional($objectId, $branchUuid);
foreach ($this->encodedDictionaries as $property) { foreach ($this->encodedDictionaries as $property) {
$this->extractFlatDictionary($properties, $existing, $property); $this->combineAndEncodeFlatDictionaries($properties, $existing, $property);
} }
} else { } else {
$existing = null; $existing = null;
} }
foreach (array_merge($this->encodedArrays, $this->encodedDictionaries) as $deepProperty) { foreach ($this->encodedArrays as $deepProperty) {
if (isset($properties[$deepProperty])) { if (isset($properties[$deepProperty])) {
// TODO: flags // TODO: flags
$properties[$deepProperty] = Json::encode($properties[$deepProperty]); $properties[$deepProperty] = Json::encode($properties[$deepProperty]);
@ -194,7 +194,7 @@ class BranchModificationStore
}); });
} }
protected function extractFlatDictionary(&$properties, $existing, $prefix) protected function combineAndEncodeFlatDictionaries(&$properties, $existing, $prefix)
{ {
if ($existing && ! empty($existing->$prefix)) { if ($existing && ! empty($existing->$prefix)) {
// $vars = (array) Json::decode($existing->vars); // $vars = (array) Json::decode($existing->vars);
@ -202,18 +202,17 @@ class BranchModificationStore
} else { } else {
$vars = []; $vars = [];
} }
$length = strlen($prefix) + 1;
foreach ($properties as $key => $value) { foreach ($properties as $key => $value) {
if (substr($key, 0, 5) === "$prefix.") { if (substr($key, 0, $length) === "$prefix.") {
$vars[substr($key, 5)] = $value; $vars[substr($key, $length)] = $value;
} }
} }
if (! empty($vars)) { if (! empty($vars)) {
foreach (array_keys($vars) as $key) { foreach (array_keys($vars) as $key) {
unset($properties["$prefix.$key"]); unset($properties["$prefix.$key"]);
} }
// This is on store only??
$properties[$prefix] = Json::encode((object) $vars); // TODO: flags! $properties[$prefix] = Json::encode((object) $vars); // TODO: flags!
/// $properties['vars'] = (object) $vars;
} }
} }