Passed
Pull Request — master (#72)
by
unknown
08:48
created

ResultDocument::getPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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\SolrSearchResult;
14
15
use Kitodo\Dlf\Common\SolrSearchResult\Highlight;
16
use Kitodo\Dlf\Common\SolrSearchResult\Page;
17
use Kitodo\Dlf\Common\SolrSearchResult\Region;
18
19
/**
20
 * ResultDocument class for the 'dlf' extension. It keeps te result of the search in the SOLR index.
21
 *
22
 * @author Beatrycze Volk <[email protected]>
23
 * @package TYPO3
24
 * @subpackage dlf
25
 * @access public
26
 */
27
class ResultDocument
28
{
29
30
    /**
31
     * The identifier
32
     *
33
     * @var string
34
     * @access private
35
     */
36
    private $id;
37
38
    /**
39
     * The unified identifier
40
     *
41
     * @var string|null
42
     * @access private
43
     */
44
    private $uid;
45
46
    /**
47
     * The page on which result was found
48
     *
49
     * @var int
50
     * @access private
51
     */
52
    private $page;
53
54
    /**
55
     * All snippets imploded to one string
56
     *
57
     * @var string
58
     * @access private
59
     */
60
    private $snippets;
61
62
    /**
63
     * All pages in which search phrase was found
64
     *
65
     * @var array(Page)
66
     * @access private
67
     */
68
    private $pages = [];
69
70
    /**
71
     * All regions in which search phrase was found
72
     *
73
     * @var array(Region)
74
     * @access private
75
     */
76
    private $regions = [];
77
78
    /**
79
     * All highlights of search phrase
80
     *
81
     * @var array(Highlight)
82
     * @access private
83
     */
84
    private $highlights = [];
85
86
    /**
87
     * The snippets for given record
88
     *
89
     * @var array
90
     * @access private
91
     */
92
    private $snippetsForRecord = [];
93
94
    /**
95
     * The constructor for result.
96
     *
97
     * @access public
98
     *
99
     * @param array $record: Array of found document record
100
     * @param array $highlighting: Array of found highlight elements
101
     * @param array $fields: Array of fields used for search
102
     *
103
     * @return void
104
     */
105
    public function __construct($record, $highlighting, $fields)
106
    {
107
        $this->id = $record[$fields['id']];
108
        $this->uid = $record[$fields['uid']];
109
        $this->page = $record[$fields['page']];
110
111
        $highlightingForRecord = $highlighting[$record[$fields['id']]][$fields['fulltext']];
112
        $this->snippetsForRecord = is_array($highlightingForRecord['snippets']) ? $highlightingForRecord['snippets'] : [];
113
114
        $this->parseSnippets();
115
        $this->parsePages();
116
        $this->parseRegions();
117
        $this->parseHighlights();
118
    }
119
120
    /**
121
     * Get the result's record identifier.
122
     *
123
     * @access public
124
     *
125
     * @return string The result's record identifier
126
     */
127
    public function getId()
128
    {
129
        return $this->id;
130
    }
131
132
    /**
133
     * Get the result's record unified identifier.
134
     *
135
     * @access public
136
     *
137
     * @return string|null The result's record unified identifier
138
     */
139
    public function getUid()
140
    {
141
        return $this->uid;
142
    }
143
144
    /**
145
     * Get the result's record page.
146
     *
147
     * @access public
148
     *
149
     * @return int The result's record page
150
     */
151
    public function getPage()
152
    {
153
        return $this->page;
154
    }
155
156
    /**
157
     * Get all result's record snippets imploded to one string.
158
     *
159
     * @access public
160
     *
161
     * @return string All result's record snippets imploded to one string
162
     */
163
    public function getSnippets()
164
    {
165
        return $this->snippets;
166
    }
167
168
    /**
169
     * Get all result's pages which contain search phrase.
170
     *
171
     * @access public
172
     *
173
     * @return array(Page) All result's pages which contain search phrase
174
     */
175
    public function getPages()
176
    {
177
        return $this->pages;
178
    }
179
180
    /**
181
     * Get all result's regions which contain search phrase.
182
     *
183
     * @access public
184
     *
185
     * @return array(Region) All result's regions which contain search phrase
186
     */
187
    public function getRegions()
188
    {
189
        return $this->regions;
190
    }
191
192
    /**
193
     * Get all result's highlights of search phrase.
194
     *
195
     * @access public
196
     *
197
     * @return array(Highlight) All result's highlights of search phrase
198
     */
199
    public function getHighlights()
200
    {
201
        return $this->highlights;
202
    }
203
204
    /**
205
     * Get all result's highlights' ids of search phrase.
206
     *
207
     * @access public
208
     *
209
     * @return array(string) All result's highlights of search phrase
210
     */
211
    public function getHighlightsIds()
212
    {
213
        $highlightsIds = [];
214
        foreach ($this->highlights as $highlight) {
215
            array_push($highlightsIds, $highlight->getId());
216
        }
217
        return $highlightsIds;
218
    }
219
220
    /**
221
     * Parse snippets array to string for displaying purpose.
222
     * Snippets are stored in 'text' field of 'snippets' object.
223
     *
224
     * @access private
225
     *
226
     * @return void
227
     */
228
    private function parseSnippets()
229
    {
230
        $snippetArray = $this->getArrayByIndex('text');
231
232
        $this->snippets = !empty($snippetArray) ? implode(' [...] ', $snippetArray) : '';
233
    }
234
235
    /**
236
     * Parse pages array to array of Page objects.
237
     * Pages are stored in 'pages' field of 'snippets' object.
238
     *
239
     * @access private
240
     *
241
     * @return void
242
     */
243
    private function parsePages()
244
    {
245
        $pageArray = $this->getArrayByIndex('pages');
246
247
        $i = 0;
248
        foreach ($pageArray as $pages) {
249
            foreach ($pages as $page) {
250
                array_push($this->pages, new Page($i, $page));
251
                $i++;
252
            }
253
        }
254
    }
255
256
    /**
257
     * Parse regions array to array of Region objects.
258
     * Regions are stored in 'regions' field of 'snippets' object.
259
     *
260
     * @access private
261
     *
262
     * @return void
263
     */
264
    private function parseRegions()
265
    {
266
        $regionArray = $this->getArrayByIndex('regions');
267
268
        $i = 0;
269
        foreach ($regionArray as $regions) {
270
            foreach ($regions as $region) {
271
                array_push($this->regions, new Region($i, $region));
272
                $i++;
273
            }
274
        }
275
    }
276
277
    /**
278
     * Parse highlights array to array of Highlight objects.
279
     * Highlights are stored in 'highlights' field of 'snippets' object.
280
     *
281
     * @access private
282
     *
283
     * @return void
284
     */
285
    private function parseHighlights()
286
    {
287
        $highlightArray = $this->getArrayByIndex('highlights');
288
289
        foreach ($highlightArray as $highlights) {
290
            foreach ($highlights as $highlight) {
291
                foreach ($highlight as $hl) {
292
                    array_push($this->highlights, new Highlight($hl));
293
                }
294
            }
295
        }
296
    }
297
298
    /**
299
     * Get array for given index.
300
     *
301
     * @access private
302
     *
303
     * @param string $index: Name of field for which array is going be created
304
     *
305
     * @return array
306
     */
307
    private function getArrayByIndex($index)
308
    {
309
        $objectArray = [];
310
        foreach ($this->snippetsForRecord as $snippet) {
311
            array_push($objectArray, $snippet[$index]);
312
        }
313
        return $objectArray;
314
    }
315
}
316