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

FieldCollapsing::getEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\Query;
28
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
29
30
/**
31
 * The FieldCollapsing ParameterProvider is responsible to build the solr query parameters
32
 * that are needed for the field collapsing.
33
 *
34
 * @package ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder
35
 */
36
class FieldCollapsing extends AbstractDeactivatableParameterBuilder implements ParameterBuilder
37
{
38
    /**
39
     * @var string
40
     */
41
    protected $collapseFieldName = 'variantId';
42
43
    /**
44
     * @var bool
45
     */
46
    protected $expand = false;
47
48
    /**
49
     * @var int
50
     */
51
    protected $expandRowCount = 10;
52
53
    /**
54
     * FieldCollapsing constructor.
55
     * @param bool $isEnabled
56
     * @param string $collapseFieldName
57
     * @param bool $expand
58
     * @param int $expandRowCount
59
     */
60
    public function __construct($isEnabled, $collapseFieldName = 'variantId', $expand = false, $expandRowCount = 10)
61
    {
62
        $this->isEnabled = $isEnabled;
63
        $this->collapseFieldName = $collapseFieldName;
64
        $this->expand = $expand;
65
        $this->expandRowCount = $expandRowCount;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getCollapseFieldName(): string
72
    {
73
        return $this->collapseFieldName;
74
    }
75
76
    /**
77
     * @param string $collapseFieldName
78
     */
79
    public function setCollapseFieldName(string $collapseFieldName)
80
    {
81
        $this->collapseFieldName = $collapseFieldName;
82
    }
83
84
    /**
85
     * @return boolean
86
     */
87
    public function getIsExpand(): bool
88
    {
89
        return $this->expand;
90
    }
91
92
    /**
93
     * @param boolean $expand
94
     */
95
    public function setExpand(bool $expand)
96
    {
97
        $this->expand = $expand;
98
    }
99
100
    /**
101
     * @return int
102
     */
103
    public function getExpandRowCount(): int
104
    {
105
        return $this->expandRowCount;
106
    }
107
108
    /**
109
     * @param int $expandRowCount
110
     */
111
    public function setExpandRowCount(int $expandRowCount)
112
    {
113
        $this->expandRowCount = $expandRowCount;
114
    }
115
116
    /**
117
     * @param Query $query
118
     * @return Query
119
     */
120
    public function build(Query $query): Query
121
    {
122
        if (!$this->isEnabled) {
123
            $query->getFilters()->removeByName('collapsing');
124
            $query->getQueryParametersContainer()->remove('expand');
125
            $query->getQueryParametersContainer()->remove('expand.rows');
126
127
            return $query;
128
        }
129
130
        $query->getFilters()->add('{!collapse field=' . $this->collapseFieldName . '}', 'collapsing');
131
        if ($this->expand) {
132
            $query->getQueryParametersContainer()->set('expand', 'true');
133
            $query->getQueryParametersContainer()->set('expand.rows', $this->expandRowCount);
134
        }
135
136
        return $query;
137
    }
138
139
    /**
140
     * @param TypoScriptConfiguration $solrConfiguration
141
     * @return FieldCollapsing
142
     */
143
    public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration)
144
    {
145
        $isEnabled = $solrConfiguration->getSearchVariants();
146
        if (!$isEnabled) {
147
            return new FieldCollapsing(false);
148
        }
149
150
        $collapseField = $solrConfiguration->getSearchVariantsField();
151
        $expand = $solrConfiguration->getSearchVariantsExpand();
152
        $expandRows = $solrConfiguration->getSearchVariantsLimit();
153
154
        return new FieldCollapsing(true, $collapseField, $expand, $expandRows);
155
    }
156
157
    /**
158
     * @return FieldCollapsing
159
     */
160
    public static function getEmpty()
161
    {
162
        return new FieldCollapsing(false);
163
    }
164
}