Abuse the translation engine to handle more of the formatting for us

This commit is contained in:
DaneEveritt 2022-06-18 15:28:42 -04:00
parent b052d29a5f
commit 95de4c30fc
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 94 additions and 51 deletions

View file

@ -2,6 +2,7 @@
namespace Pterodactyl\Transformers\Api\Client;
use Illuminate\Support\Str;
use Pterodactyl\Models\User;
use Pterodactyl\Models\ActivityLog;
@ -22,7 +23,7 @@ class ActivityLogTransformer extends BaseClientTransformer
'is_api' => !is_null($model->api_key_id),
'ip' => $model->ip,
'description' => $model->description,
'properties' => $model->properties ? $model->properties->toArray() : [],
'properties' => $this->properties($model),
'has_additional_metadata' => $this->hasAdditionalMetadata($model),
'timestamp' => $model->timestamp->toIso8601String(),
];
@ -37,6 +38,33 @@ class ActivityLogTransformer extends BaseClientTransformer
return $this->item($model->actor, $this->makeTransformer(UserTransformer::class), User::RESOURCE_NAME);
}
/**
* Transforms any array values in the properties into a countable field for easier
* use within the translation outputs.
*/
protected function properties(ActivityLog $model): array
{
if (!$model->properties || $model->properties->isEmpty()) {
return [];
}
$properties = $model->properties
->mapWithKeys(function ($value, $key) {
if (!is_array($value)) {
return [$key => $value];
}
return [$key => $value, "{$key}_count" => count($value)];
});
$keys = $properties->keys()->filter(fn ($key) => Str::endsWith($key, '_count'))->values();
if ($keys->containsOneItem()) {
$properties = $properties->merge(['count' => $properties->get($keys[0])])->except($keys[0]);
}
return $properties->toArray();
}
/**
* Determines if there are any log properties that we've not already exposed
* in the response language string and that are not just the IP address or