Passed
Pull Request — master (#2925)
by Rafael
35:25
created

Highlighting::getPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Domain\Search\Query\AbstractQueryBuilder;
28
use ApacheSolrForTypo3\Solr\Domain\Search\Query\QueryBuilder;
29
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32
/**
33
 * The Highlighting ParameterProvider is responsible to build the solr query parameters
34
 * that are needed for the highlighting.
35
 */
36
class Highlighting extends AbstractDeactivatable implements ParameterBuilder
37
{
38
    /**
39
     * @var int
40
     */
41
    protected $fragmentSize = 200;
42
43
    /**
44
     * @var string
45
     */
46
    protected $highlightingFieldList = '';
47
48
    /**
49
     * @var string
50
     */
51
    protected $prefix = '';
52
53
    /**
54
     * @var string
55
     */
56
    protected $postfix = '';
57
58
    /**
59
     * Highlighting constructor.
60
     *
61
     * @param bool $isEnabled
62
     * @param int $fragmentSize
63
     * @param string $highlightingFieldList
64
     * @param string $prefix
65
     * @param string $postfix
66
     */
67 42
    public function __construct($isEnabled = false, $fragmentSize = 200, $highlightingFieldList = '', $prefix = '', $postfix = '')
68
    {
69 42
        $this->isEnabled = $isEnabled;
70 42
        $this->fragmentSize = $fragmentSize;
71 42
        $this->highlightingFieldList = $highlightingFieldList;
72 42
        $this->prefix = $prefix;
73 42
        $this->postfix = $postfix;
74 42
    }
75
76
    /**
77
     * @return int
78
     */
79 41
    public function getFragmentSize(): int
80
    {
81 41
        return $this->fragmentSize;
82
    }
83
84
    /**
85
     * @param int $fragmentSize
86
     */
87
    public function setFragmentSize(int $fragmentSize)
88
    {
89
        $this->fragmentSize = $fragmentSize;
90
    }
91
92
    /**
93
     * @return string
94
     */
95 41
    public function getHighlightingFieldList(): string
96
    {
97 41
        return $this->highlightingFieldList;
98
    }
99
100
    /**
101
     * @param string $highlightingFieldList
102
     */
103
    public function setHighlightingFieldList(string $highlightingFieldList)
104
    {
105
        $this->highlightingFieldList = $highlightingFieldList;
106
    }
107
108
    /**
109
     * @return string
110
     */
111 41
    public function getPrefix(): string
112
    {
113 41
        return $this->prefix;
114
    }
115
116
    /**
117
     * @param string $prefix
118
     */
119
    public function setPrefix(string $prefix)
120
    {
121
        $this->prefix = $prefix;
122
    }
123
124
    /**
125
     * @return string
126
     */
127 41
    public function getPostfix(): string
128
    {
129 41
        return $this->postfix;
130
    }
131
132
    /**
133
     * @param string $postfix
134
     */
135
    public function setPostfix(string $postfix)
136
    {
137
        $this->postfix = $postfix;
138
    }
139
140
    /**
141
     * @return bool
142
     */
143 41
    public function getUseFastVectorHighlighter()
144
    {
145 41
        return ($this->fragmentSize >= 18);
146
    }
147
148
149
    /**
150
     * @param TypoScriptConfiguration $solrConfiguration
151
     * @return Highlighting
152
     */
153 42
    public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration)
154
    {
155 42
        $isEnabled = $solrConfiguration->getSearchResultsHighlighting();
156 42
        if (!$isEnabled) {
157 1
            return new Highlighting(false);
158
        }
159
160 41
        $fragmentSize = $solrConfiguration->getSearchResultsHighlightingFragmentSize();
161 41
        $highlightingFields = $solrConfiguration->getSearchResultsHighlightingFields();
162 41
        $wrap = explode('|', $solrConfiguration->getSearchResultsHighlightingWrap());
163 41
        $prefix = isset($wrap[0]) ? $wrap[0] : '';
164 41
        $postfix = isset($wrap[1]) ? $wrap[1] : '';
165
166
167 41
        return new Highlighting($isEnabled, $fragmentSize, $highlightingFields, $prefix, $postfix);
168
    }
169
170
    /**
171
     * @return Highlighting
172
     */
173
    public static function getEmpty()
174
    {
175
        return new Highlighting(false);
176
    }
177
178
179
    /**
180
     * @param AbstractQueryBuilder $parentBuilder
181
     * @return AbstractQueryBuilder
182
     */
183 42
    public function build(AbstractQueryBuilder $parentBuilder): AbstractQueryBuilder
184
    {
185 42
        $query = $parentBuilder->getQuery();
186 42
        if(!$this->getIsEnabled()) {
187 1
            $query->removeComponent($query->getHighlighting());
188 1
            return $parentBuilder;
189
        }
190
191 41
        $query->getHighlighting()->setFragSize($this->getFragmentSize());
192 41
        $query->getHighlighting()->setFields(GeneralUtility::trimExplode(",", $this->getHighlightingFieldList()));
193
194 41
        if ($this->getUseFastVectorHighlighter()) {
195 41
            $query->getHighlighting()->setUseFastVectorHighlighter(true);
196 41
            $query->getHighlighting()->setTagPrefix($this->getPrefix());
197 41
            $query->getHighlighting()->setTagPostfix($this->getPostfix());
198
        } else {
199
            $query->getHighlighting()->setUseFastVectorHighlighter(false);
200
            $query->getHighlighting()->setTagPrefix('');
201
            $query->getHighlighting()->setTagPostfix('');
202
        }
203
204 41
        if ($this->getPrefix() !== '' && $this->getPostfix() !== '') {
205 41
            $query->getHighlighting()->setSimplePrefix($this->getPrefix());
206 41
            $query->getHighlighting()->setSimplePostfix($this->getPostfix());
207
        }
208
209 41
        return $parentBuilder;
210
    }
211
}
212