ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilLuceneSearchResult.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21
28class ilLuceneSearchResult implements Iterator
29{
33 private array $listener = [];
34 private int $position = 0;
35
36 private int $limit = 0;
37 private int $total_hits = 0;
38 private float $max_score = 0;
39
40 private array $objects = [];
41 private array $relevance = [];
42
43
44
49 public function setCallback(array $a_callback): void
50 {
51 $this->listener = $a_callback;
52 }
53
57 public function rewind(): void
58 {
59 $this->position = 0;
60 }
61
67 public function valid(): bool
68 {
69 if ($this->position < count($this->objects)) {
70 return true;
71 }
72 // if the number of candidates is smaller than the total number of hits
73 // get next result page
74 if (count($this->objects) < $this->getTotalHits()) {
75 ilLoggerFactory::getLogger('src')->debug("Trying to get next result page...");
76 @call_user_func($this->listener);
77 }
78 // Check again
79 if ($this->position < count($this->objects)) {
80 return true;
81 }
82 return false;
83 }
84
89 public function key(): int
90 {
91 return $this->position;
92 }
93
98 public function current(): int
99 {
100 return $this->objects[$this->position];
101 }
102
106 public function next(): void
107 {
108 $this->position++;
109 }
110
111
112
113 public function getCandidates(): array
114 {
115 return $this->objects;
116 }
117
118 public function addObject(int $a_value, float $a_relevance = 0): void
119 {
120 $this->objects[] = $a_value;
121 $this->relevance[$a_value] = $a_relevance;
122 }
123
124 public function getRelevance(int $a_obj_id): float
125 {
126 if (!$this->getMaxScore()) {
127 return 0;
128 }
129 return isset($this->relevance[$a_obj_id]) ? $this->relevance[$a_obj_id] / $this->getMaxScore() * 100 : 0;
130 }
131
132
133 public function setLimit(int $a_limit): void
134 {
135 $this->limit = $a_limit;
136 }
137
138 public function getLimit(): int
139 {
140 return $this->limit;
141 }
142
143
144 public function setMaxScore(float $a_score): void
145 {
146 $this->max_score = $a_score;
147 }
148
149 public function getMaxScore(): float
150 {
151 return $this->max_score;
152 }
153
154 public function setTotalHits(int $a_hits): void
155 {
156 $this->total_hits = $a_hits;
157 }
158
159 public function getTotalHits(): int
160 {
161 return $this->total_hits;
162 }
163}
static getLogger(string $a_component_id)
Get component logger.
Search result implementing iterator interface.
setCallback(array $a_callback)
set search callback
addObject(int $a_value, float $a_relevance=0)