add($rule); } } public function add(array $rule): void { $domain = trim((string) ($rule['domain'] ?? '')); $type = strtolower(trim((string) ($rule['type'] ?? ''))); $values = array_values(array_filter((array) ($rule['values'] ?? []), static fn ($value): bool => trim((string) $value) !== '')); if ($domain === '' || ! in_array($type, self::TYPES, true) || $values === []) { throw new \InvalidArgumentException('A domain rule requires a domain, supported type and values.'); } $this->rules[] = [ 'domain' => $domain, 'type' => $type, 'values' => $values, 'weight' => max(0.0, min(1.0, (float) ($rule['weight'] ?? 0.0))), 'reason' => trim((string) ($rule['reason'] ?? 'Matched a domain knowledge rule.')), ]; } public function getAll(): array { return $this->rules; } public function getByType(string $type): array { $normalizedType = strtolower(trim($type)); return array_values(array_filter( $this->rules, static fn (array $rule): bool => $rule['type'] === $normalizedType )); } public static function getSupportedTypes(): array { return self::TYPES; } }