|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Meilisearch\Bundle\Model; |
| 6 | + |
| 7 | +/** |
| 8 | + * @template T of object |
| 9 | + */ |
| 10 | +final class SearchResults implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonSerializable |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var array<T> |
| 14 | + */ |
| 15 | + private readonly array $hits; |
| 16 | + private readonly int $hitsCount; |
| 17 | + private readonly string $query; |
| 18 | + private readonly int $processingTimeMs; |
| 19 | + |
| 20 | + private readonly ?int $limit; |
| 21 | + private readonly ?int $offset; |
| 22 | + private readonly ?int $estimatedTotalHits; |
| 23 | + private readonly ?int $nbHits; |
| 24 | + |
| 25 | + private readonly ?int $page; |
| 26 | + private readonly ?int $hitsPerPage; |
| 27 | + private readonly ?int $totalHits; |
| 28 | + private readonly ?int $totalPages; |
| 29 | + |
| 30 | + private readonly int $semanticHitCount; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var array<string, mixed> |
| 34 | + */ |
| 35 | + private readonly array $facetDistribution; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var array<string, mixed> |
| 39 | + */ |
| 40 | + private readonly array $facetStats; |
| 41 | + |
| 42 | + private readonly ?string $requestUid; |
| 43 | + |
| 44 | + private readonly array $raw; |
| 45 | + |
| 46 | + /** |
| 47 | + * @param array<T> $hits |
| 48 | + * @param array<string, mixed> $facetDistribution |
| 49 | + * @param array<string, mixed> $facetStats |
| 50 | + */ |
| 51 | + public function __construct( |
| 52 | + array $hits, |
| 53 | + string $query, |
| 54 | + int $processingTimeMs, |
| 55 | + |
| 56 | + ?int $limit = null, |
| 57 | + ?int $offset = null, |
| 58 | + ?int $estimatedTotalHits = null, |
| 59 | + ?int $nbHits = null, |
| 60 | + |
| 61 | + ?int $page = null, |
| 62 | + ?int $hitsPerPage = null, |
| 63 | + ?int $totalHits = null, |
| 64 | + ?int $totalPages = null, |
| 65 | + |
| 66 | + int $semanticHitCount = 0, |
| 67 | + |
| 68 | + array $facetDistribution = [], |
| 69 | + array $facetStats = [], |
| 70 | + |
| 71 | + ?string $requestUid = null, |
| 72 | + |
| 73 | + array $raw = [], |
| 74 | + ) { |
| 75 | + $this->hits = $hits; |
| 76 | + $this->hitsCount = \count($hits); |
| 77 | + $this->query = $query; |
| 78 | + $this->processingTimeMs = $processingTimeMs; |
| 79 | + |
| 80 | + $this->limit = $limit; |
| 81 | + $this->offset = $offset; |
| 82 | + $this->estimatedTotalHits = $estimatedTotalHits; |
| 83 | + $this->nbHits = $nbHits; |
| 84 | + |
| 85 | + $this->page = $page; |
| 86 | + $this->hitsPerPage = $hitsPerPage; |
| 87 | + $this->totalHits = $totalHits; |
| 88 | + $this->totalPages = $totalPages; |
| 89 | + |
| 90 | + $this->semanticHitCount = $semanticHitCount; |
| 91 | + |
| 92 | + $this->facetDistribution = $facetDistribution; |
| 93 | + $this->facetStats = $facetStats; |
| 94 | + |
| 95 | + $this->requestUid = $requestUid; |
| 96 | + |
| 97 | + $this->raw = $raw; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @return array<T> |
| 102 | + */ |
| 103 | + public function getHits(): array |
| 104 | + { |
| 105 | + return $this->hits; |
| 106 | + } |
| 107 | + |
| 108 | + public function getHitsCount(): int |
| 109 | + { |
| 110 | + return $this->hitsCount; |
| 111 | + } |
| 112 | + |
| 113 | + public function getQuery(): string |
| 114 | + { |
| 115 | + return $this->query; |
| 116 | + } |
| 117 | + |
| 118 | + public function getProcessingTimeMs(): int |
| 119 | + { |
| 120 | + return $this->processingTimeMs; |
| 121 | + } |
| 122 | + |
| 123 | + public function getLimit(): ?int |
| 124 | + { |
| 125 | + return $this->limit; |
| 126 | + } |
| 127 | + |
| 128 | + public function getOffset(): ?int |
| 129 | + { |
| 130 | + return $this->offset; |
| 131 | + } |
| 132 | + |
| 133 | + public function getEstimatedTotalHits(): ?int |
| 134 | + { |
| 135 | + return $this->estimatedTotalHits; |
| 136 | + } |
| 137 | + |
| 138 | + public function getNbHits(): ?int |
| 139 | + { |
| 140 | + return $this->nbHits; |
| 141 | + } |
| 142 | + |
| 143 | + public function getPage(): ?int |
| 144 | + { |
| 145 | + return $this->page; |
| 146 | + } |
| 147 | + |
| 148 | + public function getHitsPerPage(): ?int |
| 149 | + { |
| 150 | + return $this->hitsPerPage; |
| 151 | + } |
| 152 | + |
| 153 | + public function getTotalHits(): ?int |
| 154 | + { |
| 155 | + return $this->totalHits; |
| 156 | + } |
| 157 | + |
| 158 | + public function getTotalPages(): ?int |
| 159 | + { |
| 160 | + return $this->totalPages; |
| 161 | + } |
| 162 | + |
| 163 | + public function getSemanticHitCount(): int |
| 164 | + { |
| 165 | + return $this->semanticHitCount; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @return array<string, mixed> |
| 170 | + */ |
| 171 | + public function getFacetDistribution(): array |
| 172 | + { |
| 173 | + return $this->facetDistribution; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @return array<string, mixed> |
| 178 | + */ |
| 179 | + public function getFacetStats(): array |
| 180 | + { |
| 181 | + return $this->facetStats; |
| 182 | + } |
| 183 | + |
| 184 | + public function getRequestUid(): ?string |
| 185 | + { |
| 186 | + return $this->requestUid; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * @return array<string, mixed> |
| 191 | + */ |
| 192 | + public function getRaw(): array |
| 193 | + { |
| 194 | + return $this->raw; |
| 195 | + } |
| 196 | + |
| 197 | + public function offsetExists(mixed $offset): bool |
| 198 | + { |
| 199 | + return isset($this->hits[$offset]); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * @return T |
| 204 | + */ |
| 205 | + public function offsetGet(mixed $offset): mixed |
| 206 | + { |
| 207 | + return $this->hits[$offset]; |
| 208 | + } |
| 209 | + |
| 210 | + public function offsetSet(mixed $offset, mixed $value): void |
| 211 | + { |
| 212 | + throw new \LogicException('Cannot modify hits'); |
| 213 | + } |
| 214 | + |
| 215 | + public function offsetUnset(mixed $offset): void |
| 216 | + { |
| 217 | + throw new \LogicException('Cannot modify hits'); |
| 218 | + } |
| 219 | + |
| 220 | + public function count(): int |
| 221 | + { |
| 222 | + return \count($this->hits); |
| 223 | + } |
| 224 | + |
| 225 | + public function getIterator(): \Traversable |
| 226 | + { |
| 227 | + return new \ArrayIterator($this->hits); |
| 228 | + } |
| 229 | + |
| 230 | + public function jsonSerialize(): array |
| 231 | + { |
| 232 | + return [ |
| 233 | + 'hits' => $this->hits, |
| 234 | + 'query' => $this->query, |
| 235 | + 'processingTimeMs' => $this->processingTimeMs, |
| 236 | + 'limit' => $this->limit, |
| 237 | + 'offset' => $this->offset, |
| 238 | + 'estimatedTotalHits' => $this->estimatedTotalHits, |
| 239 | + 'nbHits' => $this->nbHits, |
| 240 | + 'page' => $this->page, |
| 241 | + 'hitsPerPage' => $this->hitsPerPage, |
| 242 | + 'totalHits' => $this->totalHits, |
| 243 | + 'totalPages' => $this->totalPages, |
| 244 | + 'semanticHitCount' => $this->semanticHitCount, |
| 245 | + 'facetDistribution' => $this->facetDistribution, |
| 246 | + 'facetStats' => $this->facetStats, |
| 247 | + 'requestUid' => $this->requestUid, |
| 248 | + ]; |
| 249 | + } |
| 250 | +} |
0 commit comments