|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Meilisearch\Search; |
| 6 | + |
| 7 | +class SimilarDocumentsSearchResult implements \Countable, \IteratorAggregate |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @var array<int, array<string, mixed>> |
| 11 | + */ |
| 12 | + private array $hits; |
| 13 | + |
| 14 | + /** |
| 15 | + * `estimatedTotalHits` is the attributes returned by the Meilisearch server |
| 16 | + * and its value will not be modified by the methods in this class. |
| 17 | + * Please, use `hitsCount` if you want to know the real size of the `hits` array at any time. |
| 18 | + */ |
| 19 | + private int $estimatedTotalHits; |
| 20 | + private int $hitsCount; |
| 21 | + private int $offset; |
| 22 | + private int $limit; |
| 23 | + private int $processingTimeMs; |
| 24 | + |
| 25 | + private string $id; |
| 26 | + |
| 27 | + public function __construct(array $body) |
| 28 | + { |
| 29 | + $this->id = $body['id']; |
| 30 | + $this->hits = $body['hits'] ?? []; |
| 31 | + $this->hitsCount = \count($body['hits']); |
| 32 | + $this->processingTimeMs = $body['processingTimeMs']; |
| 33 | + $this->offset = $body['offset']; |
| 34 | + $this->limit = $body['limit']; |
| 35 | + $this->estimatedTotalHits = $body['estimatedTotalHits']; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Return a new {@see SearchResult} instance. |
| 40 | + * |
| 41 | + * The $options parameter is an array, and the following keys are accepted: |
| 42 | + * - transformHits (callable) |
| 43 | + * |
| 44 | + * The method does NOT trigger a new search. |
| 45 | + */ |
| 46 | + public function applyOptions($options): self |
| 47 | + { |
| 48 | + if (\array_key_exists('transformHits', $options) && \is_callable($options['transformHits'])) { |
| 49 | + $this->transformHits($options['transformHits']); |
| 50 | + } |
| 51 | + |
| 52 | + return $this; |
| 53 | + } |
| 54 | + |
| 55 | + public function transformHits(callable $callback): self |
| 56 | + { |
| 57 | + $this->hits = $callback($this->hits); |
| 58 | + $this->hitsCount = \count($this->hits); |
| 59 | + |
| 60 | + return $this; |
| 61 | + } |
| 62 | + |
| 63 | + public function getHit(int $key, $default = null) |
| 64 | + { |
| 65 | + return $this->hits[$key] ?? $default; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @return array<int, array> |
| 70 | + */ |
| 71 | + public function getHits(): array |
| 72 | + { |
| 73 | + return $this->hits; |
| 74 | + } |
| 75 | + |
| 76 | + public function getOffset(): int |
| 77 | + { |
| 78 | + return $this->offset; |
| 79 | + } |
| 80 | + |
| 81 | + public function getLimit(): int |
| 82 | + { |
| 83 | + return $this->limit; |
| 84 | + } |
| 85 | + |
| 86 | + public function getEstimatedTotalHits(): int |
| 87 | + { |
| 88 | + return $this->estimatedTotalHits; |
| 89 | + } |
| 90 | + |
| 91 | + public function getProcessingTimeMs(): int |
| 92 | + { |
| 93 | + return $this->processingTimeMs; |
| 94 | + } |
| 95 | + |
| 96 | + public function getId(): string |
| 97 | + { |
| 98 | + return $this->id; |
| 99 | + } |
| 100 | + |
| 101 | + public function getHitsCount(): int |
| 102 | + { |
| 103 | + return $this->hitsCount; |
| 104 | + } |
| 105 | + |
| 106 | + public function toArray(): array |
| 107 | + { |
| 108 | + $arr = [ |
| 109 | + 'id' => $this->id, |
| 110 | + 'hits' => $this->hits, |
| 111 | + 'hitsCount' => $this->hitsCount, |
| 112 | + 'processingTimeMs' => $this->processingTimeMs, |
| 113 | + 'offset' => $this->offset, |
| 114 | + 'limit' => $this->limit, |
| 115 | + 'estimatedTotalHits' => $this->estimatedTotalHits, |
| 116 | + ]; |
| 117 | + |
| 118 | + return $arr; |
| 119 | + } |
| 120 | + |
| 121 | + public function toJSON(): string |
| 122 | + { |
| 123 | + return json_encode($this->toArray(), JSON_PRETTY_PRINT); |
| 124 | + } |
| 125 | + |
| 126 | + public function getIterator(): \ArrayIterator |
| 127 | + { |
| 128 | + return new \ArrayIterator($this->hits); |
| 129 | + } |
| 130 | + |
| 131 | + public function count(): int |
| 132 | + { |
| 133 | + return $this->hitsCount; |
| 134 | + } |
| 135 | +} |
0 commit comments