Completed
Push — master ( 6ee2a7...5f60a5 )
by Rafael
04:29
created

Slops::getBigramPhraseSlopFromConfiguration()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 8
nop 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
use ApacheSolrForTypo3\Solr\Domain\Search\Query\Query;
27
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
28
29
/**
30
 * The Slops ParameterProvider is responsible to build the solr query parameters
31
 * that are needed for the several slop arguments.
32
 *
33
 * @package ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder
34
 */
35
class Slops implements ParameterBuilder
36
{
37
    const NO_SLOP = null;
38
39
    /**
40
     * The qs parameter
41
     *
42
     * @var int
43
     */
44
    protected $querySlop = self::NO_SLOP;
45
46
    /**
47
     * @var int
48
     */
49
    protected $phraseSlop = self::NO_SLOP;
50
51
    /**
52
     * @var int
53
     */
54
    protected $bigramPhraseSlop = self::NO_SLOP;
55
56
    /**
57
     * @var int
58
     */
59
    protected $trigramPhraseSlop = self::NO_SLOP;
60
61
    /**
62
     * Slops constructor.
63
     * @param int|null $querySlop
64
     * @param int|null $phraseSlop
65
     * @param int|null $bigramPhraseSlop
66
     * @param int|null $trigramPhraseSlop
67
     */
68
    public function __construct($querySlop = self::NO_SLOP, $phraseSlop = self::NO_SLOP, $bigramPhraseSlop = self::NO_SLOP, $trigramPhraseSlop = self::NO_SLOP)
69
    {
70
        $this->querySlop = $querySlop;
71
        $this->phraseSlop = $phraseSlop;
72
        $this->bigramPhraseSlop = $bigramPhraseSlop;
73
        $this->trigramPhraseSlop = $trigramPhraseSlop;
74
    }
75
76
    /**
77
     * @return int
78
     */
79
    public function getQuerySlop(): int
80
    {
81
        return $this->querySlop;
82
    }
83
84
    /**
85
     * @param int $querySlop
86
     */
87
    public function setQuerySlop(int $querySlop)
88
    {
89
        $this->querySlop = $querySlop;
90
    }
91
92
    /**
93
     * @return int
94
     */
95
    public function getPhraseSlop(): int
96
    {
97
        return $this->phraseSlop;
98
    }
99
100
    /**
101
     * @param int $phraseSlop
102
     */
103
    public function setPhraseSlop(int $phraseSlop)
104
    {
105
        $this->phraseSlop = $phraseSlop;
106
    }
107
108
    /**
109
     * @return int
110
     */
111
    public function getBigramPhraseSlop(): int
112
    {
113
        return $this->bigramPhraseSlop;
114
    }
115
116
    /**
117
     * @param int $bigramPhraseSlop
118
     */
119
    public function setBigramPhraseSlop(int $bigramPhraseSlop)
120
    {
121
        $this->bigramPhraseSlop = $bigramPhraseSlop;
122
    }
123
124
    /**
125
     * @return int
126
     */
127
    public function getTrigramPhraseSlop(): int
128
    {
129
        return $this->trigramPhraseSlop;
130
    }
131
132
    /**
133
     * @param int $trigramPhraseSlop
134
     */
135
    public function setTrigramPhraseSlop(int $trigramPhraseSlop)
136
    {
137
        $this->trigramPhraseSlop = $trigramPhraseSlop;
138
    }
139
140
    /**
141
     * @param Query $query
142
     * @return Query
143
     */
144
    public function build(Query $query): Query
145
    {
146
        $query->getQueryParametersContainer()->setWhenIntOrUnsetWhenNull('qs', $this->querySlop);
147
        $query->getQueryParametersContainer()->setWhenIntOrUnsetWhenNull('ps', $this->phraseSlop);
148
        $query->getQueryParametersContainer()->setWhenIntOrUnsetWhenNull('ps2', $this->bigramPhraseSlop);
149
        $query->getQueryParametersContainer()->setWhenIntOrUnsetWhenNull('ps3', $this->trigramPhraseSlop);
150
151
        return $query;
152
    }
153
154
    /**
155
     * @param TypoScriptConfiguration $solrConfiguration
156
     * @return Slops
157
     */
158
    public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration)
159
    {
160
        $searchConfiguration = $solrConfiguration->getSearchConfiguration();
161
        $querySlop = static::getQuerySlopFromConfiguration($searchConfiguration);
162
        $phraseSlop = static::getPhraseSlopFromConfiguration($searchConfiguration);
163
        $bigramPhraseSlop = static::getBigramPhraseSlopFromConfiguration($searchConfiguration);
164
        $trigramPhraseSlop = static::getTrigramPhraseSlopFromConfiguration($searchConfiguration);
165
        return new Slops($querySlop, $phraseSlop, $bigramPhraseSlop, $trigramPhraseSlop);
166
    }
167
168
    /**
169
     * @param array $searchConfiguration
170
     * @return int|null
171
     */
172
    protected static function getPhraseSlopFromConfiguration($searchConfiguration)
173
    {
174
        $phraseEnabled = !(empty($searchConfiguration['query.']['phrase']) || $searchConfiguration['query.']['phrase'] !== 1);
175
        $phraseSlopConfigured = !empty($searchConfiguration['query.']['phrase.']['slop']);
176
        return  ($phraseEnabled && $phraseSlopConfigured) ? $searchConfiguration['query.']['phrase.']['slop'] : self::NO_SLOP;
177
    }
178
179
    /**
180
     * @param array $searchConfiguration
181
     * @return int|null
182
     */
183
    protected static function getQuerySlopFromConfiguration($searchConfiguration)
184
    {
185
        $phraseEnabled = !(empty($searchConfiguration['query.']['phrase']) || $searchConfiguration['query.']['phrase'] !== 1);
186
        $querySlopConfigured = !empty($searchConfiguration['query.']['phrase.']['querySlop']);
187
        return ($phraseEnabled && $querySlopConfigured) ? $searchConfiguration['query.']['phrase.']['querySlop'] : self::NO_SLOP;
188
    }
189
190
    /**
191
     * @param array $searchConfiguration
192
     * @return int|null
193
     */
194
    protected static function getBigramPhraseSlopFromConfiguration($searchConfiguration)
195
    {
196
        $bigramPhraseEnabled = !empty($searchConfiguration['query.']['bigramPhrase']) && $searchConfiguration['query.']['bigramPhrase'] === 1;
197
        $bigramSlopConfigured = !empty($searchConfiguration['query.']['bigramPhrase.']['slop']);
198
        return ($bigramPhraseEnabled && $bigramSlopConfigured) ? $searchConfiguration['query.']['bigramPhrase.']['slop'] : self::NO_SLOP;
199
    }
200
201
    /**
202
     * @param array $searchConfiguration
203
     * @return int|null
204
     */
205
    protected static function getTrigramPhraseSlopFromConfiguration($searchConfiguration)
206
    {
207
        $trigramPhraseEnabled = !empty($searchConfiguration['query.']['trigramPhrase']) && $searchConfiguration['query.']['trigramPhrase'] === 1;
208
        $trigramSlopConfigured = !empty($searchConfiguration['query.']['trigramPhrase.']['slop']);
209
        return ($trigramPhraseEnabled && $trigramSlopConfigured) ? $searchConfiguration['query.']['trigramPhrase.']['slop'] : self::NO_SLOP;
210
    }
211
}