mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-04 17:04:03 +00:00
wip
This commit is contained in:
parent
fefe12dab4
commit
0176579e80
3 changed files with 38 additions and 13 deletions
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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?
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue