last step

This commit is contained in:
team2
2026-04-29 22:22:57 +02:00
parent 8ece67b461
commit d618265044
12 changed files with 918 additions and 656 deletions

View File

@@ -4,31 +4,18 @@ declare(strict_types=1);
namespace App\Config;
/**
* YAML-backed query-enrichment configuration.
*
* This class intentionally has no PHP fallback values. Missing or invalid
* configuration must be fixed in config/retriex/query_enrichment.yaml.
*/
final readonly class QueryEnricherConfig
{
/**
* Backwards-compatible fallback vocabulary.
* Active values are loaded from retriex.query_enrichment.config when present.
*
* @var array<int|string, mixed>
*/
private const DEFAULT_ENRICH_QUERY_LIST = [
'Wasserhärte' => 'Resthärte',
'Gerät' => 'Modell',
'Indikator' => 'Chemie',
'Seminar' => 'Webinar',
'Schulung' => 'Seminar',
'Indikatoren' => 'Indikator',
'Wasserhärte-Grenzwert' => 'Resthärte',
'Resthärte-Grenzwert' => 'Wasserhärte',
'Grenzwert' => 'Überwachungsbereich',
'store' => 'shop',
];
/**
* @param array<string, mixed> $config
*/
public function __construct(private array $config = [])
public function __construct(private array $config)
{
}
@@ -52,11 +39,7 @@ final readonly class QueryEnricherConfig
public function getEnrichQueryList(): array
{
$normalized = [];
$rules = $this->config['rules'] ?? self::DEFAULT_ENRICH_QUERY_LIST;
if (!is_array($rules)) {
$rules = self::DEFAULT_ENRICH_QUERY_LIST;
}
$rules = $this->requiredArray('rules');
foreach ($rules as $key => $value) {
if (is_array($value)) {
@@ -76,18 +59,16 @@ final readonly class QueryEnricherConfig
}
}
if ($normalized === []) {
throw new \InvalidArgumentException('RetrieX query enrichment config key "rules" must contain at least one valid enrichment rule.');
}
return $normalized;
}
public function getMaxExpansions(): int
{
$value = $this->config['max_expansions'] ?? 4;
if (!is_numeric($value)) {
return 4;
}
return max(0, (int) $value);
return $this->requiredNonNegativeInt('max_expansions');
}
/**
@@ -160,6 +141,49 @@ final readonly class QueryEnricherConfig
return true;
}
/** @return array<int|string, mixed> */
private function requiredArray(string $key): array
{
if (!array_key_exists($key, $this->config)) {
throw new \InvalidArgumentException(sprintf('Missing required RetrieX query enrichment config key "%s".', $key));
}
$value = $this->config[$key];
if (!is_array($value)) {
throw new \InvalidArgumentException(sprintf('RetrieX query enrichment config key "%s" must be an array.', $key));
}
if ($value === []) {
throw new \InvalidArgumentException(sprintf('RetrieX query enrichment config key "%s" must not be empty.', $key));
}
return $value;
}
private function requiredNonNegativeInt(string $key): int
{
if (!array_key_exists($key, $this->config)) {
throw new \InvalidArgumentException(sprintf('Missing required RetrieX query enrichment config key "%s".', $key));
}
$value = $this->config[$key];
if (is_int($value)) {
$intValue = $value;
} elseif (is_string($value) && preg_match('/^-?\d+$/', trim($value)) === 1) {
$intValue = (int) trim($value);
} else {
throw new \InvalidArgumentException(sprintf('RetrieX query enrichment config key "%s" must be an integer.', $key));
}
if ($intValue < 0) {
throw new \InvalidArgumentException(sprintf('RetrieX query enrichment config key "%s" must be greater than or equal to 0.', $key));
}
return $intValue;
}
private function normalizePhrase(string $value): string
{
$value = trim($value);