Passed
Pull Request — master (#123)
by
unknown
03:43
created

ResultDocument::getHighlightsIds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Common\Solr\SearchResult;
14
15
use Solarium\QueryType\Select\Result\Document;
16
17
/**
18
 * ResultDocument class for the 'dlf' extension. It keeps the result of the search in the SOLR index.
19
 *
20
 * @package TYPO3
21
 * @subpackage dlf
22
 *
23
 * @access public
24
 */
25
class ResultDocument
26
{
27
28
    /**
29
     * @access private
30
     * @var string The identifier
31
     */
32
    private ?string $id;
33
34
    /**
35
     * @access private
36
     * @var string|null The unified identifier
37
     */
38
    private ?string $uid;
39
40
    /**
41
     * @access private
42
     * @var int|null The page on which result was found
43
     */
44
    private ?int $page;
45
46
    /**
47
     * @access private
48
     * @var string|null All snippets imploded to one string
49
     */
50
    private ?string $snippets;
51
52
    /**
53
     * @access private
54
     * @var string|null The thumbnail URL
55
     */
56
    private ?string $thumbnail;
57
58
    /**
59
     * @access private
60
     * @var string|null The title of the document / structure element (e.g. chapter)
61
     */
62
    private ?string $title;
63
64
    /**
65
     * @access private
66
     * @var bool It's a toplevel element?
67
     */
68
    private bool $toplevel = false;
69
70
    /**
71
     * @access private
72
     * @var string|null The structure type
73
     */
74
    private ?string $type;
75
76
    /**
77
     * @access private
78
     * @var Page[] All pages in which search phrase was found
79
     */
80
    private array $pages = [];
81
82
    /**
83
     * @access private
84
     * @var Region[] All regions in which search phrase was found
85
     */
86
    private array $regions = [];
87
88
    /**
89
     * @access private
90
     * @var Highlight[] All highlights of search phrase
91
     */
92
    private array $highlights = [];
93
94
    /**
95
     * @access private
96
     * @var array The snippets for given record
97
     */
98
    private array $snippetsForRecord = [];
99
100
    /**
101
     * The constructor for result.
102
     *
103
     * @access public
104
     *
105
     * @param Document $record: found document record
106
     * @param array $highlighting: Array of found highlight elements
107
     * @param array $fields: Array of fields used for search
108
     *
109
     * @return void
110
     */
111
    public function __construct(Document $record, array $highlighting, array $fields)
112
    {
113
        $this->id = $record[$fields['id']];
114
        $this->uid = $record[$fields['uid']];
115
        $this->page = $record[$fields['page']];
116
        $this->thumbnail = $record[$fields['thumbnail']];
117
        $this->title = $record[$fields['title']];
118
        $this->toplevel = $record[$fields['toplevel']];
119
        $this->type = $record[$fields['type']];
120
121
        $highlightingForRecord = $highlighting[$record[$fields['id']]][$fields['fulltext']];
122
        $this->snippetsForRecord = is_array($highlightingForRecord['snippets']) ? $highlightingForRecord['snippets'] : [];
123
124
        $this->parseSnippets();
125
        $this->parsePages();
126
        $this->parseRegions();
127
        $this->parseHighlights();
128
    }
129
130
    /**
131
     * Get the result's record identifier.
132
     *
133
     * @access public
134
     *
135
     * @return string The result's record identifier
136
     */
137
    public function getId(): ?string
138
    {
139
        return $this->id;
140
    }
141
142
    /**
143
     * Get the result's record unified identifier.
144
     *
145
     * @access public
146
     *
147
     * @return string|null The result's record unified identifier
148
     */
149
    public function getUid(): ?string
150
    {
151
        return $this->uid;
152
    }
153
154
    /**
155
     * Get the result's record page.
156
     *
157
     * @access public
158
     *
159
     * @return int|null The result's record page
160
     */
161
    public function getPage(): ?int
162
    {
163
        return $this->page;
164
    }
165
166
    /**
167
     * Get all result's record snippets imploded to one string.
168
     *
169
     * @access public
170
     *
171
     * @return string|null All result's record snippets imploded to one string
172
     */
173
    public function getSnippets(): ?string
174
    {
175
        return $this->snippets;
176
    }
177
178
    /**
179
     * Get the thumbnail URL
180
     *
181
     * @access public
182
     *
183
     * @return string|null
184
     */
185
    public function getThumbnail(): ?string
186
    {
187
        return $this->thumbnail;
188
    }
189
190
    /**
191
     * Get the title
192
     *
193
     * @access public
194
     *
195
     * @return string|null
196
     */
197
    public function getTitle(): ?string
198
    {
199
        return $this->title;
200
    }
201
202
    /**
203
     * Get the toplevel flag
204
     *
205
     * @access public
206
     *
207
     * @return bool
208
     */
209
    public function getToplevel(): bool
210
    {
211
        return $this->toplevel;
212
    }
213
214
    /**
215
     * Get the structure type
216
     *
217
     * @access public
218
     *
219
     * @return string|null
220
     */
221
    public function getType(): ?string
222
    {
223
        return $this->type;
224
    }
225
226
    /**
227
     * Get all result's pages which contain search phrase.
228
     *
229
     * @access public
230
     *
231
     * @return array(Page) All result's pages which contain search phrase
232
     */
233
    public function getPages(): array
234
    {
235
        return $this->pages;
236
    }
237
238
    /**
239
     * Get all result's regions which contain search phrase.
240
     *
241
     * @access public
242
     *
243
     * @return array(Region) All result's regions which contain search phrase
244
     */
245
    public function getRegions(): array
246
    {
247
        return $this->regions;
248
    }
249
250
    /**
251
     * Get all result's highlights of search phrase.
252
     *
253
     * @access public
254
     *
255
     * @return array(Highlight) All result's highlights of search phrase
256
     */
257
    public function getHighlights(): array
258
    {
259
        return $this->highlights;
260
    }
261
262
    /**
263
     * Get all result's highlights' ids of search phrase.
264
     *
265
     * @access public
266
     *
267
     * @return array(string) All result's highlights of search phrase
268
     */
269
    public function getHighlightsIds(): array
270
    {
271
        $highlightsIds = [];
272
        foreach ($this->highlights as $highlight) {
273
            array_push($highlightsIds, $highlight->getId());
274
        }
275
        return $highlightsIds;
276
    }
277
278
    /**
279
     * Parse snippets array to string for displaying purpose.
280
     * Snippets are stored in 'text' field of 'snippets' object.
281
     *
282
     * @access private
283
     *
284
     * @return void
285
     */
286
    private function parseSnippets(): void
287
    {
288
        $snippetArray = $this->getArrayByIndex('text');
289
290
        $this->snippets = !empty($snippetArray) ? implode(' [...] ', $snippetArray) : '';
291
    }
292
293
    /**
294
     * Parse pages array to array of Page objects.
295
     * Pages are stored in 'pages' field of 'snippets' object.
296
     *
297
     * @access private
298
     *
299
     * @return void
300
     */
301
    private function parsePages(): void
302
    {
303
        $pageArray = $this->getArrayByIndex('pages');
304
305
        $i = 0;
306
        foreach ($pageArray as $pages) {
307
            foreach ($pages as $page) {
308
                array_push($this->pages, new Page($i, $page));
309
                $i++;
310
            }
311
        }
312
    }
313
314
    /**
315
     * Parse regions array to array of Region objects.
316
     * Regions are stored in 'regions' field of 'snippets' object.
317
     *
318
     * @access private
319
     *
320
     * @return void
321
     */
322
    private function parseRegions(): void
323
    {
324
        $regionArray = $this->getArrayByIndex('regions');
325
326
        $i = 0;
327
        foreach ($regionArray as $regions) {
328
            foreach ($regions as $region) {
329
                array_push($this->regions, new Region($i, $region));
330
                $i++;
331
            }
332
        }
333
    }
334
335
    /**
336
     * Parse highlights array to array of Highlight objects.
337
     * Highlights are stored in 'highlights' field of 'snippets' object.
338
     *
339
     * @access private
340
     *
341
     * @return void
342
     */
343
    private function parseHighlights(): void
344
    {
345
        $highlightArray = $this->getArrayByIndex('highlights');
346
347
        foreach ($highlightArray as $highlights) {
348
            foreach ($highlights as $highlight) {
349
                foreach ($highlight as $hl) {
350
                    array_push($this->highlights, new Highlight($hl));
351
                }
352
            }
353
        }
354
    }
355
356
    /**
357
     * Get array for given index.
358
     *
359
     * @access private
360
     *
361
     * @param string $index: Name of field for which array is going be created
362
     *
363
     * @return array
364
     */
365
    private function getArrayByIndex(string $index): array
366
    {
367
        $objectArray = [];
368
        foreach ($this->snippetsForRecord as $snippet) {
369
            if (!empty($snippet[$index])) {
370
                array_push($objectArray, $snippet[$index]);
371
            }
372
        }
373
        return $objectArray;
374
    }
375
}
376