diff --git a/src/StorageDrivers/DatabaseStorageDriver.php b/src/StorageDrivers/DatabaseStorageDriver.php index 23ee0a9f..4a75b079 100644 --- a/src/StorageDrivers/DatabaseStorageDriver.php +++ b/src/StorageDrivers/DatabaseStorageDriver.php @@ -32,9 +32,9 @@ class DatabaseStorageDriver implements StorageDriver public function getTenantById(string $uuid, array $fields = []): array { if ($fields) { - return Tenant::find($uuid)->only($fields); + return Tenant::decodeData(Tenant::find($uuid)->only($fields)); } else { - return Tenant::find($uuid)->toArray(); + return Tenant::find($uuid)->decoded(); } } diff --git a/src/Tenant.php b/src/Tenant.php index 72bef212..bf13800c 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -37,15 +37,35 @@ class Tenant extends Model { $tenants = $uuids ? static::findMany($uuids) : static::all(); - return $tenants->map(function ($tenant) { - $tenant = (array) $tenant->attributes; - foreach (json_decode($tenant[static::dataColumn()], true) as $key => $value) { - $tenant[$key] = $value; - } - unset($tenant[static::dataColumn()]); // todo what if 'data' key is stored in tenant storage? + return $tenants->map([__CLASS__, 'decodeData'])->toBase(); + } - return $tenant; - })->toBase(); + public function decoded() + { + return static::decodeData($this); + } + + /** + * Return a tenant array with data decoded into separate keys. + * + * @param Tenant|array $tenant + * @return array + */ + public static function decodeData($tenant) + { + $tenant = $tenant instanceof self ? (array) $tenant->attributes : $tenant; + $decoded = json_decode($tenant[$dataColumn = static::dataColumn()], true); + + foreach ($decoded as $key => $value) { + $tenant[$key] = $value; + } + + // If $tenant[$dataColumn] has been overriden by a value, don't delete the key. + if (! array_key_exists($dataColumn, $decoded)) { + unset($tenant[$dataColumn]); + } + + return $tenant; } public function getFromData(string $key) diff --git a/src/TenantManager.php b/src/TenantManager.php index ce6cf656..5f75d918 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -118,7 +118,12 @@ final class TenantManager { $fields = (array) $fields; - return $this->jsonDecodeArrayValues($this->storage->getTenantById($uuid, $fields)); + $tenant = $this->storage->getTenantById($uuid, $fields); + if ($this->useJson()) { + $tenant = $this->jsonDecodeArrayValues($tenant); + } + + return $tenant; } /** @@ -269,7 +274,7 @@ final class TenantManager $uuid = $uuid ?: $this->tenant['uuid']; if (\is_array($key)) { - return $this->jsonDecodeArrayValues($this->storage->getMany($uuid, $key)); + return $this->jsonDecodeArrayValues($this->storage->getMany($uuid, $key)); // todo is this correct? } return json_decode($this->storage->get($uuid, $key), true); @@ -321,7 +326,7 @@ final class TenantManager $key[$k] = json_encode($v); } - return $this->jsonDecodeArrayValues($this->storage->putMany($uuid, $key)); + return $this->jsonDecodeArrayValues($this->storage->putMany($uuid, $key)); // todo is this correct? } /**