Passed
Pull Request — master (#123)
by
unknown
04:29
created

ResultDocument::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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']] ?? false;
119
        $this->type = $record[$fields['type']];
120
121
        if (!empty($highlighting[$this->id])) {
122
            $highlightingForRecord = $highlighting[$this->id][$fields['fulltext']];
123
            $this->snippetsForRecord = is_array($highlightingForRecord['snippets']) ? $highlightingForRecord['snippets'] : [];
124
        }
125
126
        $this->parseSnippets();
127
        $this->parsePages();
128
        $this->parseRegions();
129
        $this->parseHighlights();
130
    }
131
132
    /**
133
     * Get the result's record identifier.
134
     *
135
     * @access public
136
     *
137
     * @return string The result's record identifier
138
     */
139
    public function getId(): ?string
140
    {
141
        return $this->id;
142
    }
143
144
    /**
145
     * Get the result's record unified identifier.
146
     *
147
     * @access public
148
     *
149
     * @return string|null The result's record unified identifier
150
     */
151
    public function getUid(): ?string
152
    {
153
        return $this->uid;
154
    }
155
156
    /**
157
     * Get the result's record page.
158
     *
159
     * @access public
160
     *
161
     * @return int|null The result's record page
162
     */
163
    public function getPage(): ?int
164
    {
165
        return $this->page;
166
    }
167
168
    /**
169
     * Get all result's record snippets imploded to one string.
170
     *
171
     * @access public
172
     *
173
     * @return string|null All result's record snippets imploded to one string
174
     */
175
    public function getSnippets(): ?string
176
    {
177
        return $this->snippets;
178
    }
179
180
    /**
181
     * Get the thumbnail URL
182
     *
183
     * @access public
184
     *
185
     * @return string|null
186
     */
187
    public function getThumbnail(): ?string
188
    {
189
        return $this->thumbnail;
190
    }
191
192
    /**
193
     * Get the title
194
     *
195
     * @access public
196
     *
197
     * @return string|null
198
     */
199
    public function getTitle(): ?string
200
    {
201
        return $this->title;
202
    }
203
204
    /**
205
     * Get the toplevel flag
206
     *
207
     * @access public
208
     *
209
     * @return bool
210
     */
211
    public function getToplevel(): bool
212
    {
213
        return $this->toplevel;
214
    }
215
216
    /**
217
     * Get the structure type
218
     *
219
     * @access public
220
     *
221
     * @return string|null
222
     */
223
    public function getType(): ?string
224
    {
225
        return $this->type;
226
    }
227
228
    /**
229
     * Get all result's pages which contain search phrase.
230
     *
231
     * @access public
232
     *
233
     * @return array(Page) All result's pages which contain search phrase
234
     */
235
    public function getPages(): array
236
    {
237
        return $this->pages;
238
    }
239
240
    /**
241
     * Get all result's regions which contain search phrase.
242
     *
243
     * @access public
244
     *
245
     * @return array(Region) All result's regions which contain search phrase
246
     */
247
    public function getRegions(): array
248
    {
249
        return $this->regions;
250
    }
251
252
    /**
253
     * Get all result's highlights of search phrase.
254
     *
255
     * @access public
256
     *
257
     * @return array(Highlight) All result's highlights of search phrase
258
     */
259
    public function getHighlights(): array
260
    {
261
        return $this->highlights;
262
    }
263
264
    /**
265
     * Get all result's highlights' ids of search phrase.
266
     *
267
     * @access public
268
     *
269
     * @return array(string) All result's highlights of search phrase
270
     */
271
    public function getHighlightsIds(): array
272
    {
273
        $highlightsIds = [];
274
        foreach ($this->highlights as $highlight) {
275
            array_push($highlightsIds, $highlight->getId());
276
        }
277
        return $highlightsIds;
278
    }
279
280
    /**
281
     * Parse snippets array to string for displaying purpose.
282
     * Snippets are stored in 'text' field of 'snippets' object.
283
     *
284
     * @access private
285
     *
286
     * @return void
287
     */
288
    private function parseSnippets(): void
289
    {
290
        $snippetArray = $this->getArrayByIndex('text');
291
292
        $this->snippets = !empty($snippetArray) ? implode(' [...] ', $snippetArray) : '';
293
    }
294
295
    /**
296
     * Parse pages array to array of Page objects.
297
     * Pages are stored in 'pages' field of 'snippets' object.
298
     *
299
     * @access private
300
     *
301
     * @return void
302
     */
303
    private function parsePages(): void
304
    {
305
        $pageArray = $this->getArrayByIndex('pages');
306
307
        $i = 0;
308
        foreach ($pageArray as $pages) {
309
            foreach ($pages as $page) {
310
                array_push($this->pages, new Page($i, $page));
311
                $i++;
312
            }
313
        }
314
    }
315
316
    /**
317
     * Parse regions array to array of Region objects.
318
     * Regions are stored in 'regions' field of 'snippets' object.
319
     *
320
     * @access private
321
     *
322
     * @return void
323
     */
324
    private function parseRegions(): void
325
    {
326
        $regionArray = $this->getArrayByIndex('regions');
327
328
        $i = 0;
329
        foreach ($regionArray as $regions) {
330
            foreach ($regions as $region) {
331
                array_push($this->regions, new Region($i, $region));
332
                $i++;
333
            }
334
        }
335
    }
336
337
    /**
338
     * Parse highlights array to array of Highlight objects.
339
     * Highlights are stored in 'highlights' field of 'snippets' object.
340
     *
341
     * @access private
342
     *
343
     * @return void
344
     */
345
    private function parseHighlights(): void
346
    {
347
        $highlightArray = $this->getArrayByIndex('highlights');
348
349
        foreach ($highlightArray as $highlights) {
350
            foreach ($highlights as $highlight) {
351
                foreach ($highlight as $hl) {
352
                    array_push($this->highlights, new Highlight($hl));
353
                }
354
            }
355
        }
356
    }
357
358
    /**
359
     * Get array for given index.
360
     *
361
     * @access private
362
     *
363
     * @param string $index: Name of field for which array is going be created
364
     *
365
     * @return array
366
     */
367
    private function getArrayByIndex(string $index): array
368
    {
369
        $objectArray = [];
370
        foreach ($this->snippetsForRecord as $snippet) {
371
            if (!empty($snippet[$index])) {
372
                array_push($objectArray, $snippet[$index]);
373
            }
374
        }
375
        return $objectArray;
376
    }
377
}
378