|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PHPVector\Metadata; |
| 6 | + |
| 7 | +use PHPVector\Document; |
| 8 | +use PHPVector\MetadataFilter; |
| 9 | + |
| 10 | +/** |
| 11 | + * Evaluates whether a document's metadata matches a set of filters. |
| 12 | + * |
| 13 | + * Filter logic: |
| 14 | + * - Top-level filters are ANDed together |
| 15 | + * - Nested arrays (array of MetadataFilter) are ORed within the group |
| 16 | + * - Example: [[$f1, $f2], $f3] = (f1 OR f2) AND f3 |
| 17 | + */ |
| 18 | +final class MetadataFilterEvaluator |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Check if a document's metadata matches all filters. |
| 22 | + * |
| 23 | + * @param Document $document The document to check |
| 24 | + * @param array<MetadataFilter|array<MetadataFilter>> $filters Filters to apply |
| 25 | + * @return bool True if document matches all filters |
| 26 | + */ |
| 27 | + public function matches(Document $document, array $filters): bool |
| 28 | + { |
| 29 | + foreach ($filters as $filter) { |
| 30 | + if (is_array($filter)) { |
| 31 | + // OR group: at least one must match |
| 32 | + if (!$this->matchesOrGroup($document, $filter)) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + } elseif (!$this->matchesSingleFilter($document, $filter)) { |
| 36 | + // Single filter |
| 37 | + return false; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Check if document matches at least one filter in an OR group. |
| 46 | + * |
| 47 | + * @param Document $document The document to check |
| 48 | + * @param array<MetadataFilter> $filters OR group of filters |
| 49 | + * @return bool True if at least one filter matches |
| 50 | + */ |
| 51 | + private function matchesOrGroup(Document $document, array $filters): bool |
| 52 | + { |
| 53 | + foreach ($filters as $filter) { |
| 54 | + if ($this->matchesSingleFilter($document, $filter)) { |
| 55 | + return true; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Check if document matches a single filter. |
| 64 | + * |
| 65 | + * @param Document $document The document to check |
| 66 | + * @param MetadataFilter $filter The filter to apply |
| 67 | + * @return bool True if a document matches the filter |
| 68 | + */ |
| 69 | + private function matchesSingleFilter(Document $document, MetadataFilter $filter): bool |
| 70 | + { |
| 71 | + $metadata = $document->metadata; |
| 72 | + |
| 73 | + // Missing metadata key returns false |
| 74 | + if (!array_key_exists($filter->key, $metadata)) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + $metadataValue = $metadata[$filter->key]; |
| 79 | + $filterValue = $filter->value; |
| 80 | + |
| 81 | + return match ($filter->operator) { |
| 82 | + '=' => $metadataValue === $filterValue, |
| 83 | + '!=' => $metadataValue !== $filterValue, |
| 84 | + '<' => $metadataValue < $filterValue, |
| 85 | + '<=' => $metadataValue <= $filterValue, |
| 86 | + '>' => $metadataValue > $filterValue, |
| 87 | + '>=' => $metadataValue >= $filterValue, |
| 88 | + 'in' => is_array($filterValue) && in_array($metadataValue, $filterValue, true), |
| 89 | + 'not_in' => is_array($filterValue) && !in_array($metadataValue, $filterValue, true), |
| 90 | + 'contains' => $this->evaluateContains($metadataValue, $filterValue), |
| 91 | + default => false, |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + private function evaluateContains(mixed $metadataValue, mixed $filterValue): bool |
| 96 | + { |
| 97 | + if (!is_array($metadataValue)) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + return in_array($filterValue, $metadataValue, true); |
| 102 | + } |
| 103 | +} |
0 commit comments