Automated PR provided by Codex via Gitea API. Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #484
94 lines
2.5 KiB
PHP
94 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\TenantConfiguration;
|
|
|
|
final class CoverageSecondaryKeyBuilder
|
|
{
|
|
public function __construct(
|
|
private readonly CoveragePayloadRedactor $redactor,
|
|
) {}
|
|
|
|
/**
|
|
* @param array<string, mixed> $strategy
|
|
* @param array<string, mixed> $payload
|
|
* @param array<string, mixed> $sourceMetadata
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function build(array $strategy, array $payload, array $sourceMetadata = []): array
|
|
{
|
|
$fields = collect([
|
|
...$this->list($strategy['display_fields'] ?? []),
|
|
...$this->list($strategy['secondary_fields'] ?? []),
|
|
])->unique()->values()->all();
|
|
|
|
$keys = [];
|
|
|
|
foreach ($fields as $field) {
|
|
$value = $this->value($field, $payload, $sourceMetadata);
|
|
|
|
if ($value === null || $value === '') {
|
|
continue;
|
|
}
|
|
|
|
$redacted = $this->redactor->redact([$field => $value]);
|
|
$keys[$field] = $this->bounded(is_array($redacted) ? ($redacted[$field] ?? null) : $redacted);
|
|
}
|
|
|
|
return $keys;
|
|
}
|
|
|
|
/**
|
|
* @return list<string>
|
|
*/
|
|
private function list(mixed $fields): array
|
|
{
|
|
if (! is_array($fields)) {
|
|
return [];
|
|
}
|
|
|
|
return array_values(array_filter(
|
|
array_map(static fn (mixed $field): string => is_string($field) ? trim($field) : '', $fields),
|
|
static fn (string $field): bool => $field !== '',
|
|
));
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $payload
|
|
* @param array<string, mixed> $sourceMetadata
|
|
*/
|
|
private function value(string $field, array $payload, array $sourceMetadata): mixed
|
|
{
|
|
if (str_starts_with($field, 'source_metadata.')) {
|
|
return data_get($sourceMetadata, substr($field, 16));
|
|
}
|
|
|
|
if (str_starts_with($field, 'payload.')) {
|
|
return data_get($payload, substr($field, 8));
|
|
}
|
|
|
|
return data_get($payload, $field);
|
|
}
|
|
|
|
private function bounded(mixed $value): mixed
|
|
{
|
|
if (is_string($value)) {
|
|
return mb_substr($value, 0, 240);
|
|
}
|
|
|
|
if (is_array($value)) {
|
|
return collect($value)
|
|
->take(12)
|
|
->map(fn (mixed $nested): mixed => $this->bounded($nested))
|
|
->all();
|
|
}
|
|
|
|
if (is_scalar($value) || $value === null) {
|
|
return $value;
|
|
}
|
|
|
|
return (string) $value;
|
|
}
|
|
}
|