CollectionValidator.php
- Pfad:
src/Infrastructure/Validation/CollectionValidator.php - Namespace: Infrastructure\Validation
- Zeilen: 148 | Größe: 4,719 Bytes
- Geändert: 2025-12-23 08:06:28 | Gescannt: 2025-12-31 10:22:15
Code Hygiene Score: 100
- Dependencies: 100 (25%)
- LOC: 100 (20%)
- Methods: 100 (20%)
- Secrets: 100 (15%)
- Classes: 100 (10%)
- Magic Numbers: 100 (10%)
Keine Issues gefunden.
Dependencies 2
- constructor Infrastructure\Persistence\CollectionRepository
- use Infrastructure\Persistence\CollectionRepository
Klassen 1
-
CollectionValidatorclass Zeile 11
Funktionen 5
-
__construct()public Zeile 13 -
validateSelection()public Zeile 25 -
validateEmbedding()Zeile 66 -
validateSearchRequest()Zeile 97 -
validateSearchable()Zeile 124
Verwendet von 7
- ContentCollectionService.php constructor
- ContentCollectionService.php use
- ContentServiceProvider.php use
- ManageChatSessionsUseCase.php constructor
- ManageChatSessionsUseCase.php use
- UpdateChatSessionUseCase.php constructor
- UpdateChatSessionUseCase.php use
Versionen 1
-
v1
2025-12-23 08:06 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
Code
<?php
declare(strict_types=1);
namespace Infrastructure\Validation;
// @responsibility: Validiert RAG-Collection-Kompatibilität (Vektor-Dimensionen)
use Infrastructure\Persistence\CollectionRepository;
final class CollectionValidator
{
public function __construct(
private CollectionRepository $repository
) {
}
/**
* Validate that all selected collections have compatible vector sizes.
*
* Returns ValidationResult with vector_size as data if valid.
*
* @param array<string> $collectionIds Array of collection_id values
*/
public function validateSelection(array $collectionIds): ValidationResult
{
if (count($collectionIds) === 0) {
return ValidationResult::ok();
}
$collections = $this->repository->findByIds($collectionIds);
// Check all collections exist
$foundIds = array_column($collections, 'collection_id');
$missing = array_diff($collectionIds, $foundIds);
if (!empty($missing)) {
return ValidationResult::error(
'Collections nicht gefunden: ' . implode(', ', $missing)
);
}
// Check all have same vector_size
$vectorSizes = array_unique(array_column($collections, 'vector_size'));
if (count($vectorSizes) > 1) {
$details = [];
foreach ($collections as $col) {
$details[] = "{$col['display_name']}: {$col['vector_size']}d";
}
return ValidationResult::error(
'Inkompatible Collections: Unterschiedliche Vektorgrößen ('
. implode(', ', $details) . ')'
);
}
return ValidationResult::ok((int) ($vectorSizes[0] ?? 0));
}
/**
* Validate that an embedding vector matches the expected collection dimension.
*
* @param string $collectionId The collection to check against
* @param array<float> $embedding The embedding vector
*/
public function validateEmbedding(string $collectionId, array $embedding): ValidationResult
{
$collection = $this->repository->find($collectionId);
if ($collection === null) {
return ValidationResult::error(
"Collection '$collectionId' nicht gefunden"
);
}
$expectedSize = (int) $collection['vector_size'];
$actualSize = count($embedding);
if ($expectedSize !== $actualSize) {
return ValidationResult::error(
"Dimension Mismatch: Collection '{$collection['display_name']}' "
. "erwartet {$expectedSize}d, Embedding hat {$actualSize}d"
);
}
return ValidationResult::ok();
}
/**
* Validate embedding against multiple collections.
*
* First validates collection compatibility, then checks embedding dimension.
*
* @param array<string> $collectionIds Collections to search
* @param array<float> $embedding The embedding vector
*/
public function validateSearchRequest(array $collectionIds, array $embedding): ValidationResult
{
// First validate collections are compatible
$selectionResult = $this->validateSelection($collectionIds);
if (!$selectionResult->isValid()) {
return $selectionResult;
}
// Then check embedding dimension
$expectedSize = $selectionResult->getData();
if ($expectedSize !== null && $expectedSize > 0) {
$actualSize = count($embedding);
if ($expectedSize !== $actualSize) {
return ValidationResult::error(
"Embedding hat {$actualSize}d, Collections erwarten {$expectedSize}d"
);
}
}
return ValidationResult::ok();
}
/**
* Check if collections exist and are searchable.
*
* @param array<string> $collectionIds
*/
public function validateSearchable(array $collectionIds): ValidationResult
{
if (empty($collectionIds)) {
return ValidationResult::ok();
}
$collections = $this->repository->findByIds($collectionIds);
foreach ($collections as $col) {
if (!$col['is_active']) {
return ValidationResult::error(
"Collection '{$col['display_name']}' ist nicht aktiv"
);
}
if (!$col['is_searchable']) {
return ValidationResult::error(
"Collection '{$col['display_name']}' ist nicht durchsuchbar"
);
}
}
return ValidationResult::ok();
}
}